Close but no cigar! lil help!!

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!

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
  #include <iostream>
using namespace 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 does trigger it. But only once, because it only runs once. Use a while-loop instead.

1
2
3
4
5
while(n < 1 || n > 100)
{
    cout << "Please enter a number between 1 and 100: ";
    cin >> n;
}


Do the same with the score.
Last edited on
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;
		}
Also move some lines around, or you'll get an incorrect result when someone enters numbers less than zero or greater than 100.

1
2
3
4
5
6
7
8
9
10
11
12
13
	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: ";
		}

		sum += 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.
Last edited on
Try entering -1 twice in a row
TarikNeaj, thank you for your diligence.

If you enter -1 twice is it still gives you "the sum is 0" "the average is -0"

How would I get "please enter a number between ...." to loop until valid input is given?
Last edited on
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?
Last edited on
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
#include <iostream>
using namespace 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;


}
Code looks good, you got a problem though. You need to initialize sum.

double sum = 0; // change it to this in the begining
Last edited on
Alas! Thank you for your help

Learning is Fun
Topic archived. No new replies allowed.