我一直在努力做到這一點,如果作業,我已經嘗試以各種可能的方式放置 scanf 并且這不是問題,因為它拾取了“ ”,正如我在下面的 printf 上看到的那樣。誰能弄清楚為什么不?謝謝
float number1;
float number2;
float total;
char operator[1];
printf("Welcome to the calculator\n");
while(3>2)
{
printf("Pick a number\n");
scanf("%f", &number1);
printf("Que quieres hacer?\n");
scanf(" %c", &operator);
printf("You wrote %s\n", operator);
if(operator ==' ')
{
printf("This works!");
}
}
uj5u.com熱心網友回復:
這個代碼片段
scanf(" %c", &operator);
printf("You wrote %s\n", operator);
if(operator ==' ')
{
printf("This works!");
}
不正確,與變數operator的宣告方式無關。
如果變數operator被宣告為
char operator;
那么這個宣告
printf("You wrote %s\n", operator);
呼叫未定義的行為。
在這種情況下,您需要撰寫
printf("You wrote %c\n", operator);
如果變數operator被宣告為字符陣列,例如
char operator[N];
哪里N有一些價值,那么至少這個陳述
if(operator ==' ')
沒有意義,條件將始終評估為假。
在while回圈中注意那個而不是這個條件
while(3>2)
只是寫起來會更簡單和易讀
while ( 1 )
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/316336.html
