我在microsoft visual c ++ 2010中编写了这个简单的c代码.
#include<stdio.h>
#include<conio.h>
void main()
{
char title[20], artist[30];
int numtrack, price;
char type;
printf("Enter the title of CD \n");
scanf("%s",title);
printf("\nName of the artist \n");
scanf("%s",artist);
printf("\nEnter the type of CD(enter a for album and s for single)\n");
scanf("%c",&type);
printf("\n Enter the number of tracks \n");
scanf("%d", &numtrack);
printf("\n Enter the price of the cd \n");
scanf("%d", &price);
printf("%s\n%s\n%c\n%d\n%d\n",title, artist, type, numtrack, price);
getch();
}
Run Code Online (Sandbox Code Playgroud)
它出来了
Enter the title of CD
ranjit
Name of the artist
mahanti
Enter the type of CD(enter a for album and s for single)
Enter the number of tracks
4
Enter the price of the cd
4
ranjit
mahanti
4
4
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么它不等待类型变量的输入?有人可以解释一下吗?提前致谢.
代替
scanf("%c",&type);
Run Code Online (Sandbox Code Playgroud)
你要
scanf(" %c",&type);
Run Code Online (Sandbox Code Playgroud)
否则,前一个字符串中的一个换行符将作为类型使用.