Program doesn't work. Program is for finding the no. of words in a text file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
#include <fstream>
int main()
{
    int words=0;
    ofstream fout ("Text.txt",ios::out);
    char str[100], s[10];
    cout<<"\nEnter text:";
    cin.getline(str, 100);
    fout<<str;
    fout.close();
    ifstream fin ("Text.txt",ios::in);
    while (!fin.eof())
    {
        fin.getline(s,10," ");
        words++;
    }
    cout<<"\nNo. of words in the file:"<<words;
    fin.close();
    return 0;
}


 In function 'int main()': 16:29: error: invalid conversion from 'const char*' to 'std::basic_istream<char>::char_type {aka char}' [-fpermissive] 
Last edited on
closed account (ybf3AqkS)
fin.getline(s,10," "); Use single quotes. ' '
Thanks!!
exfo's suggestion will remove the compiler error, but it won't fix the logic.

At the moment your words are limited to 10 letters (which rules out "programming"!) and have to be separated by single spaces - there are plenty of other delimiters (various forms of punctuation etc.)

I would recommend inputting your line into a string (called 'line' below) using an alternative form of getline:
getline( fin, line );
and breaking up line using string member functions (see the "Reference" section on this site).
Could you please send a sample code? @lastchance
I won't give you a whole solution, as that would constitute doing your homework.

Get each line of the file into a string (called, e.g., 'line') using my earlier suggestion. Write a for() loop to cycle through the characters of that string. The classic idiom for a new word is
- "new word if previous character was space and current character is not". The following function would achieve that:

1
2
3
4
bool isNewWord( char oldc, char c )
{
   return ( oldc == ' ' && c != ' ' );
}


You will have to loop through the characters of the line (having initialised oldc to a space for the first test and updated after each test).

If you want other criteria for new words (e.g. if you don't like an isolated number like 1 being counted as a new word you would have to include them in that function).

The advantage of having the whole line in a string is that it avoids problems with the carriage-return and new-line characters if you tried to read the text file character by character.

There are other techniques using string member functions.
closed account (ybf3AqkS)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    cout << "Enter a file name: ";

    string filename;
    cin >> filename;

    ifstream fin(filename);

    if(!fin)
    {
        cerr << "Error: could not open " << filename << '\n';
        return 1;
    }

    string word;
    int word_counter = 0;

    while(fin >> word)
        ++word_counter;

    cout << "Counted " << word_counter << " words\n";
}

Ah, I have to admit defeat! - exf0's solution would be better than mine, though you may have to change line 13 to
ifstream fin(filename.c_str());
to make it adhere to the original C++ standard (string rather than const char* in the constructor added in C++11 according to this site).

Mind you, I managed to break both suggestions with a text.dat file containing
I don't know how to do this homework - let's ask ...

It's not as easy a problem as originally thought!
Thanks!
And by the way, it's not a homework but a practise for my practicals!

:)
Here's another variant (stealing exf0's good ideas, but restricted to the original filename text.txt for simplicity). Numbers added to alphabet in the word validation so that Britain doesn't lose its most famous fictional secret agent!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

bool validWord( string word )
{
   string atLeastOneOf = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
   return ( word.find_first_of( atLeastOneOf ) != string::npos );
}

int main()
{
   int nwords = 0;
   string word;
   ifstream in( "text.txt" );
   while( in >> word ) 
   {
      if ( validWord( word ) ) nwords++;
   }
   in.close();
   cout << "Number of words = " << nwords << endl;
}
Topic archived. No new replies allowed.