0

My code has 3 functions doing 3 different works. First one just prints "Hello!", second one takes an integer input from the user and returns its square and the third one takes a character input from the user and makes a triangular pattern out of it. But when I execute the code the first two functions work well and the third one doesn't. Although, when I execute the third function separately, it works well. What could be the problem? Here is the code :

#include <stdio.h>

void greet()
{
    printf("Hello !\n");
}

int square()
{
    int n;
    printf("Enter a number to be squared:\n");
    scanf("%d",&n);
    return n*n;
}

void pattern(char ch)
{
    printf("Enter a Character:\n");
    scanf("%c",&ch);
    for (int i=10;i>0;i--)
    {
        for (int j=0;j<i;j++)
        {
            printf("%c",ch);
        }
        printf("\n");
    }
}
int main()
{
    int num;
    char c;

    greet();

    num=square();
    printf("%d\n",num);

    pattern(c);

    return 0;
}
rioV8
  • 24,506
  • 3
  • 32
  • 49
Raja
  • 1
  • 1
  • 1
    Does this answer your question? [scanf Getting Skipped](https://stackoverflow.com/questions/14484431/scanf-getting-skipped) – kaylum Nov 03 '21 at 10:36
  • Try to change `scanf("%c",&ch);` to `scanf(" %c",&ch);`. Because the previous `scanf` in the `square` function leaves a newline character which the `%c` will match instead of the expected character input. See duplicate post for more details. – kaylum Nov 03 '21 at 10:38
  • 1
    OT: The `char ch` param in `pattern` is unnecessary as it is neither used as an input value nor to return an output value. Just declare it as a local variable in the function. – kaylum Nov 03 '21 at 10:39
  • @kaylum Thanks ,it worked. I read the duplicate post but I couldn't understand why it happened at the first place ? Can you please help me with it ? – Raja Nov 03 '21 at 10:55

1 Answers1

0

Because a user is hitting the "Enter" key to submit their character for the second function, that "Enter" character is being passed on to the third function's scanf, and as such will print a triangle of new-lines (or a column in this case, you probably got a large amount of whitespace printed after the program's execution).

You can fix it by including a space before the %c in the pattern func's scanf, like this:

void pattern(char ch) {
    printf("Enter a Character:\n");
    scanf(" %c",&ch);
    for (int i=10;i>0;i--)
    {
        for (int j=0;j<i;j++)
        {
            printf("%c",ch);
        }
        printf("\n");
    }
}
  • Thanks. Can you please tell me why does it happen ? – Raja Nov 03 '21 at 11:03
  • 1
    When you use scanf("%d") before, you're telling scanf to only accept ints. So, a user enters and int, and hits enter, so now you have an int and a newline char in the input buffer. Then, scanf consumes the int from the input buffer, leaving the newline char. When you call scanf("%c"), it can accept now accept chars, looks in the input buffer, and consumes the newline char that's already there. Putting a space in front of "%c" simply tells it to ignore any existing whitespace chars in the buffer, thereby ignoring the newline character that was entered before. – Jared Linklater Nov 03 '21 at 12:05