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

标签 c

我的任务是创建一个移位密码,从用户那里获取一个字符串,然后加密、解密、移动移位变量或退出。我编写的代码在 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/

相关文章:

c - 使用 MiniZip 库读取omni.jar 存档文件

c - 关于 C 结构成员

c - 使用 C 语言的 GNU 科学库进行线性拟合

c - fscanf 转 int 和数组

c - Makefile:一个命令输入多个文件

c - 如何从C中的字符串中提取数字

c - 强制内存排序

c - malloc() 指针输出说明

c - 在 C x86 错误中反转字符串

c++ - 这个插值搜索实现有什么问题?