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)
}
解决方案
对您的代码的一些改进...
数组作为指针传递。因此,将使用您的数组的函数声明为一个函数,该函数采用数组的指针和最大长度。
您仍然需要为getShift
and提供定义getUserChoice
,并且可能为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);
}
推荐阅读
- python - 如何使用线程让一个动作等待另一个动作完成后再继续?
- javascript - Javascript 变量通过引用发生变化 - 我该如何解决这个问题?
- ubuntu - Ubuntu 20.04 启动到 initramfs
- node.js - 节点 Bcrypt 比较总是返回成功
- javascript - 当有人在 discord.js 中发送命令时,它会吐出错误而不是发送消息
- airflow - 气流通过for循环运行火花提交任务---值没有被迭代
- c - 这个C的代码有什么问题要乘以数字并找出它重复了多少次直到最终数字是一个数字
- flutter - firestore快照在flutter中返回null
- java - Java:实现动态消费Jars的等效方法是什么
- python - 修改和操作目录和子目录中的多个 csv 文件