我的程序中有什麼錯誤?這是代碼:爲什麼變量在程序的一部分中正確打印,但不是另一部分
/*
* 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);
,它工作正常,但它在程序結束時不起作用。變量總是空的。
可能對[codereview.SE](http://codereview.stackexchange.com)更好,這不是爲我的網站找到問題。 – 2012-07-25 16:40:12
謝謝:)尋求幫助 – 2012-07-25 16:41:35
不是'fflush(stdin);'UB? – 2012-07-25 16:41:53