0

I am trying to do the Mario problem with C. The issue is that I want the user to select a number from 1 to 8. So my boolean expression is (n < 9 && n > 0).

But I don't know why is not working. And without the do while loop I don't see any other way to complete the task. I am stuck here. Here is the code.

What I am doing wrong?

Thanks in advance.

#include <stdio.h>
#include <cs50.h>

int main(void)
{
    int n;
    do
    {
        n = get_int("Height: ");
    }
    while (n < 9);
    for (int i = 0 ; i <= n ; i++)
    ;
    {
        printf("#\n");
    }
}

enter image description here

2 Answers 2

3
  1. you want to keep asking the user about height if n is smaller than 1 or larger than 8 right ? check your code, the way you're writing it ,the program is gonna keep asking for height as long as n is smaller than nine ! ( which is not what you want).

therefore the solution is change "while (n < 9)" to "while (n < 1 || n > 8)"

1
-2

Your do-while loop has condition n < 9. Which means that it is continuously going to prompt the user for height until a value greater than 9 is not inputted. So to get desired results, your condition should be (n < 9 && n > 0).

Your next mistake is in the for loop. You have written an unnecessary semicolon between the for and the opening curly brace.

2
  • Nope. It will still keep looping.
    – Cliff B
    Jun 7, 2020 at 20:22
  • Thanks for your answer Palash! Jun 7, 2020 at 21:29

You must log in to answer this question.

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