0

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. enter image description here enter image description here

1

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. ;-)

| improve this answer | |
  • 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? – Rayray94 May 8 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 B May 8 at 22:24
  • Thank you so much! – Rayray94 May 8 at 22:26
-1

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.

| improve this answer | |
  • 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. – Rayray94 May 8 at 21:45
  • none of this is the problem. – Cliff B May 8 at 21:59
  • What is the problem? – Rayray94 May 8 at 22:02
  • 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 B May 8 at 22:11

You must log in to answer this question.

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