我需要使用一个函数来获取用户输入,将其存储到一个数组中,然后传递给另一个函数来解密或加密消息。
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
和 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);
}
关于c - 如何让我的程序提示用户输入并使用函数将其存储到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59818059/