Can't understand why this doesn't print nothing when I give the integer within the parameters...just keeps asking for an integer no matter what the user input is

1 Answer 1


a do..while loop executes the code in its body at least once then keeps executing it as long as the continuation condition is true.

this means that you want the continuation condition to be true (you want the loop to repeat) if the user enters a value that is not between 0 and 23 and false otherwise.

your continuation condition is: ((n < 1) && (n >= 23))

this condition is true if and only if n < 1 AND n >= 23 which doesn't really make sense and is always false — how can n be less than 1 and greater than or equal to 23 at the same time?

so the question is: when do you need the continuation condition to be true (when do you need your loop to repeat)?

also according to the specs, 0 is a valid height.

Edit #0: well, it obviously prompts twice because you have an extra call to GetInt on line 7. also you're never storing the result returned by the call to GetInt on line 12. and yes, you need the loop to repeat execution if the given input is less than 0 or greater than 23.

Edit #1: okay. let's try approaching things from the beginning. the problem is that we need to keep asking the user for input until he/she provides valid once.

when something involves repeating an operation again and again, we're typically talking about a loop. in C, we have 3 kinds of loops

  1. for loops.
  2. while loops.
  3. do..while loops.

for loops and while loops are similar in the sense that initially their conditions are checked first and if it's evaluated to true, their bodies are executed and the process repeats. if the condition is evaluated to false, they terminate.

of course for loops are different from while loop as for loops have headers. a for loop header consists of 3 main sections within parentheses after the keyword for. these sections are initialization (executed once at beginning of the very first iteration), condition (checked at the beginning of every condition), and update (where you update a variable in order to make the condition evaluate to false eventually and thus the loop terminates).

a do..while loop, unlike a for or a while loops, has its body executed at least once before the condition is evaluated(1). after that, the condition is evaluated and if it's true, the body is executed again and the process repeats. if the condition is evaluated to false the loop terminates.

usually for and while loops can be used interchangeably, but it's preferred it use for loops when the number of iteration is known and while loops when a sentinel value is involved.

since we're always gonna need to ask the user for input at least once, a do..while loop is a perfect choice in this case. the question now is: when do we want our loop to execute again? according to the specs, we need to keep asking the user for input until he provides an integer value in [0, 23].

so we need our loop to keep executing as long as the input is less than 0 or greater than 23. let's write some pseudocode

    prompt the user for input
    take input
while the inputted value is less than 0 or greater than 23

there is something missing here. we need to take input from the user only once at a time, so store the value that they're providing in each iteration in order to be able to test whether this value is valid in the condition.

so we need a variable (specifically of type int). we can't declare our variable inside body of do because it won't be seen by the while part (see Scope). so we should declare our variable before the do. let's call that height.

create an int named height

    // some code ...

another thing is that we need to get input from the user (specifically an integer input). luckily for us we have a function in the CS50 library named GetInt that does exactly that for us — it asks the user for integer input, ensuring it's an integer, and returning it.

as GetInt returns the user input, we need to store it in height. so we use the assignment operator (i.e., =). for example

int i;
i = GetInt();

assigns the value returned by GetInt to the variable named i. with that, we may now have a complete version of the pseudocode

create an int named height

    prompt the user for input
    take input and store it into height
while height is less than 0 or greater than 23

(1) if the condition of a for or a while loop is initially evaluated to false, the body is never executed.

  • Thanks Kareem, that makes sense. I'm also a little confused about the actual directions: do I want the pyramid to print out at whatever integer height the user gives? Will work on the code per your suggestion and see if I can resolve it, thanks for your help Commented Jun 21, 2015 at 2:49
  • and if 0 is a valid height no hashtags print? Commented Jun 21, 2015 at 2:51
  • @MarinFernald not really. according to the specs, the integer must be in [0, 23]. and yes, if the height is 0, your program just terminates.
    – kzidane
    Commented Jun 21, 2015 at 3:14
  • @MarinFernald let me know if you're still struggling to give you more hints.
    – kzidane
    Commented Jun 21, 2015 at 3:16
  • I def need more hints. When I change the && to || (or), and I run it, it prompts from an integer just once and again. It doesn't loop like I'm expecting it to. If the while is evaluating to true (which it seems it is, I was giving it an integer greater than 23), it should keep looping and prompting the user for another integer, correct? Commented Jun 21, 2015 at 4:03

You must log in to answer this question.

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