0

This is my solution for the cs50 pset1 (less). Passes ok all the test cases except for the "9 then 2" case. I tried it in the IDE myself and after passing 9 and the any other integer the loop exits. Can not understand why....? Can please some one spill some light over the darkness of this matter?

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

int get_integer(void);

int main(void)
{    
    //assigning the function to an integer
    int n = get_integer();

    if (n >= 1 && n <= 8)
    {
        for (int i = 0; i <= n - 1; i++)
        {
            for (int j = i; j <= n - 2; j++)
            {
                printf(" ");
            }

            for (int g = 0; g <= i; g++)
            {
                printf("#");
            }

            printf("\n");
        }    
    }
    else
    {
        get_integer();
    }
    return 0;
}

int get_integer(void)
{        
    int integer = get_int("Please input a positive integer: ");    
    return integer;
}
1

The code is supposed to keep prompting for a new value until the user enters an acceptable number. This code doesn't do that. Instead, it will ask for a number, and if that number is out of range, it will skip down to the end of the program where it will ask for a new number a second time, but it won't do anything with it. When the second number is entered, whether it is valid or not, the program will then terminate without doing anything with it.

The code needs to keep looping until it gets a valid input, and then process that input.

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

| improve this answer | |
0

Thank you so much! I have refactored the code and now it work just fine.

[EDIT: Working code removed for honor code compliance. ]

| improve this answer | |

You must log in to answer this question.

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