无论输入什么输入,If语句的主体都会运行

时间:2015-04-14 08:56:38

标签: c if-statement

我觉得这将是一个非常简单的错误,但无论我在程序中输入什么字符,“帮助屏幕”都会出现问题。仍然显示。我一直在寻找如何修复一段时间并且不能破解它,正如我所说,我觉得这将是非常愚蠢和简单的事情。我对C没有多少经验,所以对于任何业余错误都是如此。 哪些符号用于分配,哪些符号用于C中的比较? ( = and ==

int initialSelection(){
printf( "                                Welcome to Anagramania!\n");
printf( "Please press (s) to start or (h) to view the help screen\n");
initialChoice = getchar();
    if (initialChoice = 'h'){ //Display help screen
        system("cls");
        printf( "                                Anagramania Help Screen\n");
        printf( "Welcome to Anagramia, created by Toby Cannon. There are three levels of difficulty in this game, easy, medium, or hard! How good do you think you are? Once you start the game you will see some jumbled letters on the screen. You're job is to guess what word these letters have come from! There is 20 words in each game, and you can review your game at the end. \n Good luck!\n");
            getch(); //wait for user input
            system("cls"); //Clear the console
        }
}

2 个答案:

答案 0 :(得分:7)

=作业,因此您的代码正在做的是 'h'分配给initialChoice,然后测试结果,值'h'(赋值表达式的值是指定的值)。最终测试为true,因此执行if的正文。

==是平等比较。所以:

if (initialChoice == 'h'){
// Note -----------^

任何体面的编译器都应该有" lint"当你这样做时警告你的功能。 (在文档中搜索"警告")

答案 1 :(得分:2)

需要看起来像这样:if (initialChoice == 'h')所以你实际上是在检查左边的表达式是否等于右边的表达式