Greetings,

I'm having a problem with this code below. I want to ask 3 questions and then gather userinput into char variables. When I run the program it asks the first question and then outputs the answer. Then it asks the second question, but does not allow the user to answer the question and moves on to the last question which it does allow user input. Is there a rule about the use of multiple scanf's? Here's a screen shot:

[IMG]file:///C:/DOCUME%7E1/smr10/LOCALS%7E1/Temp/moz-screenshot.jpg[/IMG][IMG]file:///C:/DOCUME%7E1/smr10/LOCALS%7E1/Temp/moz-screenshot-1.jpg[/IMG]

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char Light;
    char lane;
    char turn;
    char PlayAgain = 'Y';

     //while(PlayAgain == 'y')
    {
        //USER_INPUT Light Red?, Lane Right? Turn Yes?;
        printf("Is light red?  ");
        scanf("%c", &Light);
        printf("%c", Light);   //validate user input

        printf("Are you in the right lane?  ");
        scanf("%c", &lane);
        printf("%c", lane);    //validate user input

        printf("Are you making a turn?  ");
        scanf("%c", &turn);
        printf("%c", turn);     //validate user input

     }

try using %s in the scanf's

That worked, thanks Lost in Code!

>try using %s in the scanf's
No, don't. At least don't until you know how to safely use %s and change the input variables to arrays. Otherwise you'll invoke undefined behavior and create security holes/mysterious crashes.

>Is there a rule about the use of multiple scanf's?
There's one that applies to you, yes. If you're using one of the format specifiers that doesn't trim leading whitespace, you need to deal with the newline that's left over from the last call:

printf("Is light red? ");
scanf(" %c", &Light);
printf("%c", Light); //validate user input

printf("Are you in the right lane? ");
scanf(" %c", &lane);
printf("%c", lane); //validate user input

printf("Are you making a turn? ");
scanf(" %c", &turn);
printf("%c", turn); //validate user input

Notice the extra space in front of %c for the scanf format string. This tells scanf to read as many whitespace characters as it can find.

Greetings,

I'm having a problem with this code below.

So am I. See the scanf() series here...

try using %s in the scanf's

No, better not. Here's why.

>try using %s in the scanf's
No, don't. At least don't until you know how to safely use %s and change the input variables to arrays. Otherwise you'll invoke undefined behavior and create security holes/mysterious crashes.

See above link. Although %s can be made somewhat safe, it's better to use fgets() to read strings. It's harder to create problems and control your input.

hay use %s instead of %c then u get the output as like u stated

commented: hay, good effort, only 3+ years TOO LATE -4
commented: And it helps to read the other responses before saying something foolish. -4

hi Lost in Code,
I hope you understand why %s worked and not %c. This is because %c will only accept one character and not a string like %s would. In your case, if the user had put YES for the first question, only Y would appear in the printf and not YES. %s on the other hand takes the whole answer and thus the whole of it is printed out.
Another problem could have been pressing the SPACE key or ENTER key after giving the answer. If you want that to be able to happen, you could use

getchar()

to capture the SPACE or ENTER since they are taken as characters. An example:

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char Light;
    char lane;
    char turn;
    char PlayAgain = 'Y';
 
     
    {
        //USER_INPUT Light Red?, Lane Right? Turn Yes?;
        printf("Is light red?  ");
        scanf("%s", &Light);
        printf("%s\n", Light);   //validate user input
        getchar(); //To capture the new line character.
 
        printf("Are you in the right lane?  ");
        scanf("%s", &lane);
        printf("%s\n", lane);    //validate user input
        getchar(); //To capture the new line character.
 
        printf("Are you making a turn?  ");
        scanf("%s", &turn);
        printf("%s\n", turn);     //validate user input
    }

Also, you *might* want to do

fflush(stdin);

right before every single "scanf" that you have.

Regards.

commented: Bad advice, fflush(stdin); -> fflush is for OUTPUT streams -4

Also, you *might* want to do

fflush(stdin);

right before every single "scanf" that you have.

Regards.

You might, if you want to suffer from undefined behavior. fflush is not defined for input streams and is the incorrect way of discarding unextracted input. The correct way is a loop:

void clear_stream(FILE *in)
{
    int ch;

    do
        ch = getc(in);
    while (ch != '\n' && ch != EOF);
}

A complete solution is not possible without losing portability. The remaining problem is how to avoid blocking when the stream is already empty.

just put extra space infront of %c
like
scanf(" %c",&var);

commented: Read the thread first and try to learn (if you can) of the advise given already. -3
commented: thank a lot!!!! I stuck here like forever +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.