With no braces,{}, your while loop is only getting the statement directly underneath it. Start there. You don't want to return a true or false from main as your return type for main is an integer but also doing so will halt your program. Instead, after your if statement, set a variable (so initiate it as false at the top and then set it to be true once your condition occurs) that you have defined under int ctr; (really you could declare it anywhere outside of the while loop and within main).
Then use your if statement to set this boolean variable if the value is out of range. Then make another boolean for values over 500 and yet another for those under 10.
Don't be so discouraged, programming like anything else takes time to work on and improve.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
Your new function, which still has the { } problem jonsca pointed out, will return immediately after the first number is read in, depending on whether it meets the 10-500 test or not.
You should construct the loop to read in six times, and test if the new number is outside the 10-500 range. If so, set a variable to indicate so. After the loop, test that variable to see if there was an out of range number, returning true if so, false otherwise.
In the enhancements to the function, to indicate specifically if number less than 10 or greater than 500 was entered, you'll need to pass two boolean variables by reference to the function, and they will initially be set false, and be set true any time a qualifying value is entered.
I think the purpose of this exercise, besides a simple loop, is to see how you can pass one piece of information back by the return value, but to get multiple pieces of information you must use multiple parameters passed by reference.
The general model for you loop should be something like:
while ( loop still needs to run )
{
get input
if( input meets test )
set flag
}
if( flag set )
return true;
else
return false;
}
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
Your while loop is fine. I would ask yourself if 6 people came up to you and handed you a card with a number on it, how would you tell if the numbers fell into those categories. Hint, can a number be both less than 10 AND greater than 500? No. Those are the two things you are measuring. There are a number of different ways to test for it but just break down the logic, is this less than 10? No. Is it greater than 500? Yes. Ok, we don't care how many times this condition comes up, just switch the flag to true. Are there any values that are less than 10 OR greater than 500.... Just write it out the logic on a piece of paper and translate your words into the appropriate code.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581