Simple Program.. Big Problem!

I'm writing a program that takes in input and formats it into a score as "Name Score".
Here is what needs to be done "Here's Moonglow's format. The text file is composed of words. If a word is a number, then that is a student's score on a question, so you add it to the student's exam score. If the word is not a number, but is the word "NAME", then the next word is the student's name (Moonglow only uses first names -- last names are corporate and impersonal). If the word is "AVERAGE", then you start reading numbers until you read a word that is not a number (or is the end of the file). You average all of those numbers and add that to the score. Since Moonglow is a little scatterbrained, sometimes a number does not follow "AVERAGE." In that case, you ignore the "AVERAGE". "

My problem is I've somehow entered into a infinite loop, and have spent hours trying to fix this. This is a VERY simple program, and I can't seem to get it to work!

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
 #include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <stdlib.h>
using namespace std;

int main(){
         string s, name = "test";
         string buffer;
         double  qScore, eScore, totalExam = 0, grade = 0, numExam = 0, finalGrade, avg = 0;
                    while (!cin.eof())
                    { if (cin >> qScore){
                        if (cin.fail()){
                            cin.clear();
                        }

                        else{
                        grade += qScore;
                    }
                        }
                    else if (cin >> s){
                        if (s == "NAME"){
                            cin >> s;
                            s = name;
                        }
                        else if (s == "AVERAGE"){
                            while (cin >> eScore){
                                numExam++;
                                totalExam += eScore;

                            }
                            cin.clear();
                        }
                    }
                    }
            
                        if (numExam == 0){ 
                            avg = 0;
                                        }
                         else {avg = (totalExam/numExam);
                                    }
                        finalGrade = avg + grade;
                        cout << name << " " << finalGrade << endl;
                         return 0;
                                            
} 
    // end of main  
I too get the infinite loop.

I think its because "if (cin>>qScore)"

when that fails the if() is never entered so cin.fail isnt detected and cin.clear isnt executed.

this worked better for me.

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
   while (!cin.eof())
    { 
        cin >> qScore;
        if (cin.fail())
        {
            cin.clear();
        }
        else
        {
            grade += qScore;
            cin >> s;
            if (s == "NAME")
            {
                cin >> s;
                s = name;
            }
            else if (s == "AVERAGE")
            {
                while (cin >> eScore)
                {
                    numExam++;
                    totalExam += eScore;
                }
                cin.clear();
            }
        }
    }
Last edited on
I think that's slightly different than what I want though, but chances are I could be wrong about that.

I want it to keep reading into qScore until it fails, and then read into s.
This is what I've got:

I still seem to be getting an infinite loop and am not sure why.

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
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <stdlib.h>
using namespace std;

int main(){
         string s, name = "";
         double  qScore, eScore, totalExam = 0, grade = 0, numExam = 0, finalGrade, avg = 0;
                    while (!cin.eof()){ //Keep reading until end of input
                        cin >> qScore;
                        if (cin.fail()){
                            cin.clear();
                        }
                        else
                        {
                            grade += qScore;
                            cin >> s;
                            if (s == "NAME")
                            {
                                cin >> s;
                                s = name;
                            }
                            else if (s == "AVERAGE"){
                                while (cin >> eScore)
                                {
                                    numExam++;
                                    totalExam += eScore;
                                }
                                cin.clear();
                            }
                        }
                    }
                if (numExam == 0) {avg = 0;}
                else {
                    avg = (totalExam / numExam);
                }
                
                finalGrade = (avg + grade);
                cout << name << " " << finalGrade << endl;
      
                }// End of main                        



instead of while (!cin.eof)
would while (cin >> s || cin >> qScore); work?
Hi heroicJ,
you have an
outer loop
while(!cin.eof())//while end-of-file character or sequence is NOT entered
do something: (Keep looping)....
and an inner loop
while (cin >> eScore)//while a double valid value is entered
do something(Keep looping):..

i.e.
outer while loop


./While
Enter number: 1
Number is: 1
Enter number: 3
Number is: 3
Enter number: 7
Number is: 7
Enter number: ^Z  <-- EOF command (ctrl+D Mac OS X)
[2]+  Stopped                 ./While
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//While.cpp
//##

#include <iostream>

using namespace std;


int main(){

int number;

while(!cin.eof()){
        cout<<"Enter number: ";
        cin>>number;
        cout<<"Number is: "<<number<<endl;

}//end while


return 0; //indicates success
}//end of main 


inner while


./While2
Enter number: 1
Enter number: 2
Enter number: 3
Enter number: 1
Enter number: Characters <-- not a double
Sum is: 7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//While2.cpp
//##

#include <iostream>

using namespace std;



int main(){


double num=0,sum=0;

cout<<"Enter number: ";
while(cin>>num){
        sum+=num;
        cout<<"Enter number: ";
}//end while

cout<<"Sum is: "<<sum<<endl;

return 0; //indicaes success
}//end of main 


i think that
this may help!

regards.


EDIT: EOF command on Mac OS X ctrl+D
Last edited on
Topic archived. No new replies allowed.