c - 菜单功能

标签 c function

我有一个关于 C 语言函数的问题。我有一个菜单,可以插入调用函数的数字,但问题是我不想使用 for,我想每次按菜单中的选项 a 时都一一插入数字。我还想仅打印选择 b 选项时输入的数字。我只是不知道如何解决计数器问题。抱歉,如果有语法错误,英语不是我的母语。

#include<stdio.h>
//Functions
char menu();
void insert (int[],int);
void print (int[],int);

//******************************
//CUERPO
int main (){
    int lenght=5;
    int num [lenght];
    char option;

    while((option=menu())!='x'){

        switch (option){

        case 'a':
            insert(num,largo);
            break;

        case 'b':
            print (num,largo);
            break;
        }
    }
    system ("pause");  
    return 0;
}

/* Codes ************************************************************** */

char menu (){
    char option;
    printf("\nInsert an option :" );
    printf("\nA. insert  :" );
    printf("\nB. print :" );

    scanf("%c", &option);
    fflush (stdin);
    return option;
}

void insert (int a[], int lenght){ // Here i have the problem
    int x=0;  
    printf("\nInsert your number %d: ", x);
    scanf("%d", &a[x]);
    x++;
}

void print (int a[], int lenght){
    int y;
    for(y=0; y<largo; y++){
        printf("\nThe numer you have entered are %d: ", a[y]);
    }
}

最佳答案

您应该在主函数中声明“x”变量。

int x = 0;

在用户选择“插入”选项后添加 x 的增量

insert(num,largo);
x++;

最后将“插入”函数更改为接受“x”而不声明它。

void insert (int a[], int lenght, int x){
    printf("\nInsert your number %d: ", x);
    scanf("%d", &a[x]);
}

关于c - 菜单功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16487055/

相关文章:

java - 如何使用 Luaj 向 _G 添加运行 java 代码的函数?

c - 试图在 C 中返回指向二维数组的两个指针

提示时出现 C 无限循环问题

c++ - C++如何在内存中存储函数和对象?

jquery - jQuery 中非常基本的匿名函数失败?

C++、模板、静态函数

c - Makefile:扩展依赖项

c++ - 通过 `void *` 和 C++ `extern "C"` 函数调用在 C 中初始化一个 C++ 类

python - 使用 ctypes 在 Python 中解码 C const char*

c - 表示结构缓冲区的有效且不易出错的方法?