#include <stdio.h>
struct book
{
char name[1000];
int price;
};
int main()
{
struct book ct1[5];
int i;
for (i = 0; i < 5; i++)
{
printf("Please Enter %d Number Book Name: ",i+1);
gets(ct1[i].name);
printf("Price: ");
scanf("%d", &ct1[i].price);
}
for (i = 0; i < 5; i++)
{
printf("%d Nuumber Book's name and price : \n",i+1);
printf("%s = %d\n", ct1[i].name, ct1[i].price);
}
return 0;
}
我寫這段代碼取書名和價格並打印出來。取結構變量中的字符串和整數
像
輸入: 請輸入1號簿名稱:福爾摩斯
價格:100
...................
...................
輸出:
怒江MBER書的名稱和價格: 福爾摩斯= 100
.................
............... 。
,但它採取輸入這樣 請輸入1號簿名稱:福爾摩斯
價格:100
請進入2號簿名稱:價格:........
第一次是正確的,但從第二次出現問題。請幫幫我。
究竟「出錯」換行符?不要使用'gets'而是使用'fgets'! – Downvoter
'scanf(「%d」' - >'scanf(「%d%* c」' – BLUEPIXY
它停止接收字符串 –