c - 如何让我的程序提示用户输入并使用函数将其存储到数组中?

标签 c

我需要使用一个函数来获取用户输入,将其存储到一个数组中,然后传递给另一个函数来解密或加密消息。

  int main()
{
    char inputBuf[SIZE];

    do
    {
        switch (getUserChoice())
        {
            case 1:
            getShift();
            break;

            case 2:
            getString(inputBuf[SIZE]);
            break;

        }
    } while(getUserChoice() != 4);

    return 0;
}

void getString(char inputBuf[SIZE])
{
    char inputBuf[SIZE];
    printf("Input: \n");
    fgets(inputBuf, SIZE, stdin)
}

最佳答案

对代码的一些改进...

数组作为指针传递。因此,将使用数组的函数声明为采用指针和数组最大长度的函数。

您仍然需要提供 getShiftgetUserChoice 的定义,并且大概还需要提供 SIZE 的宏定义(ala #define SIZE 500)

void getString(char* inputBuf, size_t maxLength); // forward declare

int main()
{
    char inputBuf[SIZE];
    int choice;

    while((choice = getUserChoice()) != 4)
    {
        switch (choice)
        {
            case 1:
            {
                getShift();
                break;
            }

            case 2:
            {
                getString(inputBuf, SIZE);
                break;
            }
        }
    } 

    return 0;
}

void getString(char* inputBuf, size_t maxLength)
{
    printf("Input: \n");
    fgets(inputBuf, maxLength, stdin);
}

关于c - 如何让我的程序提示用户输入并使用函数将其存储到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59818059/

相关文章:

用 C 计算算法运行时间

c - 为什么malloc有时不起作用?

c - 即使使用 fflush 也会跳过 scanf

c - C 中的 Unix 路径解析

使用 Xlib 捕获鼠标

c - 如何使用 clang -emit-llvm 编译和保留 "unused"C 声明

c - 为结构中的变量分配内存的最佳方法是什么?

将 char 数组转换为 char 常量 (c)

c - 访问数组索引

c - 了解 SPARC 汇编中函数调用的基本示例