首页 > 解决方案 > 在他离开“输入”选项后,我如何保持用户输入的值并让他从他完成的开始?

问题描述

我有一项必须做的运动。希望有人能理解我没有做的事情。

每当用户按下回车,输入少于 10 个整数,按下 q 退出选项然后返回,他所有的输入值都将消失。我想输入要留在那里的值,直到存储的总元素为 10。我该怎么做?

笔记!用户不应该退出应用程序(在这种情况下是 CMD),而是他们应该只能离开“进入”选项并仍然“存储”他们的输入值。

#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define L 10

void view(int a[])

     {
        int i;
        printf("\n Here are the currently stored values\n");  

        printf("[");
        for(i=0; i<L; i++)
            {
            printf(" %d ",a[i]);
            }
        printf("]");
     }

int enter(int a[], int n)
     {
         printf("\n Note! You can press any letter if you want to return to the main menu. \n");

         for(int i=0; i<L; i++)

            {

            int mesa;
            printf(" Enter measurement #%d (or q to quit): ", n+1);
            int check = scanf("%d",&mesa); 

            if(check)
                {
                    a[i] = mesa;
                    n++;
                }
            else
                { 
                    char temp;
                    scanf(" %c",&temp);
                    return n;
                }
            }
           return n;
     }

void compute(int a[], int n)

     {
            printf(" Computing...\n");

            //Min value
            {
                int i;
                int min = 10000;

                for (i = 0; i < L; i++)
                if(a[i] < min)
                min = a[i];

                printf("\n The min value is: %d \n", min);
            }

            //Max value
            {
                int i;
                int max = a[0];

                for (i = 0; i < L; i++) 
                if (a[i] > max) 
                max = a[i]; 

               printf(" The max value is: %d \n", max);
            }

            // Average & Normalization
            {
                float average;
                int i;
                int norm;
                int sum;

                for(i = 0; i < L; ++i)
                    {
                    sum = sum + a[i];
                    average = (float)sum / 10; //typecast - Ge en interger en tillfällig roll. På så sätt kan du säga åt programmet att du faktiskt vill ha float som svar och inte ett heltal som svar.
                    } 
                    printf(" Average value: %.2f \n", average);

                    printf(" Normalized array: [");
                for(i = 0; i < L; i++)
                    {
                    norm = a[i] - average; //average - every a[]
                    printf(" %d", (int)round(norm));
                    }
                    printf(" ]");
            }
     }

void reset(int a[])

    {
        printf(" You have chosen to reset the array. All the elements in the array will now be deleted. \n");

        //memset( a, 0, 10*sizeof(a)); <--- Kan ej användas då sizeof() funktionen läser av en variabel och inte hela arrayens storlek.
        memset( a, 0, 10*sizeof(int*));
    }

int main()

{
    char command;
    int a[L];

    printf(" Hello and welcome to this measurement tool. In this program you will be able to type in and analyze data.\n");
    printf(" In the section below, you can choose a letter v,e,c,r or q to do certain things. More information will be provided down below.\n");
    printf("\n v(View) - Displays currently stored values.");
    printf("\n e(Enter) - Allows you to store values. ");
    printf("\n c(Compute) - Displays the maxiumum, minimum, normalized and average value of those which are stored.");
    printf("\n r(Reset) - Deletes all stored values.");
    printf("\n q(Quit) - Exits the program. \n");
    printf("\n Please choose one of the commands above: ");

    do
    {
      int n = 0;
      scanf(" %c",&command);
      switch(command)
       {
            case 'v' : view(a);break;
            case 'e' : enter(a,n);break;
            case 'c' : compute(a,n);break;
            case 'r' : reset(a);break;
            default : printf("\n Please choose one of the options given.\n");break;
            case 'q' : 
                    {
                    printf(" Want to exit? Confirmation needed. Press q again to exit.");
                    scanf(" %c",&command);
                    }   
        }
            printf("\n VECRQ?: ");
    } while(command != 'q');
return 0;
}

标签: c

解决方案


main()中,将声明移到n循环外:

int n = 0;
do
{
    ...

n致电时更新enter()

n = enter(a,n);

enter函数中,从以下位置开始循环n

for(int i=n; i<L; i++)

推荐阅读