• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

输入存储在数组,并在输入-1时停止并存储

用户头像
it1352
帮助3

问题说明

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

正确答案

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



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

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

掌握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)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcbggih
系列文章
更多 icon
同类精品
更多 icon
继续加载