Input not accurate

Jan 29, 2014 at 6:20pm
The output of this program is supposed to be "name" "grade". However when I run it I only get "grade". The name will never show up. I've tried using while (!cin.eof()) but I keep getting an infinite loop with that.

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
  #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 >> s || cin >> qScore){
            if (cin >> qScore){
                grade += qScore;
                cin.clear();
            }
            else if (cin >> s){
                if (s == "NAME"){
                    cin.clear();
                    cin >> s;
                    name = s;
                }
                else if (s == "AVERAGE"){
                    while (cin >> eScore){
                        numExam++;
                        totalExam += eScore;
                    }
                    cin.clear();
                }
            }
            cin.clear();
         }
                
         if (numExam == 0){
            avg = 0;
         }
        
         else {
            avg = (totalExam / numExam);
         }

         finalGrade = avg + grade; 
    
         cout << name << " " << finalGrade << endl;
    
                }// End of main
Jan 29, 2014 at 7:39pm
That is an unconventional way to handle input. What is the input format?
Jan 29, 2014 at 8:38pm
The input is a bunch of words. It could start with numbers, which should be added to "grade", or it could start with words. Words that aren't NAME, the person's name (only after NAME), and AVERAGE are to be ignored.
Topic archived. No new replies allowed.