I was supposed to count the number of words in each file and the number of lines in each file. However, the compiler is giving me hell and I'm about ready to explode in frustration.
I've tried this code and it keeps giving me the errors and I've no clue what they mean. (I was assigned this by an instructor who obviously thinks I know everything about C++, even though some in the class have never even taken that course. I have and I did ok, though I had a very hard time with it.)
#include <iostream> #include<fstream> #include<istream> #include<string> #include<vector> #include<stdio.h> #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, ifstream::in); char* line; line = file.getline(); char* ch; int tempWord = 0; ch = strtok(line, " "); while (ch != NULL) { tempWord++; } v.push_back(tempWord); lineCount++; while(!file.eof()) { line = 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; }
g++ WordCount.cpp
/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:22: error: no match for call to ‘(std::ifstream) (std::string&, const std::_Ios_Openmode&)’
WordCount.cpp:25: 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:40: 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>]
pradcoc@tranquility:~/IT383/16>
I thought there would be c++ functions that would make this easy but they're nto working. In fact, I'm not even sure that, if I get this to work, that it'll even count the words and lines correctly but some credit is better than none (My instructor is unfair and won't count it if it won't compile. Which is really unfair as I've never done this kind of C++ before.)
This thread has been cross posted here:http://www.codeguru.com/forum/showthread.php?p=2052666#post2052666
Although cross posting is allowed, for everyone's benefit, please read: