问题描述:

I want to use the following script in my c program.

the user will be able to input an IP. after that i want to be sure, the input was right and ask the user

char eingabe;

printf("Is that the right input? y/n: ");

scanf("%s",&eingabe);

printf("your input was: %s\n", eingabe);

if(eingabe == 'y') {

printf("you klicked y");

} else {

printf("you clicked no");

}

网友答案:

Change

scanf("%s",&eingabe);

to

scanf(" %c",&eingabe);

and

printf("your input was: %s\n", eingabe);

to

printf("your input was: %c\n", eingabe);

because %c is the right format specifier for a char. %s is the format specifier for a string. The space before %c in the scanf skips any number of whitespace characters(' ',\n etc) includeing none before scanning a character(%c).

相关阅读:
Top