如果在main函数中忽略了语句 [英] If statement being ignored in main function

查看:157
本文介绍了如果在main函数中忽略了语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在用C语言编写代码,并忽略了main函数中的if语句。如您所见,此代码接收一些字符串作为输入并应用Caesar的密码。注意:在main中调用的函数加密也是定义的,我只是不粘贴,因为我不认为这是必要的,因为问题是当我问用户是否要加密或解密文本时,fgets if if语句被完全忽略(写入加密后程序退出)。
代码如下:

I'm currently writing a code in C and my if statement in main function is being ignored. As you can see, this code receives some string as input and applies the Caesar's Cipher. Note: Function ciphering called in main is also defined, I just don't paste because I don't think it is necessary because the problem is that when I ask the user if he wants to encrypt or decrypt the text, after fgets the if statement is completely ignored (after writing "encrypt" the program just quits). The code is the following:

int main()
{
    char text[SIZE], choice[SIZE];
    printf("\nWelcome to Caesar's Cipher.\nDo you wish to encrypt or decrypt text?\n");
    fgets(choice, SIZE, stdin);
    if (strcmp(choice, "encrypt") == 0)
    { 
        printf("Insert text to encrypt:\n");
        fgets(text, SIZE, stdin);
        ciphering(text);
        printf("\nThis is your text encrypted with Caesar's Cipher:\n%s\n", text);
    }
    return 0;
}


推荐答案

<$ c的字符串$ c> fgets get最终有一个尾随的 \ n 。您需要手动删除它,或将其与encrypt \ n进行比较

The string that fgets gets has a trailing \n in the end. You need to remove it manually, or compare it with "encrypt\n"

这篇关于如果在main函数中忽略了语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆