【问题标题】:Read Input In An Array, Store -1 Value, And Exit When -1 Is Entered?读取数组中的输入,存储-1值,输入-1时退出?
【发布时间】:2016-08-18 00:45:55
【问题描述】:

我正在尝试制作一个程序,它首先用一个菜单提示用户,菜单选项从 0 到 7 开始。

我有一个 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

这可能是什么原因造成的?

【问题讨论】:

    标签: c arrays debugging switch-statement computer-science


    【解决方案1】:

    去掉最后一个分号

    for (x = 0; x < SIZE; x++);
    

    为了将循环之后的块放入循环内,并让系统迭代该块。

    然后,在块后添加break;进行迭代,以免系统继续显示下一个菜单。

    另外请注意,您应该在托管环境中使用标准 int main(void) 而不是 void main(),这在 C89 中是非法的,在 C99 或更高版本中是由实现定义的,除非您有特殊原因使用非标准签名。

    回答更新的问题:

    printf("[%i.] %i\n", i + 1, &userinput[x]);
    

    将调用未定义的行为,因为将错误类型的数据传递给printf()%i 调用int,但&amp;userinput[x]int* 类型。使用不带&amp;userinput[x] 打印数组的内容。

    【讨论】:

    • 谢谢你,我让那部分完美地工作了!但是,我在尝试打印出数组内容时遇到了一个错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 2011-12-23
    • 1970-01-01
    • 2016-07-07
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多