1

I'm currently trying to write a C program using the NetBeans IDE.The goal is to take input from the user on the amount of sticks they want drawn on the screen where the Letter "I' is a stick as noted in the for loops. The thing is, I use scanf to get the user input for each row , but the program executes and runs but never actually seems to reach the for loop, since it doesn't do any of the print statements within it. My IDE doesn't print any of my print statements in the for-loops, which is why I'm assuming it isn't reaching them.

I have tried simply assuming values for user input, instead of using scanf, but that also does not seem to reach the for loop. Any input on what I could do to fix this, would be much appreciated!

#include <stdio.h>
int main(void) {
    int length1, length2, length3;
    int inputRow1, inputRow2, inputRow3;
    printf("Please enter the number of Sticks you would like per row\n");
    scanf("%d%d%d", inputRow1, inputRow2, inputRow3);
    for (length1 = 0; length1 < inputRow1; length1++) {
        printf("I");
        for (length2 = 0; length2 < inputRow2; length2++) {
            printf("I");
            for (length3 = 0; length3 < inputRow3; length3++) {
                printf("I");
                return (0);
            }
        }
    }
}

1 Answer 1

5

While using scanf you should store the values in the variables using &.

Change

scanf("%d%d%d",inputRow1,inputRow2,inputRow3); 

to

scanf("%d%d%d",&inputRow1,&inputRow2,&inputRow3);

And your return statement should be before exiting from the main().


For example:

#include <stdio.h>

int main()
{
    int length1,length2,length3;
    int inputRow1,inputRow2,inputRow3;
    printf("Please enter the number of Sticks you would like per row\n");
    scanf("%d%d%d",&inputRow1,&inputRow2,&inputRow3);
    for(length1=0;length1<inputRow1; length1++)
    {
        printf("I");
        for(length2=0;length2<inputRow2;length2++)
        {
            printf("I");
            for(length3=0;length3<inputRow3;length3++)
            {
                printf("I");
            }
            printf("\n");
        }
        printf("\n");
    }
    printf("\n");
    return(0);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Cheers Mate! Almost face-palmed in forgetting the '&'.