kev*_*ski -4 c if-statement menu
我的菜单有问题.我从用户那里得到一个号码,但每当我得到一个号码时,无论我做什么,它都只做一个选项.我究竟做错了什么?
int main()
{
int array[SIZE];
int size = readNum();
fillArray(array, size);
char option = 'y';
do
{
int num = menu();
if(num == 1)
fillArray(array, size);
else if(num == 2)
{
int newSize = readNum();
fillArray(array, newSize);
}
else
{
sortArray(array);
}
}while(option == 'y');
return 0;
}//end main
int menu()
{
printf("1)Change the values of the array\n2)Change the size of the array and the values in the array\n3)Find and display the mean and median\nChoice: ");
int menuChoice = scanf("%i", &menuChoice);
return menuChoice;
}
Run Code Online (Sandbox Code Playgroud)
该scanf
函数返回它所做的数字或成功转换,而不是实际的转换值.它似乎总能成功地读取你的价值,所以将返回1
一次成功的转换.
要返回实际的用户选择,请不要通过调用分配给它:
int menuChoice:
scanf("%i", &menuChoice);
return menuChoice;
Run Code Online (Sandbox Code Playgroud)
int menuChoice = scanf("%i", &menuChoice);
Run Code Online (Sandbox Code Playgroud)
scanf
返回成功转换的次数,因此如果扫描成功,则用1覆盖转换后的值.