infinite loop problem!

I have an assignment in my C++ class and the code I wrote
keeps running as an infinite loop. I don't know what I did
wrong. Any help is appreciated.

adding a break statement didn't help and i don't completley understand the whole sentinel data thing. When I run the program and enter the infile and outfile names the program just doesn't stop, it doesn't come back with all the return data, and when i open the file it just has a list of 'D's that go on forever.

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
  #include <iostream>
#include <fstream>

using namespace std;

int main()
{
    char infile[50];
    char outfile[50];
    string name;
    double homework, exam, score;
    char grade;

    cout<<"Enter infile name: ";
    cin>>infile;
    cout<<"Enter outfile name: ";
    cin>>outfile;

    ifstream fin(infile);
    ofstream fout(outfile);

    fin>>name>>homework>>exam;

    while (!fin.eof())
    {
        score = 60 * (homework/200) + 40 * (exam/100);

        if (score>=75)
            grade='A';
        else if (score>=50)
            grade='B';
        else if (score>=25)
            grade='C';
        else
            grade='D';

        fout<<name<<" "<<grade<<endl;

        fin>>name>>homework>>exam;
    }

    fin.close();
    fout.close();

    return 0;
}
Last edited on
I would rather suggest you add another line of info to the text file and call it sentinel data then declare;

const string sentinel = "Stop"

and instead of while (!fin.eof()),

while(!(name == sentinel))
{
fin>>name>>homework>>exam;

score = 60 * (homework/200) + 40 * (exam/100);

if (score>=75)
grade='A';
else if (score>=50)
grade='B';
else if (score>=25)
grade='C';
else
grade='D';

fout<<name<<" "<<grade<<endl;
}
Alternatively you could use a "break" statement
Topic archived. No new replies allowed.