0

So I have a "do while" loop that asking my user to give me an integer between 1 and 8 and then print out that number of hash marks like so enter image description here

But when i type in my integer its just keeps promting me even though the statement is true, and the program keeps running. i have to ctrl c to get out.enter image description here

1 Answer 1

1

Think about the test condition:

while(n>0 && n<8)

It says that if the value is from 1 to 7 inclusive, repeat the loop. But, you really want to repeat the loop if the value of n doesn't meet the conditions.

In this situation, you actually want to test for the opposite of the values that you want.

BTW, what happens if n IS 8? Think about it. ;-)

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

3
  • cool thank you. its not prompting me over and over again now. it just ends the program without printing my hashmarks. My printf is within my curly braces so why isnt it showing me the hashes?
    – Aamon Van
    Commented Mar 6, 2021 at 1:14
  • You have a return statement following the first while loop. A return statement immediately terminates a program without processing any further code. It optionally returns a value to the calling code or to the operating system. In this case, it returns the value stored in n.
    – Cliff B
    Commented Mar 6, 2021 at 1:18
  • sweet! now I've got "#"
    – Aamon Van
    Commented Mar 6, 2021 at 1:34

You must log in to answer this question.

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