Hello xcxave,
You are welcome.
Now after testing your program here are some of the things I found that I did not see at first.
Your header files:
1 2 3 4 5 6
|
#include <iostream>
#include <limits>
//#include <string> // <--- Never used in this program.
//using namespace std; // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
|
Actually you could replace "string" with 'limits" and save some work.
Your prototypes I changed to this:
1 2 3 4
|
void readScores(double& score1, double& score2, double& score3);
void getAverage(const double score1, const double score2, const double score3, double& average);
void getGrade(const double average, char& grade);
void print(const double score1, const double score2, const double score3, const double average, const char grade);
|
The "const" is nor required, but it does mean that the function can not change the value of the variable. You could think of it more as a safe guard for your variables.
In "main"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
double score1{}, score2{}, score3{}, average{};
char grade{};
readScores(score1, score2, score3); // call the function please
getAverage(score1, score2, score3, average);
getGrade(average, grade);
print(score1, score2, score3, average, grade);
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0;
|
On line 8 I added the "grade" to the parameters and passed it by reference so that the variable can have a value by the time you call the "print" function.
Also "Score1" is a much better name than "s1" At least it describes better what it is fore. Evan with the "1", "2" and "3" you should avoid a single letter variable name. Should you return to this program in the future you do not have to guess or take time to figure out what the single letter variable name is. And when your programs get bigger it helps to keep everything in straight and easier to understand.
The last bit of "main" is a replacement for "system("pause");" which you should avoid using. It can leave your program vulnerable to attack.
This function I changed around a little:
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
|
void readScores(double& score1, double& score2, double& score3)
{
std::cout << "Please input three grades: " << std::endl;
std::cout << " Score 1: ";
std::cin >> score1;
while (score1 < 0 || score1 > 100)
{
std::cout << "The grade must be from 0 to 100. Please re-enter your grade: ";
std::cin >> score1;
//break;
}
std::cout << " Score 2: ";
std::cin >> score2;
while (score2 < 0 || score2 > 100)
{
std::cout << "The grade must be from 0 to 100. Please re-enter your grade: " << std::endl;
std::cin >> score2;
//break;
}
std::cout << " Score 3: ";
std::cin >> score3;
while (score3 < 0 || score3 > 100)
{
std::cout << "The grade must be from 0 to 100. Please re-enter your grade: " << std::endl;
std::cin >> score3;
//break;
}
}
|
I broke up the input into three separate sections, at least to help you understand how it works. In the first while loop I removed the "endl" which puts the input on the same line as the prompt. Maybe its just me, but I think it works out better this way. I left the other two for you to change if you want.
For the while loops: just checking for "< 0" does have its problems. Any number greater than (-1), say 200 or 1000, would be considered a good score. Not what you want. Also the "break" statement is not needed. If I wer to enter (-1) and enter the while and then enter (-1) again it would accept that as a good score. The "||" keeps it in the range of (0) to (100). Only when a score is within this range does the while loop fail and you go to the next entry.
It is also possible to put the while loop in a function and call the function to check if the entry is valid.
In the function "getAveraage()" to find the average you add the scores not multiply them. Otherwise the function is fine. If you pass "average" by reference there is no need to return anything and what you originally wrote is wrong to begin with.
Here is the first part of this function:
1 2 3 4 5 6 7 8 9 10 11 12
|
void getGrade(double average, char& grade)
{
//cout << "your average please? " << endl;
//cin >> average;
if (90 <= average && average <= 100)
{
//cout << "Letter Grade is: A " << endl;
grade = 'A';
}
else if (80 <= average && average < 90)
|
Lines 3 and 4 I am not sure what you had in mind here, but you are asking for the average that the user would have to figure out even though it is a value passed to the function that has already been calculated.
What you have for your if conditions may look good on paper, but does not work a C++ code. What I have done here work better. Which is not to say that a different compiler may allow what you did to work, but I d not think it would make any difference.
The "cout" statements are the wrong place to output the letter grade unless you put it there for testing. Setting "grade" is what is needed here.
I adjusted the "print" function like this:
1 2 3 4 5
|
std::cout << "\n First Grade: " << score1 << std::endl;
std::cout << " Second Grade: " << score2 << std::endl;
std::cout << " Third Grade: " << score3 << std::endl;
std::cout << "\n Average Score: " << average << std::endl;
std::cout << "\n Letter Grade: " << grade << std::endl;
|
Part of this is a personal choice of mine. Starting the output one line off the top edge of the screen and one character from the left edge makes it easier to read. The other part comes from the years I have been working with programs and the use of the "\n"s and spaces just make for a nicer output.
Other than the syntax problems the rest is just a suggestion that you can take or leave as you choose.
Hope that helps,
Andy