这是一个简单的菜单。提示用户选择介于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