Connecting Tech Pros Worldwide Help | Site Map

Ok, I have an if, and the user enters a value outside of the range.....

Newbie
 
Join Date: May 2009
Posts: 6
#1: 2 Weeks Ago
Ok, I have an if statement, and the user enters a value outside of the range....
say:

int sco; //Variable used for score.

if(sco< 0 || sco > 100)
{
cout<<"Error, You have to enter on the range 0-100\n";
// I want to be able to subtract the last input from the total
// without affect the other valid amounts.
// So, I was thinking in:

ave--;
scomax=-sco; // This is wrong because makes my score to zero, and
in the average variable, instead of subtract 1, subtract 2.


}

else

ave++;

scomax+=sco;
total=comax/ave; // This formula is right when is a valid input, but my negative formula is bad.



// Thank's 4 the comments.
best answer - posted by Banfa
If sco is out of range then don't include it into ave and scomax, then you don't have to try and subtract it which is hard (if not impossible).
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,148
#2: 2 Weeks Ago

re: Ok, I have an if, and the user enters a value outside of the range.....


If sco is out of range then don't include it into ave and scomax, then you don't have to try and subtract it which is hard (if not impossible).
Newbie
 
Join Date: May 2009
Posts: 6
#3: 2 Weeks Ago

re: Ok, I have an if, and the user enters a value outside of the range.....


Thank you Banfa,

I will try to assign different name variables, to see if that helps....and I will let sco, only for the valid input.
Familiar Sight
 
Join Date: Jan 2007
Posts: 188
#4: 2 Weeks Ago

re: Ok, I have an if, and the user enters a value outside of the range.....


Consider the following which rejects invalid input
while(sco is outside required range)
{
//do stuff here
}
Reply