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!
#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