C 输入字母,存储在变量中,将字母打印到命令行
我正在尝试制作一个非常基本的程序来比较两个数字。输入两个数字后,询问用户是否要比较这两个数字。如果“是”或“否”为“是”,我遇到的问题是程序似乎没有要求我输入,而是立即转到下一个打印语句。代码如下:C 输入字母,存储在变量中,将字母打印到命令行,c,visual-studio-2010,variables,user-input,windows-7-x64,C,Visual Studio 2010,Variables,User Input,Windows 7 X64,我正在尝试制作一个非常基本的程序来比较两个数字。输入两个数字后,询问用户是否要比较这两个数字。如果“是”或“否”为“是”,我遇到的问题是程序似乎没有要求我输入,而是立即转到下一个打印语句。代码如下: #include<stdio.h> #include<conio.h> #include<stdlib.h> int main(void){ int n1; int n2; char ch; printf("compare 2 n
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main(void){
int n1;
int n2;
char ch;
printf("compare 2 numbers, input a number\n");
scanf("%d", &n1);
printf("your first number is %d\n", n1);
printf("enter your second number to compare \n");
scanf("%d", &n2);
printf("your second number is %d\n", n2);
printf("do you want to compare these numbers? y/n \n");
//here is the problem. after entering y, the program closes.
//at this point I just want it to print the letter it was given by the user.
scanf("%c", &ch);
//the print statement that is supposed to print the letter the user inputs
printf("%c is a vowel.\n", ch);
getch();
return 0;
}
#包括
#包括
#包括
内部主(空){
int-n1;
int n2;
char ch;
printf(“比较两个数字,输入一个数字”);
scanf(“%d”&n1);
printf(“您的第一个号码是%d\n”,n1);
printf(“输入要比较的第二个数字\n”);
扫描频率(“%d”和“n2”);
printf(“您的第二个号码是%d\n”,n2);
printf(“是否要比较这些数字?是/否\n”);
//问题是,输入y后,程序关闭。
//在这一点上,我只想它打印的信,它是由用户给出的。
scanf(“%c”和“ch”);
//打印语句,用于打印用户输入的字母
printf(“%c是元音。\n”,ch);
getch();
返回0;
}
//我将此代码用作正确运行的参考
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character\n");
scanf("%c", &ch);
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I'
|| ch =='o' || ch=='O' || ch == 'u' || ch == 'U')
printf("%c is a vowel.\n", ch);
else
printf("%c is not a vowel.\n", ch);
return 0;
}
#包括
int main()
{
char ch;
printf(“输入字符\n”);
scanf(“%c”和“ch”);
如果(ch='a'| | ch='a'| | ch='e'| | ch='e'| | ch='i'| | ch='i'
||ch='o'| | ch='o'| | ch='u'| | ch='u')
printf(“%c是元音。\n”,ch);
其他的
printf(“%c不是元音。\n”,ch);
返回0;
}
当您这样做时
printf("enter your second number to compare \n");
scanf("%d", &n2);
您将输入第二个数字,然后按enter
。此ENTER(\n)
仍在缓冲区中。
scanf
函数自动删除空白<当您使用%c
时,code>scanf()会在缓冲区中保留新行字符(%c
是例外,它们不会删除空格
)
scanf(“%c”和&ch)代码>
而不是这个用途
scanf("\n%c", &ch);
当你这样做的时候
printf("enter your second number to compare \n");
scanf("%d", &n2);
您将输入第二个数字,然后按enter
。此ENTER(\n)
仍在缓冲区中。
scanf
函数自动删除空白<当您使用%c
时,code>scanf()
会在缓冲区中保留新行字符(%c
是例外,它们不会删除空格
)
scanf(“%c”和&ch)代码>
而不是这个用途
scanf("\n%c", &ch);
当您使用%c、空格和“转义字符”作为有效字符时,它将存储在ch中。在前两次扫描中,您使用%d不会出错
在第二个程序中,调用scanf(“%c”、&ch);首先。此问题不会出现。如果再次调用,也可能出现相同的问题。当您使用%c、空格和“转义字符”作为有效字符时,它将存储在ch中。在前两次扫描中,您使用%d不会出错
在第二个程序中,调用scanf(“%c”、&ch);首先。此问题不会出现。如果再次调用,也可能出现相同的问题。此问题的原因是上一次按Enter键后的scanf
留下的换行符\n
。此\n
留给下一次调用scanf
要避免此问题,您需要在scanf
中的%c
说明符之前放置一个空格
scanf(" %c", &C);
...
scanf(" %c", &B);
...
scanf(" %c", &X);
%c
前面的空格可以吃掉任意数量的换行符
或
您可以使用scanf来读取单个字符,而无需将其分配给以下内容:
scanf( "%[^\n]%*c", &C ) ;
%[^\n]
告诉scanf读取每个不是'\n'
的字符。这将'\n'
字符保留在输入缓冲区中,然后*(分配抑制)
将使用单个字符('\n')
),但不会将其分配给任何对象。此问题的原因是按Enter键后上一个scanf
留下的换行字符\n
。此\n
留给下一次调用scanf
要避免此问题,您需要在scanf
中的%c
说明符之前放置一个空格
scanf(" %c", &C);
...
scanf(" %c", &B);
...
scanf(" %c", &X);
%c
前面的空格可以吃掉任意数量的换行符
或
您可以使用scanf来读取单个字符,而无需将其分配给以下内容:
scanf( "%[^\n]%*c", &C ) ;
%[^\n]
告诉scanf读取每个不是'\n'
的字符。这将'\n'
字符保留在输入缓冲区中,然后*(赋值抑制)
将使用单个字符('\n')
),但不会将其赋值给任何对象