loop output "-1.#IND"

First post and if this has been a question before I am sorry for not searching more.

As it stands it will let you input "scores" until you enter "-1" then it will average them. What I need help with is something that will prevent it from displaying an weird message when -1 is the first "score" you enter.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
double score = 0,count = 0,loop = 0,average;
		
while (score>-1)
{		
  loop +=  score;
  count++;
  cout << "Enter the scores one at a time \n";
  cout << "Enter -1 to signal that you are done\n";
  cin >> score;
}
  count = count -1;
  average = loop/ count;
  cout << "The average of those "<<count << " scores is: " << average <<endl;
  return 0;
} 



If -1 is entered as first score
Enter the scores one at a time
Enter -1 to signal that you are done
-1
The average of those 0 scores is: -1.#IND
Press any key to continue . . .


Normal output

Enter the scores one at a time
Enter -1 to signal that you are done
98
Enter the scores one at a time
Enter -1 to signal that you are done
75
Enter the scores one at a time
Enter -1 to signal that you are done
65
Enter the scores one at a time
Enter -1 to signal that you are done
85
Enter the scores one at a time
Enter -1 to signal that you are done
51
Enter the scores one at a time
Enter -1 to signal that you are done
-1
The average of those 5 scores is: 74.8
Press any key to continue . . .
well I ve seen that some people got this error when they try to divide by zero but it s not your porblem bu you can simply fix it with if function. if count is 0 then print 0 else print your average. i hope that it helps. :)
Thank you for you quick response! :)

I had to use 1 instead of 0 but again thank you very much. I spent a few hours stumped on this one

1
2
3
4
5
6
7
8
9
10
if (count == 1)					
	{
	cout << "You can not start with -1\n"; 
	}
else if (count > 1)
	{	
	count = count -1;
	average = loop/ count;
	cout << "The average of those "<<count << " scores is: " << average <<endl;
	}
Last edited on
you re welcome.. :)
Topic archived. No new replies allowed.