我想做一個程序,第一次提示菜單的菜單選項從0到7開始菜單選項。在數組中讀取輸入,存儲-1值並在輸入-1時退出?
我有一個switch語句來檢查輸入的數字。這個程序還沒有完成我開始在「構建列表選項」,我是用戶輸入值以將其存儲到數組中,當用戶輸入「-1」時,我希望它將-1存儲在數組以及存儲用戶輸入的數字。
問題是:程序不會一直提示用戶輸入數字,直到輸入'-1'。相反,它會顯示下一個菜單選項並退出。
#include <stdio.h>
#include <stdlib.h>
#define SIZE 50
void main()
{
int i = 0;
int userinput[SIZE];
int x = 0;
do
{
printf("===DSCI0====\n");
printf("0. Build List\n");
printf("1. Display List Forward\n");
printf("2. Display List Backwards\n");
printf("3. Insert into list\n");
printf("4. Append into list\n");
printf("5. Obtain from List\n");
printf("6. Clear List\n");
printf("7. Quit\n");
printf("What is your menu option?\n");
scanf("%d", &i);
} while(i < 0 || i > 7);
switch(i)
{
case(0) :
for (x = 0; x < SIZE; x++);
{
printf("Enter a value for ther array (-1 to quit): \n");
scanf("%x", &userinput[x]);
if(userinput[x] == -1)
{
break;
}
}
case(1) : printf("Display List Fwd\n");
break;
case(2) : printf("Display List Bkwd\n");
break;
case(3) : printf("Insert into list\n");
break;
case(4) : printf("Append into list\n");
break;
case(5) : printf("Obtain from list\n");
break;
case(6) : printf("Clear List\n");
break;
case(7) : printf("Good-Bye\n");
exit(0);
break;
default : printf("Enter a value 0-7\n");
exit(0);
break;
}
}
更新:
我給出的解決方案在幫助我輸入數字到我的數組。但是,當我嘗試讀出它時,我沒有得到輸入值。
switch(i)
{
case(0) :
for (x = 0; x < SIZE; x++)
{
printf("Enter a value for ther array (-1 to quit): \n");
scanf("%x", &userinput[x]);
if(userinput[x] == -1)
{
break;
}
}
case(1) :
for (x = 0; x < SIZE; x++)
{
printf("[%i.] %i\n", i + 1, &userinput[x]);
if(userinput[x] == -1)
{
break;
}
我在這個例子輸出爲:
What is your menu option?
0
Enter a value for the array (-1 to quit):
0
Enter a value for the array (-1 to quit):
1
Enter a value for the array (-1 to quit):
2
Enter a value for the array (-1 to quit):
3
Enter a value for the array (-1 to quit):
-1
1. 698667216
1. 698667216
1. 698667216
1. 698667216
Display List Bkwd
可能是什麼造成的?
謝謝,我讓那部分工作完美!不過,我在嘗試打印出數組內容時遇到了一個錯誤 – sss34
@Stan我更新了我的答案。 – MikeCAT