我试图简单地从用户读取输入并将CD记录存储到某些变量中。除了char
的第二个阵列artist
(不打印任何内容)外,所有变量的详细信息都已正确输出。我试图通过在scanf()
的每个格式化字符串的前面引入空格来处理额外的字符间距,但这并没有解决它。Printf和Scanf对于第二个字符串输入不能正常工作
void displayCDInfo(char* title, char* artist, short noOfTracks, int isAlbum, float price);
int main()
{
char title[61];
char artist[41];
short noOfTracks = 0;
int isAlbum = 0;
float price = 0.0;
char albumOrSingleResponse;
printf("Please enter the title: ");
scanf(" %s", title);
printf("Please enter the artist: ");
scanf(" %s", artist);
printf("Please enter the number of records: ");
scanf(" %d", &noOfTracks);
printf("Please enter whether or not the cd is an album/single (A or S): ");
scanf(" %c", &albumOrSingleResponse);
if(albumOrSingleResponse == 'A')
{
isAlbum = 1;
}
printf("Please enter the price of the CD: ");
scanf(" %f", &price);
displayCDInfo(title, artist, noOfTracks, isAlbum, price);
return 0;
}
void displayCDInfo(char* title, char* artist, short noOfTracks, int isAlbum, float price)
{
printf("\nThe title of the CD is %s", title);
printf("\nThe name of the artist is %s", artist);
printf("\nThe number of tracks on this CD are %d", noOfTracks);
printf("\nThe CD is an %s", (isAlbum == 1) ? "album" : "single");
printf("\nThe price of the cd is $%.2f", price);
}
也许使用'%s'是不是一个好主意 - 参见[scanf函数(http://linux.die.net/man/3/scanf) - 一个假设有些标题是多个单词 –
只有为第二个和第四个scanf语句提供空间 – Cherubim
%s即使只有一个单词也不工作,并且获取和放入正在演示相同的问题 –