Hello ac829,
As knowclue said yes you can use a while loop and his example should work for what you want.
As I worked on the program I realized more of the problems that are there.
To start with it is a good practice and habit to initialize your variables when you define them. I had some problems with "tavge" and "finalscore" until I initialized the variables. Also that is when I discovered that in the "readTestScores" function "sum" and "score" were defined as "int"s when they should be a double. Then the numbers started to come out better.
Consider this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
void readTestScores(double & exam, double & tavge)
{
// Read the student's exam score
cout << endl << "ENTER EXAM SCORE: ";
cin >> exam;
// Print prompt and read the student's test scores
double sum{};
double score[7]{};
int i;
cout << endl << "ENTER ALL SEVEN TEST SCORES:\n\n";
//cin >> score[0] >> score[1] >> score[2] >> score[3] >> score[4] >> score[5] >> score[6];
for (i = 0; i < 7; i++)
{
std::cout << "Enter test score #" << i + 1 << ": ";
std::cin >> score[i];
sum = sum + score[i];
}
tavge = sum / 7.0; // <--- Changed to 7.0.
}
|
Line 14 is one way of doing input, but after I thought about the for loop works better because it prompts for each score. Notice the change I made in line 13. Working with this function is when I noticed that "sum" and "score" had the wrong type. You were storing a floating point number as an int thus loosing everything to the right of the decimal point, so in the end "tavge" had the wrong answer.
The next two functions I have not looked at closely because they appear to work.
In main again initialize your variables when defined. The simplest way is
int id{};
. The empty {} will set "id" to zero. Or if you need something different just put the number inside the {}.
The while loop is an endless loop with no way out at this time. For now I commented out the while loop for testing.
Other than the use of "endl" being in the wrong place main works well. I did add this line to main:
std::cout << std::fixed << std::showpoint << std::setprecision(2); // <--- Requires header file "<iomanip>".
This will affect the output of floating point numbers.
Just as a note the use of "endl" is a bit overused in your program. Many places in your "cout" statements you can use "\n" and use "endl" on the last line printed or use "std::flush()" if you feel the need.
With the proper corrections the program will work the way you want.
Hope that helps,
Andy