Saturday, May 22, 2010

C++ loop question?

do{


cout %26lt;%26lt; "Enter a value in the range of 5 to 10: " %26lt;%26lt; endl;


cin %26gt;%26gt; num2;


if(num2 %26lt;= 5 %26amp;%26amp; num2 %26gt;= 10)


cout %26lt;%26lt; "This value is not in the proper range please try again" %26lt;%26lt; endl;


}while(numValues2 %26gt; 1);








this is for an output that looks something like this





Enter a value in the range of 5 to 10: 4


This value is not in the proper range; please try again





Enter a value in the range of 5 to 10: 20


This value is not in the proper range; please try again





Enter a value in the range of 5 to 10: -6


This value is not in the proper range; please try again





Enter a value in the range of 5 to 10: 7











When I conpile and run this loop, it only goes through once. What seems to be the problem?

C++ loop question?
That code has some issues. Check your conditions. I can see you have a problem with the if condition.





if ( num2 %26lt;= 5 %26amp;%26amp; num2 %26gt;=10 ) will never succeed...





you cant have a number 5 or less and 10 or greater at the same time!
Reply:if(num2 %26lt;= 5 %26amp;%26amp; num2 %26gt;= 10)?





a value cannot be placed in this field. could you think of a number that would be less five and over 10?


try if(num2 %26lt;= 5 || num2 %26gt;= 10)





|| represent or statement.
Reply:I like this statement so much I am going to use it again.


I have never written a C++ program in my life.





There I feel so much better for that.





Your loop control compares a variable to 1.


}while(numValues2 %26gt; 1);





The variable numValues2 is not in your loop and so control for the loop is outside of the loop.





My guess is that numValues2 is set to zero so the loop will only be done once.





You already have the other corrections.


No comments:

Post a Comment