如何将输入存储在数组中,并在输入(-1)时停止并存储? [英] How to have input stored in an array, and stop and store when (-1) is entered?

查看:141
本文介绍了如何将输入存储在数组中,并在输入(-1)时停止并存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个程序,首先提示用户菜单的菜单选项从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.

推荐答案

您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你接近一个错误。



第一个错误:

你在中做菜单选择我

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

First bug:
you do your menu choice in i
scanf("%d", &i);



但你打开 x


but you switch on x

switch(x)


这篇关于如何将输入存储在数组中,并在输入(-1)时停止并存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆