gpt4 book ai didi

c - 在 while 循环中使用函数(在 Atom 中有效,但在 VIM 中无效)

转载 作者:行者123 更新时间:2023-11-30 19:26:07 24 4
gpt4 key购买 nike

我的任务是创建一个移位密码,从用户那里获取一个字符串,然后加密、解密、移动移位变量或退出。我编写的代码在 Atom 中完美运行,但是当我在 VIM 中尝试它时(我必须从其中提交代码),一切正常,除了:每当我的加密或解密函数运行时,输出都是空白的。

    int main(){
int shift = 3;
char stringresponse[SIZE];

while (1 || 2 || 3){
switch(getUserChoice()){
case 1:
shift = getShift();
break;
case 2:
getString(stringresponse);
encrypt(stringresponse,shift);
break;
case 3:
getString(stringresponse);
decrypt(stringresponse,shift);
break;
default :
printf("Exiting the program");
exit(0);
}
}
return 0;
}

int getUserChoice(){
int userchoice = 0;
char dump = 0;
printf("--------------------------------\n");
printf("| 1) Change Shift (Default 3) |\n");
printf("| 2) Encrypt a message |\n");
printf("| 3) Decrypt a message) |\n");
printf("| 4) Quit) |\n");
printf("--------------------------------\n");

printf("Option: ");
scanf(" %d%c",&userchoice,&dump);

return userchoice;
}

int getShift(){
int newshift = 0;
char dump = 0;
printf("Enter a new shift value:");
scanf("%d,%c",&newshift,&dump);

return newshift;
}

void getString(char buf[]){
printf(" Input: ");
fgets(buf,SIZE,stdin);

}

void encrypt(char buf[], int shift){
int i = 0;

while(buf[i] != '\0'){
if (buf[i] == 32){
buf[i] = buf[i];
i++;
}
else if (buf[i] == 10){
buf[i] == 32;
i++;
}
else{
buf[i] -= shift;
i++;
}

}
printf("Output: %5s\n",buf);
}

void decrypt(char buf[], int shift){
int i = 0;

while(buf[i] != '\0'){
if (buf[i] == 32){
buf[i] = buf[i];
i++;
}
else if (buf[i] == 10){
buf[i] == 32;
i++;
}
else{
buf[i] += shift;
i++;
}

}
printf("Output: %5s\n",buf);
}

最佳答案

我测试了上面的代码,这些是我遇到的错误类型(VC++命令行)

error C2371: 'getString': redefinition; different basic types

将函数移至 main() 上方,因为函数应在使用前声明。

error C2065: 'SIZE': undeclared identifier

我将 SIZE 替换为 1024 进行测试。

还有警告:

warning C4553: '==': result of expression not used; did you intend '='?

对于这些

else if (buf[i] == 10){
buf[i] == 32;
i++;
}

完成、格式化和更改我用于测试的代码

#include <stdio.h>
#include <stdlib.h>

int getUserChoice()
{
int userchoice = 0;
char dump = 0;
printf("--------------------------------\n");
printf("| 1) Change Shift (Default 3) |\n");
printf("| 2) Encrypt a message |\n");
printf("| 3) Decrypt a message |\n");
printf("| 4) Quit) |\n");
printf("--------------------------------\n");

printf("Option: ");
scanf(" %d%c", &userchoice, &dump);

return userchoice;
}

int getShift()
{
int newshift = 0;
char dump = 0;
printf("Enter a new shift value:");
scanf("%d,%c", &newshift, &dump);

return newshift;
}

void getString(char buf[])
{
printf(" Input: ");
fgets(buf, 1024, stdin);
}

void encrypt(char buf[], int shift)
{
int i = 0;

while (buf[i] != '\0')
{
if (buf[i] == 32)
{
buf[i] = buf[i];
i++;
}
else if (buf[i] == 10)
{
buf[i] = 32;
i++;
}
else
{
buf[i] -= shift;
i++;
}
}
printf("Output: %5s\n", buf);
}

void decrypt(char buf[], int shift)
{
int i = 0;

while (buf[i] != '\0')
{
if (buf[i] == 32)
{
buf[i] = buf[i];
i++;
}
else if (buf[i] == 10)
{
buf[i] = 32;
i++;
}
else
{
buf[i] += shift;
i++;
}
}
printf("Output: %5s\n", buf);
}

int main()
{
int shift = 3;
char stringresponse[1024];

while (1 || 2 || 3)
{
switch (getUserChoice())
{
case 1:
shift = getShift();
break;
case 2:
getString(stringresponse);
encrypt(stringresponse, shift);
break;
case 3:
getString(stringresponse);
decrypt(stringresponse, shift);
break;
default:
printf("Exiting the program");
exit(0);
}
}
return 0;
}

关于c - 在 while 循环中使用函数(在 Atom 中有效,但在 VIM 中无效),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57931230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com