• Post Reply Bookmark Topic Watch Topic
  • New Topic

test score calculator while loop  RSS feed

 
Tianna Taylor-Albin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to make a calculator that asks a user how many test scores will be entered, prompt the user to enter a score for the amount of scores to be entered, add the scores, and average the scores. If the user enters and invalid score, one that is above 100, the program should display an error message and not add the entry to the total number of scores or the added scores. I've gotten it to do everything I need until an invalid score is entered. Can anyone help me figure out how to tell it that if the score is over 100, do not add it to the score count or total. Here is what I have so far:



Thanks for any help figuring this out.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone help me figure out how to tell it that if the score is over 100, do not add it to the score count or total. Here is what I have so far:

Your code already handles that situation, what it doesn't do is get the user to enter a valid score instead of the invalid one. The problem is you increment the loop counter whether the input is valid or not, you may want to move the increment so it only happens if the input is valid.
You may also want to look at what happens is someone enters a negative number or a number of scores that is less than 1.
 
Alex Armenteros
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've only seen two problems

a) What sir Docherty has said.


b) remember always that int / int will return an int, so the fraction part of the number is lost even if you "cast" it to double. To solve this make the average operation int /double, double/int or double/double


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!