Thread: Stuck on a loop - please help

  1. #1
    Registered User
    Join Date
    Aug 2019
    Posts
    5

    Stuck on a loop - please help

    Hi, I'm stuck on an assignment. I'm new to C and finding it quite intimidating and really stuck at present. I need the programme to continue running until a positive integer between 0 and including 8 has been input by the user, if another integer is entered ie -2 or 9 for example then the programme needs to prompt the user to enter Height again. Currently what the programme does when -2 is entered is return Height which is correct, but when 9 is entered, the programme ends, which is not what I want to happen.

    If anyone can point me in the right direction I would be grateful. Current code is:

    Code:
    #include <cs50.h>
    #include <stdio.h>
    
    
    int get_positive_int(string prompt);
    
    
    int main(void)
    {
        int Height = get_positive_int("Height: ");
        
        for (int i = 0; i < Height && Height < 9; i++)
        {
            for (int j = Height - i - 1; j > 0; j--) 
            {
                printf(" ");
            }
            for (int j = 0; j < i + 1; j++)
            {
                printf("#");
            }
            {
                printf("\n");
            }
        }
    }
    
    //prompt user to input positive integer
    int get_positive_int(string prompt)
    {
        int n;
        do
        {
            n = get_int("%s", prompt);
        }
        while (n < 1);
        return n;
    }
    // if less than 1  or greater than 8 then repeat previous step

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,153
    You need to adjust the condition of the do while loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2019
    Posts
    5
    Quote Originally Posted by laserlight View Post
    You need to adjust the condition of the do while loop.
    Thank you, you're amazing. I've done the below and it now works. I've been staring at this forever and tried so many different things with the condition and it hadn't worked before, but never thought of doing this until your message.

    Code:
     int Height;
        do
        {
            Height = get_int("%s", prompt);
        }
        while (Height <= 0 || Height > 8);
        return Height;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help.. newbie for loop..stuck with the loop..
    By jochen in forum C Programming
    Replies: 15
    Last Post: 10-01-2007, 12:31 AM
  2. Stuck in a FOR loop
    By xp5 in forum C Programming
    Replies: 2
    Last Post: 09-04-2007, 08:15 PM
  3. Stuck in a loop
    By liquidcourage1 in forum C++ Programming
    Replies: 20
    Last Post: 04-27-2006, 03:11 AM
  4. stuck in a loop
    By sweetly in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2003, 01:24 PM
  5. Stuck in a loop!.....Get me out of here!!
    By rabmaz in forum C Programming
    Replies: 3
    Last Post: 09-01-2002, 09:16 AM

Tags for this Thread