Friday, July 31, 2009

C++ code, how to write a value returned function to force the user to input an integer test score between...?

A value returned function to force the user to input an integer test score between 0 and 100 inclusive and then return that valid score.

C++ code, how to write a value returned function to force the user to input an integer test score between...?
int testScore()


{


int score=-1;


while(score%26gt;100 || score%26lt;0)


{


cout%26lt;%26lt;"You must enter a number between 0 and 100"%26lt;%26lt;endl;


cin%26gt;%26gt;score;


}


return score;


}
Reply:this is what it would be in C, I don't know C++





int get_test_score (void)


{


int test_score;


puts("Enter a test score");


scanf(" %d", %26amp;test_score);


if ((test_score %26lt; 0) || (test_score %26gt; 100))


{


get_test_score();


{


else


{


return test_score;


}


}





the languages are similar, I wouldn't be surprised if this works


No comments:

Post a Comment