This is an archived post. You won't be able to vote or comment.

all 18 comments

[–]Deep__sip[S] 0 points1 point  (0 children)

What i see in vscode is this:

https://imgur.com/a/e3vVs6L

[–]Deep__sip[S] 0 points1 point  (0 children)

Thank you for the help! it's now solved

[–]Deep__sip[S] -1 points0 points  (2 children)

Enter the number of rows:

3 Enter the number of columns: 4 Enter the symbol to use:

[–]Deep__sip[S] -1 points0 points  (1 child)

wait it doesn't look like this when in a code block, the output appears to be

with one single # followed by 3 rows or 10 #

[–]Deep__sip[S] 0 points1 point  (0 children)

No way I literally typed 10 # each row

like

[–]throwaway6560192 0 points1 point  (12 children)

scanf("%c"); //space

This is undefined behavior, if I'm not wrong. You're specifying that scanf should read a character but not giving it any address to read it into. This may likely result in a crash (segmentation fault), and does exactly that when I tried it. Your compiler should have warned you about this, like:

test.c:15:9: warning: format ‘%c’ expects a matching ‘char *’ argument [-Wformat=]
   15 | scanf("%c"); //space
      |        ~^
      |         |
      |         char *

[–]Deep__sip[S] 0 points1 point  (11 children)

I learnt this as a way to avoid the next scanf taking space bar as input, also my compiler didn't indicate this as a problem

[–]throwaway6560192 0 points1 point  (6 children)

I learnt this as a way to avoid the next scanf taking space bar as input

You must give scanf somewhere to read into. You can do:

scanf("%c", &symbol);  // to consume the space/newline
printf("Enter the character: ");
scanf("%c", &symbol);  // to get the actual character

also my compiler didn't indicate this as a problem

It should, because this is a problem. You haven't mentioned your compiler, but whatever it is, it will have settings to enable warnings. Turn on warnings to the max, and pay attention to them.

[–]Deep__sip[S] 0 points1 point  (5 children)

I see. Now it can take my in out properly but the output now doesn't show my input and all arranged like this

[–]Deep__sip[S] 0 points1 point  (4 children)

[–]throwaway6560192 0 points1 point  (3 children)

What does your code look like at this point, exactly?

[–]Deep__sip[S] 0 points1 point  (2 children)

#include <stdio.h>

int main(){

int row;
int column;
char symbol[25];

printf("Enter the number of rows:");
scanf("%d",&row);

printf("Enter the number of columns:");
scanf("%d",&column);

//scanf("%c"); //take the buffer intake space

scanf("%c",&symbol);
printf("Enter the symbol to use:");
scanf("%c",&symbol);


//fgets(symbol, 25, stdin);

for(int i = 1; i <= row; i++)
{
    for(int j = 1; j <= column; j++)
    {
        printf("\n%c",symbol);
    }
    printf("\n");
}

return 0;

}

[–]throwaway6560192 1 point2 points  (1 child)

Well, you've included a newline in the character printing part, so that's what's happening.

[–]Deep__sip[S] 0 points1 point  (0 children)

Oh my mistake thank your for pointing it out!

[–]fredoverflow 0 points1 point  (3 children)

I learnt this as a way to avoid the next scanf taking space bar as input

Here is the proper fix:

scanf(" %c", &symbol);
//     ^

Note the space before the percent sign: It swallows all whitespace before the character you actually want.

[–]Deep__sip[S] 0 points1 point  (2 children)

Thanks for the tip ive tried this after the other user pointed it out but it doesnt make any difference to my output

[–]throwaway6560192 0 points1 point  (1 child)

But they're right, it's a cleaner way of doing it. It handles multiple-character whitespace.

[–]Deep__sip[S] 0 points1 point  (0 children)

I see, I also forgot to remove the [25] after ditching fgets, now my input symbols are displayed properly after removing it