0

I don't know how to properly connect user's input part and for-loop(which is incorrect) in this program. I was seeking for how to do this but didn't find anything useful.

Here are my code:

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

int get_propriate_int(string prompt);

int main(void)
{
    int i = get_propriate_int("Height: ");
    return i;
}

int get_propriate_int(string prompt)
{
    int n;
    do
    {
        n = get_int("%s", prompt);
    }    
    while (n < 1 || n > 8);
    return n;
}    

for (int k = 0; k < i; k++)
    {
        for (int j = 0; j < i; j++)
        {
            printf("#");
        }
        printf("\n");
    }
1

Your loops are out of main and the function of your program, I guess what you want to do is

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

int get_propriate_int(string prompt);

int main(void)
{
    int i = get_propriate_int("Height: ");
    for (int k = 0; k < i; k++)
    {
        for (int j = 0; j < i; j++)
        {
            printf("#");
        }
        printf("\n");
    }
    return i;
}

int get_propriate_int(string prompt)
{
    int n;
    do
    {
        n = get_int("%s", prompt);
    }    
    while (n < 1 || n > 8);
    return n;
} 

You should ask yourself why you want to return i in main, when we leave main the program returns control to the operating system, it would be normal to return 0 if everything goes well.

| improve this answer | |

You must log in to answer this question.

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