|
-
January 29th, 2012, 05:55 PM
#2
Re: Need to print out number of words and lines in a file
I've updated it to this and it's still getting a bunch of errors:
I really need this solved today.
Code:
#include <iostream>
#include<fstream>
#include<istream>
#include<string>
#include<vector>
#include <cstdlib>
#include "string.h"
using namespace std;
iostream keyboard;
ifstream file;
vector<int> v;
int main()
{
int lineCount = 0;
string filename;
cout << "Enter a file name.";
cin >> filename;
file(filename);
string line;
line = file.getline();
char* ch;
int tempWord = 0;
ch = strtok(line, " ");
while (ch != NULL)
{
tempWord++;
}
v.push_back(tempWord);
lineCount++;
while(!file.eof())
{
file >> file.getline();
char* ch;
int tempWord = 0;
ch = strtok(line, " ");
while (ch !=NULL)
{
tempWord++;
}
v.push_back(tempWord);
lineCount++;
}
cout <<"Line count: " << lineCount;
int total;
for (int i =0; i < v.size(); i++)
{
total = total + v[i];
}
cout << "Word count: " << total;
return 0;
}
/usr/include/c++/4.3/istream:801: error: ‘std::basic_iostream<_CharT, _Traits>::basic_iostream() [with _CharT = char, _Traits = std::char_traits<char>]’ is protected
WordCount.cpp:10: error: within this context
WordCount.cpp: In function ‘int main()’:
WordCount.cpp:21: error: no match for call to ‘(std::ifstream) (std::string&)’
WordCount.cpp:24: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::getline()’
/usr/include/c++/4.3/istream:598: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:409: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]
WordCount.cpp:27: error: cannot convert ‘std::string’ to ‘char*’ for argument ‘1’ to ‘char* strtok(char*, const char*)’
WordCount.cpp:38: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::getline()’
/usr/include/c++/4.3/istream:598: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.3/istream:409: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]
WordCount.cpp:42: error: cannot convert ‘std::string’ to ‘char*’ for argument ‘1’ to ‘char* strtok(char*, const char*)’
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|