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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include <iostream>
#include <string>
using namespace std;
// 2. Function prototypes
void displayResults(string name, float avg); //satisfies the complier
int getTestScore();
float calcAvg(int t1, int t2, int t3);
bool isValid(int num, int min, int max);
int main()
{
string fullName;
int test1, test2, test3;
float average;
// get student full name
cout << "Student Name: ";
getline(cin, fullName);
// get 3 test scores
test1 = getTestScore(); // function call to value returning function
test2 = getTestScore();
test3 = getTestScore();
// average calculation
average = calcAvg( test1, test2, test3);
//assign a letter grade, using a char
//diplay heading, name and average
displayResults(fullName, average); // function call for a void function
system("pause");
return 0;
}
// 1. Function definiton
// ======================================================================
// displayResults outputs the heading and the results of the calculations
// ======================================================================
void displayResults(string name, float avg) // function header, scope
{
cout << "------------------------------------------------\n";
cout << "Norco College \n";
cout << " CIS-5: Intro to Programming with C++ \n";
cout << "Student Averages \n";
cout << "-------------------------------------------------\n";
cout << name << ": " << avg << endl;
}
//getTestScore prompts user and gets a single test score
int getTestScore()
{
int score; // scope of the operator
int min = 0, max = 100;
cout << "Enter a test score: ";
cin >> score;
while(isValid(score, min, max)) // While score is not valid
{
cout << "Try again. Enter a valid test score between 0-100: ";
}
return score; //returns a valid test score
}
//calcAvg totals scores and calculate the average
float calcAvg(int t1, int t2, int t3)
{
float avg;
avg = (t1 + t2 + t3) / 3.0;
return avg;
}
// isValid test for valid input and returns true or false
bool isValid(int num, int min, int max)
{
if (num < min || num > max)
return false;
else
return true;
}
|