### Function Help

Hi Everyone,

I've been working on my assignment for a few hours now but I'm a little lost on step 3 and 4. I really want to understand this better so I'd really prefer some tips in the right direction.

I'm not exactly sure on how to incorporate the Checkscore and Average function.

3. Check each input score is between (0-100). Prompt the enduser to reenter score if not within the range (value returning function with parameter)

 `` `` ``See next post for updated code.``

Last edited on
Just use an if, else if, statement to for number 3. For number 4 I'll give you a hint, a for loop could be useful.

 `` `` ``See next post for updated code.``
Last edited on
Capture the logic needed for 3 in its own function. That way you can reuse the validation logic every time you need to get a valid number from the user.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344`` ``````#include #include int readIntInRange(int lowEnd, int highEnd, std::string prompt) { //if someone calls this the wrong way, help them out! if(lowEnd > highEnd) { int tempForSwap = highEnd; highEnd = lowEnd; lowEnd = tempForSwap; } int userInput; //print out the prompt and keep looping until we get a valid input while ((std::cout << prompt) && (!(std::cin >> userInput) || userInput < lowEnd || userInput > highEnd)) { //Conditions for entering the loop //if the userInput can't be parsed into an int [ !(std::cin >> userInput) ] //userInput is less than the bottom of our range [ userInput < lowEnd ] //userInput is greater than the top of our range [ userInput > highEnd ] std::cout << "That's not a number between " << lowEnd << " and " << highEnd << ".\n"; std::cin.clear(); //clear the flags which would be set if the user didn't input an integer std::cin.sync(); //clear anything left in the buffer } std::cin.sync(); //clear anything left in the buffer return userInput; } int main() { int userInput1, userInput2; userInput1 = readIntInRange(1, 100, "Gimme a number between 1 and 100: "); userInput2 = readIntInRange(3, 8, "Please enter a number that is between 3 and 8: "); std::cout << "userInput1: " << userInput1 << "\n"; std::cout << "userInput2: " << userInput2 << "\n"; return 0; }``````

http://www.parashift.com/c++-faq/istream-and-ignore.html
Last edited on
Topic archived. No new replies allowed.