SO I'm close but not quite there. I'm having trouble with my if statements I believe. A number outside of the range doesn't trigger the cout << "Please enter a number between 1 and 100: " and "Please enter a score between 0-100: "; respectively. Also any pointers in regards to my code is greatly appreciated. TIA!
#include <iostream>
usingnamespace std;
int main()
{
int n;
int count;
int score;
double sum;
double avg;
cout << "How many test scores would you like to average (1-100)? ";
cin >> n;
if (n < 1 || n > 100)
{
cout << "Please enter a number between 1 and 100: ";
cin >> n;
}
for (count = 1; count <= n; count++)
{
cout << "Enter test score #" << count << ":";
cin >> score;
sum = sum + score;
if (score < 0 || score > 100)
{
cout << "Please enter a score between 0-100: ";
}
}
cout << endl;
cout << "The total of the test scores is: " << sum << endl;
avg = sum / n;
cout << "The average of the test scores is: " << avg << endl << endl;
system("pause");
return 0;
It still lets you proceed with an invalid input with this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
while (n < 1 || n > 100)
{
cout << "Please enter a number between 1 and 100: ";
cin >> n;
}
for (count = 1; count <= n; count++)
{
cout << "Enter test score #" << count << ":";
cin >> score;
sum = sum + score;
while (score < 0 || score > 100)
{
cout << "Please enter a score between 0-100: ";
cin >> score;
}
It still lets you proceed with an invalid input with this code:
It works perfectly fine for me. There is nothing in the code that indicates that it would proceed if you would input something bigger than 100 or less than 1. But if it for some reason doesnt work for you, you can try a different variation of it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
bool proceed = false;
do
{
if(n < 1 || n > 100)
{
cout << "Please enter a number between 1 and 100: ";
cin >> n;
}
else
{
proceed = true;
}
}while(proceed == false);
Edit: Entering -1 twice in a row still doesnt make it proceed.
I've given you 2 different pieces of code that does that exact thing you're asking for. If it still doesnt work then this is some black magic that I do not know about.
If you enter -1 twice is it still gives you "the sum is 0" "the average is -0"
That output doesnt even exist anywhere in your program. Can you repost your entire program again, with the code that Ive given you?
#include <iostream>
usingnamespace std;
int main()
{
int n;
int count;
int score;
double sum;
double avg;
cout << "How many test scores would you like to average (1-100)? ";
cin >> n;
while (n < 1 || n > 100)
{
cout << "Please enter a number between 1 and 100: ";
cin >> n;
}
for (count = 1; count <= n; count++)
{
cout << "Enter test score #" << count << ":";
cin >> score;
while (score < 0 || score > 100)
{
cout << "Please enter a score between 0-100: ";
cin >> score;
}
sum = sum + score;
}
cout << endl;
cout << "The total of the test scores is: " << sum << endl;
avg = sum / n;
cout << "The average of the test scores is: " << avg << endl << endl;
system("pause");
return 0;
}