我有以下程序:
int main(int argc, char *argv[])
{
int a, b;
char c1, c2;
printf("Enter something: ");
scanf("%d",&a); // line 1
printf("Enter other something: ");
scanf("%d", &b); // line 2
printf("Enter a char: ");
scanf("%c",&c1); // line 3
printf("Enter another char: ");
scanf("%c", &c2); // line 4
printf("Done"); // line 5
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
正如我在C书中读到的那样,作者说scanf()在缓冲区中留下了一个新的行字符,因此,程序不会在第4行停止供用户输入数据,而是将新行字符存储在c2中并移至第5行.
是对的吗?
但是,这只发生在char数据类型中吗?因为我没有int在第1,2,3行中看到数据类型的这个问题.是不是?
请帮帮我。我想知道为什么会这样。
这段代码没有给出正确答案:
#include < stdio.h>
int main()
{
char c,ch;
int i;
printf("Welcome buddy!\n\nPlease input first character of your name: ");
scanf("%c",&c);
printf("\nPlease input first character of your lovers name: ");
scanf("%c",&ch);
printf("\nHow many children do you want? ");
scanf("%d",&i);
printf("\n\n%c loves %c and %c want %d children",c,ch,c,i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但这段代码给出了正确的答案。
#include < stdio.h>
int main()
{
char c,ch;
int i;
printf("Welcome buddy!\n\nPlease input first character of your name: ");
scanf(" %c",&c);
printf("\nPlease input first character of your lovers …Run Code Online (Sandbox Code Playgroud) 我最近开始使用C编程语言,大约两三天前,但是在使用do-while循环时我遇到了一些问题,这是我的程序中不能运行的一部分.
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
int main(void){
char another_game = 'Y';
scanf("%c", &another_game);
do{
printf("\nWould you like to continue(Y/N)?");
scanf("%c", &another_game);
}while(toupper(another_game) == 'Y');
return 0;
}
Run Code Online (Sandbox Code Playgroud)
只要用户输入'Y'或'y'提示这样做,循环就会继续运行,但我注意到在程序执行循环后第一次它只是再次显示问题然后循环中断.我尝试使用整数,让用户1在他希望继续0时输入或者当他想要退出时,它有效,所以我不明白为什么这个不会.谢谢,我将非常感谢您解决这个问题的所有帮助