我的程序有什么错误?这是代码:
/*
* courseProject.c
*
* It is a simple database for record shop
* to track its iventory of CDs
*
* by Mahmoud Emam, 2012.
*/
#include<stdio.h>
main()
{
/*
* CDs infrormations
*/
char title[31], artist[31];
short int numberOfTracks; /* short to save memory */
int albumOrSingle; /* Boolean to check 1 for Album and 0 for Single */
float price;
printf("Hello, Welcome to Record Shop!\n\n");
/*
* Asking for CD details
*/
printf("Enter CD details\n\n");
printf("CD's Title: ");
scanf("%[^\n]", title);
fflush(stdin);
printf("CD's Artist: ");
scanf("%[^\n]", artist);
printf("Number of tracks: ");
scanf("%d", &numberOfTracks);
printf("Please press \"1\" for album, \"0\" for single: ");
scanf("%d", &albumOrSingle);
printf("CD's Price: ");
scanf("%f", &price);
/*
* Output CD details
*/
printf("\nCD details:\n");
printf("=============\n\n");
printf("CD's Title: <%s>\n", title);
printf("CD's Artist: <%s>\n", artist);
printf("Number of tracks: <%d>\n", numberOfTracks);
if (albumOrSingle)
printf("This is <Album>\n");
else
printf("This is <Single CD>\n");
printf("Its price = <%.2f>\n", price);
printf("=============\n\n");
/* Exit from program */
printf("Press any key to exit\n");
fflush(stdin);
getchar();
}
这是一个简单的程序,它从用户那里读取 CD 信息并在屏幕上再次输出详细信息。但是,该artist
变量始终为空。为什么?
我是printf("%s", artist);
在从用户那里读取它之后制作的,它可以正常工作,但是在程序结束时它不起作用。变量始终为空。