Write a program that uses a loop to ask the user to input a total of 5 test scores between 0 and 100 inclusive. Test each input and do not accept numbers out of range. Add each valid test score to a total and compute the average to 2 decimal places after exiting the loop. Display the average score.
This sounds so simple to do but I messed it up. I need to create a loop for inputting the test scores and use a loop to test the scores to ensure theyre between 0-100. I'm posting the program I made with my teachers comments on it.
#include<iostream>
#include<iomanip>
usingnamespace std;
int main()
{
//define variables
double num1, num2, num3, num4, num5;
int minscore = 0;
int maxscore = 100;
//first test value
//**** teacher input **** There should be an outer loop here for
//inputting the scores
cout << "Enter the first test score ";
cout << "\n(Value within the range ";
cout << minscore << " - " << maxscore << "): ";
cin >> num1;
//validate
//**** teacher input **** This is ok for validating the input
while ((num1 < 1) || (num1 > 100))
{
cout << "\nTest scores should be between " << minscore << " - " << maxscore;
cout << "\nEnter the first test score: ";
cin >> num1;
}
//second test value
// *** Dont need
cout << "\nEnter the second test score ";
cout << "\n(Value within the range ";
cout << minscore << " - " << maxscore << "): ";
cin >> num2;
//validate
while ((num2 < 1) || (num2 > 100))
{
cout << "\nTest scores should be between " << minscore << " - " << maxscore;
cout << "\nEnter the second test score: ";
cin >> num2;
}
//third test value
// *** Dont need
cout << "\nEnter the third test score ";
cout << "\n(Value within the range ";
cout << minscore << " - " << maxscore << "): ";
cin >> num3;
//validate
while ((num3 < 1) || (num3 > 100))
{
cout << "\nTest scores should be between " << minscore << " - " << maxscore;
cout << "\nEnter the third test score: ";
cin >> num3;
}
//fourth test value
// *** Dont need
cout << "\nEnter the fourth test score ";
cout << "\n(Value within the range ";
cout << minscore << " - " << maxscore << "): ";
cin >> num4;
//validate
while ((num4 < 1) || (num4 > 100))
{
cout << "\nTest scores should be between " << minscore << " - " << maxscore;
cout << "\nEnter the fourth test score: ";
cin >> num4;
}
//fifth test value
// *** Dont need
cout << "\nEnter the fifth test score ";
cout << "\n(Value within the range ";
cout << minscore << " - " << maxscore << "): ";
cin >> num5;
//validate
while ((num5 < 1) || (num5 > 100))
{
cout << "\nTest scores should be between " << minscore << " - " << maxscore;
cout << "\nEnter the fifth test score: ";
cin >> num5;
}
// *** Above not needed, once done with validation add to total
//finding average
double addedscore = num1 + num2 + num3 + num4 + num5; // *** End outer loop
double avgscore = addedscore/5;
//set precision
cout << fixed << showpoint << setprecision(2);
//display average score
cout << "\nThe average score of the inputted test scores is: " << avgscore << "%. ";
return 0;
}
I wrote this code without knowing that I had to write a loop for the inputs. So it is able to be changed.
Yes I know what functions are and how to use them on a lower understanding, but I am not allowed to use them because this is from a chapter before learning of functions. I'm not allowed to use more advanced code than what was part of this chapter and those prior.
I dont understand what he means I need an outer loop for the inputs.
I need to create a loop for inputting the test scores and use a loop to test the scores to ensure theyre between 0-100.
Are you sure about two loops?
To me it looks like:
* Get 5 good values
* Compute something
Add each valid test score to a total and compute the average to 2 decimal places after exiting the loop.
No need to actually store the values. Only the total counts.
double total = 0.0
size_t count = 0
WHILE we don't have 5 good values
read value
IF value is good
THEN add value to total and increment count
use total
I'm not really sure what to look at, I'm still relatively new to c++. I understand you used a function within it but I cant use a function for this program I am trying to figure out. This chapter is only basic things and this chapter focuses on loops. I just dont fully understand the difference between some loops. And I understand the idea of nested loops but it eventually gets way too confusing for me to follow.
#include <iostream>
usingnamespace std ;
int main ()
{
int marks[5] ;
double sum=0 , average=0 ;
for (int i=0 ; i<5 ; i++)
{
cout << "Enter Marks of Student " << i+1 << " : " ;
cin >> marks[i] ;
if (marks[i]>0 && marks[i]<=100)
{
sum=sum+marks[i] ;
}
else
{
cout << "Invalid Marks Entered !!!" << endl ;
i-- ;
}
}
average=sum/5 ;
cout << endl << "Average : " << average ;
}
And heres a sample output :
Enter Marks of Student 1 : 23
Enter Marks of Student 2 : 46
Enter Marks of Student 3 : 0
Invalid Marks Entered !!!
Enter Marks of Student 3 : -5
Invalid Marks Entered !!!
Enter Marks of Student 3 : 24
Enter Marks of Student 4 : 345
Invalid Marks Entered !!!
Enter Marks of Student 4 : 23
Enter Marks of Student 5 : 103
Invalid Marks Entered !!!
Enter Marks of Student 5 : 100
Average : 43.2
#include<iostream>
#include<iomanip>
usingnamespace std;
int main()
{
double total, average;
for(int counter = 0; counter < 5; counter++)
{
int num = 0;
cout << "Enter a test score (1-100): " << endl;
cin >> num;
if(num > 0 && num <=100)
{
total +=num;
}
else
{
cout << "Invalid test score" << endl;
counter--;
}
}
average = total / 5;
cout << fixed << setprecision(2);
cout << "The average of all the test scores is " << average << "% " << endl;
return 0;
}