Why is the condition in my code not being met? I did clang mario.c and there was no error message. Then I did make mario and proceeded to start compiling my code yet every number from 1 to 8 leads the program to prompt me for a new valid integer.
2 Answers
You have set up the while condition incorrectly.
Your do/while loop is this:
do
{
...
} while (h>=1 || h<=8);
You have essentially created an infinite loop. Think about the test condition. It has to be true for the loop to repeat and false for the loop to terminate. So, since you want it to be false to continue on past the loop, tell me one number that is NOT either greater than 1 OR less than 8. Any number less than 1 is less than 8 so it's true, and any number greater than 8 is greater than 1, so that's true, and any number between 1 and 8 makes both statements true. So, no matter what, the test condition is ALWAYS true.
Once you understand that, you need to think about what you're really trying to do. You want to repeat the loop when the number is NOT between the two numbers. In other words, you want it to repeat if h is less than 1 OR if it is greater than 8, but not between them. Think very carefully about what I just said and make sure you understand it.
If it's between 1 and 8 inclusive, you want a false result so that the loop terminates. Think of it as a negative logic test.
Or, you could think about the test condition of what you're looking for, and negate it to move on. But don't try to figure this out until you understand what I said above.
If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)
-
Oh so the condition has to be met as false so that the loop does not continue printing and prints only what was input. Am I understanding correctly?– Rayray94May 8, 2020 at 22:23
-
That is exactly correct! Congratulations! ;-) Well, assuming that I understood what you said correctly. It will print the promopt "Height: " and will wait for an answer. If the answer is in the wrong range, it'll repeat. It has nothing to do with printing anything that follows the while statement.– Cliff BMay 8, 2020 at 22:24
-
Your whole statement is no being used. The for loop is not in the whole loop. You need to use {} and indent the statement that is next so the C knows it needs to run the code. Also to see what part of the codes is being run it is useful to printf success in your code. That way you know the line ran. There might be other issues but that definitely needs to be fixed.
-
Which line is the for loop that's not being used on? It looks like they all have brackets they need based on the examples in the lecture.– Rayray94May 8, 2020 at 21:45
-
-
-
See my answer. Also, while not the problem, Christopher is correct about cleaning up formatting of code for readability. Best practice is to put the while statement immediately after the closing curly brace on the same line. That makes it obvious, but doesn't affect the compile. Also, inserting strategic printf statements is useful too.– Cliff BMay 8, 2020 at 22:11