C Program String input&output

2011-10-25 4:00 am
我不明白那裡出錯了, 只需要簡單輸入幾串字再 print out 出來便可以...
麻煩有誰知道錯在那裡呢? Thanks.

#include int main() { char e[10];
char f[50];
char g[50];
char h[5]; printf("Please Input name 1\n");
scanf("%s", &e); printf("Please Input name 2\n");
scanf("%s", &f); printf("Please Input name 3\n");
scanf("%s", &g); printf("Please Input name 4\n");
scanf("%s", &h); printf("name 1\t\t: %s\n", e);
printf("name 2\t: %s\n", f);
printf("name 3\t: %s\n", g);
printf("name 4\t\t: %s\n", h); /* pause the program */
getchar();
}

input:
john chan <-enter
may wong <-enter

output:
Please Input name 1
john chan
Please Input name 2
may wong
Please Input name 3
Please Input name 4
name 1 : john
name 2 : chan
name 3 : may
name 4 : wong

回答 (1)

2011-10-25 6:30 am
✔ 最佳答案
scanf只會讀連續字母
一遇到空白就停
要讀埋空格
用gets() 或 fgets()


收錄日期: 2021-04-13 18:19:38
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20111024000051KK00686

檢視 Wayback Machine 備份