Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <vector>
- #include <stdlib.h>
- using namespace std;
- int main(){
- enum readInStates {Normal, Name, Average};
- readInStates state;
- string s, name;
- double qScore = 0, eScore = 0, totalExam = 0, numExam = 0, grade = 0, avg = 0, finalGrade = 0;
- bool calculateAverage;
- while (cin >> s){
- if (state == Normal){
- if (s == "NAME") {state = Name;}
- else if (s == "AVERAGE"){
- state = Average;
- //new average, so initialize values to 0
- numExam = 0;
- totalExam = 0;
- }
- else if (s[0] >= '0' && s[0] <= '9'){
- qScore = atof(s.c_str());
- grade += qScore;
- }
- }
- if (state == Name){
- name = s;
- state = Normal;
- }
- if (state == Average){
- calculateAverage = false;
- if (s == "NAME")
- {
- state = Name;
- calculateAverage = true;
- }
- else if (s[0] >= '0' && s[0] <= '9'){
- eScore = atof(s.c_str());
- numExam++;
- totalExam += eScore;
- }
- if (calculateAverage)
- {
- if (numExam > 0)
- {
- grade += (totalExam/numExam);
- }
- }
- }
- }
- cout << name << " " << grade << endl;
- return 0;
- }//end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement