3

When I run the following snippet it runs until the second question. It then puts the "Is the customer a student? (y/n) \n" and "What is the movies time? (in hours) \n" prompts together (no area to answer between them). If one takes any action from there on, the program stops working. What did I do wrong? (i'm pretty sure it's syntax related)

int A,B,C,D,age,time;
char edu, ddd;

printf ("What is the customer's age? \n");
scanf("%d", &age);

printf ("Is the customer a student? (y/n) \n");
scanf("%c", &edu);

printf ("What is the movies time? (in hours) \n");
scanf("%d", &time);

printf ("Is the movie 3-D? (y/n) \n");
scanf("%c", &ddd);
rectangletangle
  • 50,393
  • 94
  • 205
  • 275

6 Answers6

4

You probably need to eat the extra input from stdin after each scanf so it doesn't stick around in the buffer and cause scanf to receive the buffered data.

This is because the newline from hitting enter after the first text entry stays in the buffer and is a valid entry for the "%c" format - if you look at the value of "edu" you should find it's a newline character.

Matthew Iselin
  • 10,400
  • 4
  • 51
  • 62
4

When reading input using scanf, the input is read after the return key is pressed but the newline generated by the return key is not consumed by scanf, which means the next time you read from standard input there will be a newline ready to be read.

One way to avoid is to use fgets to read the input as a string and then extract what you want using sscanf.

Another way to consume the newline would be to scanf("%c%*c",&edu);. The %*c will read the newline from the buffer and discard it.

codaddict
  • 445,704
  • 82
  • 492
  • 529
2

You can add a space before the %c. This is necessary because unlike other conversion specifiers, it doesn't skip whitespace. So when the user enters something like "10\n" as the age, the first scanf reads up to the end of 10. Then, the %c reads the newline. The space tells scanf to skip all the current whitespace before reading a character.

printf ("What is the customer's age? \n");
scanf("%d", &age);

printf ("Is the customer a student? (y/n) \n");
scanf(" %c", &edu);

printf ("What is the movies time? (in hours) \n");
scanf("%d", &time);

printf ("Is the movie 3-D? (y/n) \n");
scanf(" %c", &ddd);
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
2

There are any problems with scanf and "%c", see eg: @jamesdlin. "time" is the name of a C-Standard-Lib function, better you use a different name, eg:

int A,B,C,D,age=0,timevar=0;
char edu=0, ddd=0, line[40];

printf ("What is the customer's age? \n");
if( fgets(line,40,stdin) && 1!=sscanf(line,"%d", &age) ) age=0;

printf ("Is the customer a student? (y/n) \n");
if( fgets(line,40,stdin) && 1!=sscanf(line,"%c", &edu) ) edu=0;

printf ("What is the movies time? (in hours) \n");
if( fgets(line,40,stdin) && 1!=sscanf(line,"%d", &timevar) ) timevar=0;

printf ("Is the movie 3-D? (y/n) \n");
if( fgets(line,40,stdin) && 1!=sscanf(line,"%c", &ddd) ) ddd=0;

At the end your vars have a defined content, 0 for an input-error, !=0 otherwise.

user411313
  • 3,930
  • 19
  • 16
1

Use the fflush(stdin);

statement to clear the buffer memory of stdin before reading any character data

or else it will read the enter key value of first scanf to the second scanf.

visha
  • 11
  • 1
0

I tried your program and it seems that after typing the age, when I press enter, it considers that as the input for the next scanf (i.e. for &edu) and similarly for third and fourth question. My solution could be naive but you could simply use a buffer scanf after each one to absorb the "Enter". Or simply do this

scanf(" %c", &variable);

(Any whitespace in the format string will make scanf absorb all further consecutive whitespace).

Kevin
  • 566
  • 2
  • 5
  • 13