user3513350
user3513350

Reputation: 1

using printf( ) twice in a C program

I just started learning C and one of my assignment is asking me to ask user enter a character twice. Each time user enters a character, the program will give user a response. The problem I am having right now is that every time the user enters a character, the program stops to proceed to the next question. Can someone help me?

#include <stdio.h>

int main()
{      
    char reply,reply2;

    printf("Are you an engineering major (Y/N)? ");
    scanf("%c",&reply);

    if (reply == 'y' || reply =='Y')
    {
        printf("Hey, you're an engineering major");
    }
    if (reply == 'n' || reply == 'N')
    {
        printf("You are not an engineering major");
    }

    printf("Are you a freshman (Y/N)?");
    scanf("%c",&reply2);

    if (reply2 == 'y' || reply2 =='Y')
    {
        printf("Hey, you're a freshman");
    }
    if (reply2 == 'n' || reply2 == 'N')
    {
        printf("You are not a freshman");
    }

    return 0;
}

Upvotes: 0

Views: 266

Answers (4)

DarkDust
DarkDust

Reputation: 92424

Most likely the problem is a trap that a lot of newbies fall into: none of your printed strings have a newline. You need to add \n to the end of your strings, as in printf("You are not a freshman\n");

The reason is that typically stdout (the stream where printf writes its text to) is line buffered. This means that it buffers all text until it either sees a newline or is forced to flush its contents via fflush. Only then the text that was buffered up to this point appears on screen.

Edit: Your second problem is that the scanf format "%c" reads any character. (I'm not sure whether it's line buffered as well, but its behaviour looks a bit like it.) Your first call scanf("%c",&reply) reads the y or n character but leaves the newline from pressing enter still in the stream. Your second call scanf("%c",&reply2) then reads the newline which was still in the stream (you can see that by doing a printf("%d\n", reply2); which will print 10). To fix that, use the format string " %c" (notice the space!) which tells scanf to ignore whitespace.

Also, you should handle unexpected input:

if (reply == 'y' || reply =='Y')
{
    printf("Hey, you're an engineering major.\n");
}
else if (reply == 'n' || reply == 'N')
{
    printf("You are not an engineering major.\n");
}
else
{
    printf("I don't understand the answer '%c'.\n", reply);
}

(Exercise: implement that logic using switch instead of if/else if/else ;-)

Upvotes: 3

Spikatrix
Spikatrix

Reputation: 20242

You need to add \n at the end of your printfs to make your output more cleaner and understandable.Also add a space before the %c in the second scanf.This is done because scanf does not consume the \n character after you enter the first character.As the Enter key (\n) is also a character,it gets consumed by the second scanf.The space before the %c will discard all blanks like spaces.

Upvotes: 0

macfij
macfij

Reputation: 3209

Change

scanf("%c",&reply2);

to

scanf(" %c",&reply2);

This will tell scanf to skip all the whitespace characters and read the first non-whitespace char. Otherwise, there will be a newline character \n read onto the reply2 variable. See also this answer.

Upvotes: 0

Linus
Linus

Reputation: 1123

What's happening here is that you're only reading one character with the first scanf call, but in order to input this you would have to type y or n, and then press enter. The enter is registered as another character on the input stream, a newline, but because the first scanf call reads only one character its still in the stream when it is called for the second time. This leads to reply2 = '\n', skipping your if statements and terminating the program.

Upvotes: 0

Related Questions