這是一個簡單的菜單。提示用戶選擇介於0和3之間的數字。運行時,如果在提示符下輸入1,則會輸出「Hello2」,這是正確的。然而,在第9行,當它應該返回值1(數字輸入存儲爲「類型」變量,它返回0.它將返回0爲任何值輸入。有人可以告訴我,我哪裏錯了在這裏?謝謝爲什麼這個開關盒總是返回0?
#include <stdio.h>
int type;
int main(int argc)
{
int a = 7;
getInput();
printf("You selected: %d\n", type);
}
int getInput(type)
{
printf("\nPlease select an option:\n1)Create a record\n2)Display records\n
3)Update records\n4)Exit\n\n;");
scanf("%d", &type);
switch(type)
{
case 0:
printf("Hello\n");
break;
case 1:
printf("Hello2\n");
break;
case 2:
printf("Case3\n");
break;
case 3:
printf("Exit\n");
break;
default:
printf("\nERROR! Please select a valid number\n");
}
}
爲避免此錯誤,請使用函數原型。你的代碼在C99中是非法的(在使用它們之前至少要聲明哪些函數)。 – 2015-02-17 22:19:28
不要忽略'scanf()'的返回值。 – 2015-02-17 22:23:10
打開編譯器警告並注意它們。 – 5gon12eder 2015-02-17 22:23:22