问题描述
我正在尝试制作一个程序,首先提示用户菜单的菜单选项从0到7开始。
我有一个switch语句来检查输入的数字。这个程序还没有完成我开始在构建列表选项中,我是用户输入值以将其存储到数组中,当用户输入-1时我希望它将-1存储在数组并显示一条说循环结束的printf消息。
我做了个案陈述,只显示用户输入的菜单选项。
更新:
I am trying to make a program that first prompts the user with a menu with menu options starting from 0 to 7.
I have a switch statement to check for what numbers are entered. This program is not finished I am starting on the "build list option" where I was the user to enter value to have it stored into an array, when the user enters a "-1" I want it to store the -1 in the array and display a printf message saying "loop ended".
I made case statements to just display the menu option the user entered, for now.
UPDATE:
The problem is the program does not keep prompting the user for ("Enter a value for the array (-1 to quit: \n") and does not quit and store -1 in the array when finished.
When I enter a '0' for my build menu option it prompts me for a value...when I enter a number such as a '1' it will display the menu option..instead of looping the "enter a value" statement until a -1 one is entered..
任何提示和建议都会很棒
Any tips and suggestions would be great
#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 the array (-1 to quit): \n");
scanf("%x", &userinput[x]);
if(userinput[x] == -1)
{
printf("loop ended\n");
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语句,乱用for循环和if语句。
What I have tried:
I tried changing the switch statement around, messed around with the for loops and if statements.
推荐答案
scanf("%d", &i);
但你打开 x
but you switch on x
switch(x)
这篇关于如何将输入存储在数组中,并在输入(-1)时停止并存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!