請問一下,如何改能輸入陳述句而不是數字?用的是vs2019
#include<stdio.h>
int main()
{
int c;
while ((c = getchar()) != EOF)
if (c == '1')
printf("Hello,nice to meet you.");
else if (c == '2')
printf("My name is world.");
else if (c == '3')
printf("What's your name?");
else
putchar(c);
}
如果我將此程式改成這樣
#include<stdio.h>
int main()
{
int c;
while ((c = getchar()) != EOF)
if (c == 'hi')
printf("Hello,nice to meet you.");
else if (c == 'name')
printf("My name is world.");
else if (c == '3')
printf("What's your name?");
else
putchar(c);
}
那么輸出的將是
hi
hi
name
name
3
What's your name?
uj5u.com熱心網友回復:
chat c[256]; //用字串while(gets(c)!=NULL) { //接收字串
if(strcmp(c, “hi”)==0) //字串比較
printf(xxx)
...
}
uj5u.com熱心網友回復:
輸入陳述句的話,那就是字串處理了。字串處理可以參考:fgets, strcmp,strcpy等等函式else if (c == 'name')
這兒的name應該是字串,'name'是非法的,字串比較應該用strcmp
uj5u.com熱心網友回復:
getchar()只能獲取單個字符uj5u.com熱心網友回復:
scanf,strcmp轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/24056.html
標籤:C語言
下一篇:C++ 生成doc檔案兼容性問題
