1

I'm using this code to count lines of the text, but I need to count also words and show to console how many words is in each row.

int main(int argc, char *argv[]){

    ifstream f1("text.txt"); ;
    char c;
    string b;
    int numchars[10] = {}, numlines = 0;

    f1.get(c);
    while (f1) {
        while (f1 && c != '\n') {

           // here I want to count how many words is in row
        }

        cout<<"in row: "<< numlines + 1 <<"words: "<< numchars[numlines] << endl;
        numlines = numlines + 1;
        f1.get(c);
   }

   f1.close();
   system("PAUSE");
   return EXIT_SUCCESS;
}
Ziezi
  • 6,375
  • 3
  • 39
  • 49
  • Much too complicated approach. Something simple like `while(iss >> word) { ++count; }` from an `istringstream` would do. – πάντα ῥεῖ Jan 07 '16 at 20:58
  • 1
    Something along the lines of `if (c==' ') ++numWords;` – Frecklefoot Jan 07 '16 at 20:58
  • 1
    You could use `std::getline` to read lines of text at a time. – R Sahu Jan 07 '16 at 20:59
  • You could search StackOverflow to see how other people have implemented this assignment. Try these keywords: "stackoverflow c++ count words". – Thomas Matthews Jan 07 '16 at 21:00
  • thanks you for the help. specialy for frecklefoot! – Stovi Ryžas Stotelėje Jan 07 '16 at 21:09
  • How can I updete my anwser to help other users ? the anwse is ' if(c==' ') numchars[numlines] = numchars[numlines] + 1; f1.get(c);' – Stovi Ryžas Stotelėje Jan 07 '16 at 21:10
  • Frecklefoot's suggestion is flawed in general, as words could be separated by more than one space, or by tabs, and even if that's known not to be the case for a specific input file - it counts the spaces on a line which would be one less than the number of words. Our good "simple truth"'s solution handles all that, but You should also at least be aware of things like "1) hello" - do you want to count "1)" as a word? How about "-----" if someone's done some crude underlining of a title on the line above? To handle these types of issues, you could leverage std::isalpha(c). – user433534 Jan 07 '16 at 23:45

1 Answers1

3

To count the number of lines and number of words you could try and brake it down to two simple tasks: first read each line from the text using getline() and , secondly, extract each word from a line using stringstream, after each successful (read line or extract word) action you could increment two variables that represent the number of lines and words.

The above could be implement like so:

ifstream f1("text.txt"); 
// check if file is successfully opened
if (!f1) cerr << "Can't open input file.";

string line;
int line_count = 0

string word;
int word_count = 0;

// read file line by line
while (getline(f1, line)) {

    // count line
    ++line_count;

    stringstream ss(line);

    // extract all words from line
    while (ss >> word) {

        // count word
        ++word_count;
    }
}

// print result
cout << "Total Lines: " << line_count <<" Total Words: "<< word_count << endl;
Ziezi
  • 6,375
  • 3
  • 39
  • 49