-1

MY CODE:

#include<stdio.h>
char main()
{
    char g1,g2,g3;
    printf("Enter the grade of student 1: ");
    scanf("%c",&g1);
    printf("\nEnter the grade of student 2: ");
    scanf("%c",&g2);
    printf("\nEnter the grade of student 3: ");
    scanf("%c",&g3);
    printf("%c%c%c",g1,g2,g3);
   
    return 0;
}

OUTPUT:

Enter the grade of student 1: A

Enter the grade of student 2: Enter the grade of student 3:

//I am getting a line break and couldn't enter the value of student 2 and the cursor moves to student 3!!

  • 2
    Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). Try `scanf(" %c",&g2);` etc. – Weather Vane Dec 28 '20 at 15:45
  • 2
    BTW `char main()` is non-conforming, please use `int main(void)`. – Weather Vane Dec 28 '20 at 15:48
  • Tip: Think about storing multiple values of something in an array. Steer towards using loops rather than copy-pasting code. – tadman Dec 28 '20 at 15:48
  • 1
    Tip: Stop using `scanf` for everything. Use `fgets()` instead and do your own parsing. It's not hard! – tadman Dec 28 '20 at 15:50

4 Answers4

2

Try doing:

#include<stdio.h>
char main()
{
    char g1,g2,g3;
    printf("Enter the grade of student 1: ");
    scanf(" %c",&g1);
    printf("\nEnter the grade of student 2: ");
    scanf(" %c",&g2);
    printf("\nEnter the grade of student 3: ");
    scanf(" %c",&g3);
    printf("%c%c%c",g1,g2,g3);
   
    return 0;
}

Sorry, I'm new to this.

  • 3
    Congratulations on your first answer here! Might I suggest that you add some details about what you fixed or why it is right? It helps users understand what is going on, rather than just being served the answer. Just a suggestion! – Aman Dec 28 '20 at 16:02
  • 1
    to reinforce @Aman remark, an answer must not be just code, this is not a suggestion but a rule, edit your answer and explain the space before `%c` even it is already said in remarks – bruno Dec 28 '20 at 16:04
  • No dude. Still not working!!! – Vishnuvasan Dec 28 '20 at 16:06
  • 1
    @Vishnuvasan what is not working ? are you sure you did what is proposed ? And please never call someone *dude* except if you want to be moderated ... – bruno Dec 28 '20 at 16:07
  • Some explanation: most of the format specifiers for `scanf` automatically filter leading whitespace, but `%c` and `%[]` and `%n` do not. Adding a space in front of the `%` instructs `scanf` to filter leading whitespace here too. – Weather Vane Dec 28 '20 at 16:11
  • note you do not manage the special case of an EOF when reading the grades, *scanf* returns a value, this is not for nothing – bruno Dec 28 '20 at 16:13
  • 1
    Adding the space to the format string instructs scanf to discard any whitespace characters (any of space ' ', tab '\t', or newline '\n'). – MED LDN Dec 28 '20 at 16:46
0

Just give a space before %c in scanf to get desired input. The below is your own code where I've added space in the second and third scanf statements.

#include<stdio.h>
char main()
{
    char g1,g2,g3;
    printf("Enter the grade of student 1: ");
    scanf("%c",&g1);
    printf("\nEnter the grade of student 2: ");
    scanf(" %c",&g2);
    printf("\nEnter the grade of student 3: ");
    scanf(" %c",&g3);
    printf("%c%c%c",g1,g2,g3);
   
    return 0;
}

And well, the reason? If you don't, then the next scanf statement assumes the new line you create by pressing "Enter" as a new character, which is \n. So we add a space in scanf to let the statement know that space is not considered an input.

Deepak
  • 2,660
  • 2
  • 8
  • 23
  • what appends if the user enter spaces before A (for instance) ? – bruno Dec 28 '20 at 16:10
  • What do you mean by "A"? I assume you ask what happens when user enters more than 1 space.. In that case, nothing happens. The program waits until you enter a character. – Deepak Dec 28 '20 at 16:13
  • A is the first value enter in the example of the OP question . No, without a space before `%c`in the first *scanf* if the user start by space(s) you get a space for *g1* – bruno Dec 28 '20 at 16:15
  • Yeah in that case you're right. I addressed the issue @Vishnuvasan faced. Taking your case into consideration, that might also be helpful :) – Deepak Dec 28 '20 at 16:17
  • the rule is simple, *never* suppose a user input is valid but *always* check it – bruno Dec 28 '20 at 16:22
0

It is often easier to always read an entire line, like this:

#include <stdio.h>

void ReadLine(char result[], int resultLen)
{
    int ch, i;

    i = 0;
    ch = getchar();
    while ((ch != '\n') && (ch != EOF)) {
        if (i < resultLen - 1) {
            result[i] = ch;
            i++;
        }
        ch = getchar();
    }
    result[i] = '\0';
}


int main(void)
{
    char g1[2], g2[2] ,g3[2];
    
    printf("Enter the grade of student 1: ");
    ReadLine(g1, sizeof g1);
    printf("Enter the grade of student 2: ");
    ReadLine(g2, sizeof g2);
    printf("Enter the grade of student 3: ");
    ReadLine(g3, sizeof g3);
    printf("%s%s%s\n", g1, g2, g3);   
    return 0;
}
August Karlstrom
  • 10,773
  • 7
  • 38
  • 60
0

first Scanf will not read/flush the "\n" after reading the single char. this character on input buffer will make the 2nd and 3rd scanf to get execute and fail without actually prompting the user.

There are multiple ways to handle this.

  1. read complete line

    • use gets/fgets to rad complete inputline

    • then use scanf to read from buffer

      eg:

      fgets(buff, 255, stdin);

      sscanf(buff, "%c", &value);

  2. execute a char read after every scanf statement.

    • getch()
  3. Put a preceeding " " in front of every "%c" used in scanf.

    • this will skip the special char.
    • "scanf(" %c", &value);"
Nishad C M
  • 142
  • 6