0
while(height >= 23 || height < 0)
    {
        printf("Retry: ");
        height = get_int();
    }

now if hieght is greater than 23 or less than 0 then only code inside should be executed here when i gave input as strings or charactors it still gave me a retry output (i mean the code inside while loop still worked)

cant figure out why this happening

1

Given the code, it appears that it asks the user for the height before this. The relevant code that precedes this, including the declaration of height might have a flaw, but I doubt it. For the retry to execute, height would have to have an invalid value going in.

More likely, it's possible that it is working correctly and you have misinterpreted the actions. I believe that the get_int() function itself will call a retry if non-numeric chars are entered. To test this, try changing the word "Retry:" in your code to "Try again:" and rerun your code. If it still returns with a "Retry:" prompt, it isn't your while loop code running, but rather, it is the get_int() function itself that is producing the Retry: prompt. If it does give a "Try again" prompt, then the problem lies elsewhere. If that's the case, can you please edit your question and add the code that precedes the while loop?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

0

Is not the correct way to check the height, if it is not established the value of height before entering the loop we can have an undesirable behavior of the program, in this case we are interested in a DO-WHILE loop, the loop executes at least one Time, if the height value does not meet the condition it is repeated again enter code here

1
  • MARS is right that it would be more efficient as a do/while loop. It would eliminate the need for two code blocks that essentially do the same thing - asking the user to enter a value. I would say though that both methods are "correct". In some cases like this (maybe not this particular case), one method is more efficiently written code, while the other may be more efficient at execution. Often, the latter is more important, so both analyses should be considered, as well as how important each of those considerations are. There are often many correct ways to code something.
    – Cliff B
    Feb 22 '17 at 23:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .