Last active
February 8, 2017 02:44
-
-
Save austenstrine/5666654dbb5346dff00f36a65236401c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The purpose of this assignment was to create a program that would | |
//allow teachers to input three test scores, and get the average of | |
//those three. It specifically required the use of two functions | |
//(getTestScore and calcAverage)in the way I use them below. I | |
//added functionality to make it so that if the teacher put in a | |
//wrong value, such as a letter in place of a number for a number | |
//only variable (double or int) it would simply ask for input again | |
//until such a time as valid input is given. Also ensured that the | |
// >> operator would not freeze with invalid input. I also enabled | |
//teachers to do this for a certain number of students, and assign | |
//an "ID" string to each result for convenience. We haven't gotten | |
//to arrays in the book yet, but it would have been great to use | |
//an array to store/display the data. Also made it so that a | |
//negative number entered into a test score would act as a sentinel | |
//value for the entire program. I know there must be a more | |
//efficient way to do this, but I haven't gotten that far yet. | |
//Also note that I follow the whitespace convention of indenting | |
//loops and if statements in entirety, and again for the body. | |
//Edit: This version includes some alterations made after reading | |
//the chapter about void functions and passing variables by | |
//reference. | |
#include <iostream> | |
#include <iomanip> | |
//#include <typeinfo> | |
//#include <cmath> | |
//#include <string> | |
using namespace std; | |
double getTestScore (double scorePrev, int &sVal, int &tck); | |
double calcAverage (double scoreA, double scoreB, double scoreC, int track); | |
void noFail (); | |
int main() | |
{ | |
int sent = 0; | |
while (sent == 0) | |
{ | |
cout << endl << "Enter the number of grade sets you would like the average of (any negative integer to end): "; | |
cin >> sent; //define sentinel value for while | |
noFail(); | |
}//end while | |
while (sent > 0) | |
{ | |
string stuName = " "; | |
cout << endl << "Enter student ID: "; | |
getline (cin,stuName); //prompt for student name to tag average score/w | |
int tick = 1; //Establish ticker var to send to functions | |
//Reset ticker on concurrent runs of while loop | |
double score3 = 0.0; | |
double score2 = 0.0; | |
double score1 = getTestScore(0, sent, tick); | |
score2 = getTestScore(score1, sent, tick); | |
score3 = getTestScore(score2, sent, tick); | |
if (tick < 4)//ends while loop without displaying a negative average | |
{ | |
break; | |
}//end if | |
double avg = calcAverage (score1, score2, score3, tick); //Use calcAverage to assign average to avg | |
cout << " Average score for " << stuName << ": " << fixed << setprecision(1) << avg << endl; | |
sent -= 1; | |
while (sent == 0)//chance to edit sentine value | |
{ | |
cout << endl << "Enter the number of grade sets you would like the average of (any negative integer to end): "; | |
cin >> sent; //define sentinel value for while | |
noFail(); | |
}//end while | |
} //end while | |
return 0; | |
} | |
double getTestScore (double scorePrev, int &sValue, int &tck) | |
{ | |
if (scorePrev < 0) | |
{ | |
sValue = -1; | |
return -1; | |
}//end if | |
double score = 0.0; | |
while(score > -0.0001 && score < 0.0001) | |
{ | |
cout << endl << "Enter score #" << tck << ": "; | |
cin >> score; | |
noFail(); | |
}//end while | |
tck += 1; | |
return score; | |
} | |
double calcAverage (double scoreA, double scoreB, double scoreC, int track) | |
{ | |
return (scoreA+scoreB+scoreC)/(track-1); | |
} | |
void noFail () | |
{ | |
std::cin.clear(); | |
std::cin.ignore(65536,'\n'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment