[英]Printf and Scanf not working properly for second string input
我试图简单地从用户那里读取输入并将CD记录存储到一些变量中。 正确打印了所有变量的所有变量详细信息,但artist
的第二个char
数组不打印任何内容。 我试图通过在scanf()
的每个格式化字符串的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);
}
删除空间: scanf("%s", ...)
并添加一个空格: scanf(" %c", ..)
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("%hu", &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;
}
因为当您在" %c"
之前添加空格时,它会占用先前输入所输入的换行符。
编辑:从这个 ,你可以使用"%hu"
用于打印short
,以避免任何警告。
这将起作用。 只需从主体顶部移动char数组声明的位置即可。
void displayCDInfo(char* title, char* artist, short noOfTracks, int isAlbum, float price);
int main()
{
short noOfTracks = 0;
int isAlbum = 0;
float price = 0.0;
char albumOrSingleResponse;
char title[61];
char artist[41];
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);
}
我想出了如何立即解决此问题,同时noOfTracks
保留为简短的内容。 由于我在"%d"
曲目数”变量上使用"%d"
作为scanf,因此我覆盖了分配给前一个变量的空间,即艺术家,因此为什么不显示它。 为了解决这个问题,我必须将int的int从"%d"
更改为"%hu"
。
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("%hu", &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 %hu", noOfTracks);
printf("\nThe CD is an %s", (isAlbum == 1) ? "album" : "single");
printf("\nThe price of the cd is $%.2f", price);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.