Trying to read the file

It doesnt display the world and i dont know how count the number


ex.
Give a number 1-100: 20
Give filename: test.dat
The file contains the word Homework and the number 20 appears 2 times


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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
    int count,limit,counter =0;
    char exit_prog;
    string word,number,filename,num;
    ifstream reading;
    
    cout << "Enter a number between 1 to 100" << endl;
    cin >> limit;
    
    while (limit <= 1 || limit >=100 )
    {
        cout << "Error" << endl;
        cout << "Enter a number between 1 to 100" << endl;
        cin >> limit;
        count ++;
        if ( count == 5)
        {
            cout << "You have so many try do you want to exit the program?" << endl;
            cout << "Y for and N for No" << endl;
            cin >> exit_prog;
            
            if (exit_prog == 'Y' || exit_prog == 'y')
            {
                cout << "Thank you for using the program"<< endl;
                EXIT_FAILURE;
                break;
            }
            
        }

    }
    
    cout << "What is the filename that you want to open" << endl;
    cin >> filename;
    
    reading.open(filename.c_str());
    if(reading)
    {
        reading >> word;
        while (reading >> number)
        {
            if (number == limit)
                counter += 1;
            
        }
        cout <<"The file contains the word " << word << " and the number " << limit << " appears " << counter << " times" << endl;
        
        
    }
    else
    {
        cout << "could not open the files." << endl;
    }
    reading.close();
}
bump?
What is the behavior of the program right now, and why is that behavior undesirable?
Can you show the contents of the file you're trying to pass to it?
I made file like to.txt and in that file it have homework and 10 29 29 30 40. I want to be show it homework and 29 two times
http://www.sendspace.com/file/zhf67q

I am tying to read and display the content of this file. But didnt show any thing... Can some show how to do or fix it?
Take that code you posted in your original post and declare number as an int instead of a string. Surely your compiler must have told you to do this.
the word doesnt appear either
Topic archived. No new replies allowed.