Trouble Testing Program

I need to test the program with the text file I made called scores.txt, but I am having trouble doing so. Seems like the program can't find it. The text file contains this information:
76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189

Any help would be greatly appreciated. Thank You.


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
	

    #include<iostream>
    #include<string>
    #include<fstream>
    using namespace std;
         
    int main()
    {
        int score;
        int scoreRange[8] = {0};
        ifstream ifile;
        string fileName;
       
        cout << "Enter the name of the file containing the students' test scores: ";
        cin >> fileName;
         
        ifile.open(fileName.c_str());
     
        while (!ifile)
        {
            cout << "Error opening the file " << fileName << endl;
            cout << "Please try again: ";
            cin >> fileName;
            ifile.clear();
            ifile.open(fileName.c_str());
        }
     
        while (ifile >> score)
        {
            if (score < 25)
                scoreRange[0]++;
            else if (score < 50)
                scoreRange[1]++;
            else if (score < 75)
                scoreRange[2]++;
            else if (score < 100)
                scoreRange[3]++;
            else if (score < 125)
                scoreRange[4]++;
            else if (score < 150)
                scoreRange[5]++;
            else if (score < 175)
                scoreRange[6]++;
            else if (score <= 200)
                scoreRange[7]++;
            else
              cout << "Invalid score found" << endl;
           
        }
         
        cout << endl << "Number of students in each score range:"<< endl;
        for (size_t i = 0 ; i < 8; i++)
        {
            cout << "Number of scores between " << i * 25 << " - " << (i+1)*25 << ": "<< scoreRange[i] << endl;
        }
         
         
    }
Let me guess -- it's only reading the first score?

My guess is that the while (ifile >> score) part trips up when it tries to read a comma into the integer score variable, so the loop ends at that point.

You could do something like
1
2
3
4
5
while(ifile >> score)
{
    ifile.ignore(); // Toss out the next character (the comma)
    // Continue on with business
}
.
I'm actually getting the error message, saying it can't open the file at all.
Is the file in the same directory as the program?
Did you remember to have the ".txt" extension at the end of the file name (e.g. "scores.txt" instead of just "scores")?
If nothing else works, try typing out the full path to the text file (e.g. "C:\blah\stuff.txt" -- without the quotes, of course).
I got it now, I hadn't moved it into the directory. You were right though, it only read the first number until I put in your suggested command. I appreciate your help!
Topic archived. No new replies allowed.