简体   繁体   English

嵌套的If..else-错误的输出,直接使用C编程语言输出到最后

[英]Nested If..else - wrong output, goes directly to last else in C programming language

In C programming language i need to do this question: 在C编程语言中,我需要这样做:

-Write down a program to enter a number. -写下程序以输入数字。 The program reads in the number and determines if it is even or odd. 程序读取数字并确定它是偶数还是奇数。 If the number is even, the program requests the user to enter his age. 如果数字是偶数,程序将要求用户输入他的年龄。 If the age is 18 or above, the program outputs the message “You can vote!” else the message “You cannot vote”. 如果年龄在18岁或以上,程序将输出消息“您可以投票!”,否则输出消息“您不能投票”。 If the number is odd, the program requests the user to enter a character. 如果数字为奇数,程序将要求用户输入一个字符。 If the character is lowercase the program displays the message “Good Luck!” else the program outputs the message “Work hard!”. 如果字符是小写字母,程序将显示消息“祝你好运!”,否则程序将输出消息“努力工作!”。

Unfortunately. 不幸。 when i enter an odd number say 3, it displays "enter a character: Good luck!" 当我输入一个奇数说3时,它将显示“输入一个字符:祝您好运!” before i have a chance to input the character.. It goes directly to the last else. 在我有机会输入角色之前。它直接进入最后一个。 Help please! 请帮助!

int main()
{
int num,age;
char c;
printf("Enter number: ");
scanf("%d", &num);

if (num%2 == 0)
    {
     printf("Age: ");
     scanf("%d",&age);

        if (age>=18)
            printf("You can vote!");
        else
            printf("You cannot vote.");
    }
 else
 {
  printf("Enter a character: ");
  scanf("&c",&c);

        if(c>='A' && c<='Z')
            printf("Work hard!");
        else
            printf("Good luck!");
  }

}

In line : 排队 :

scanf("&c",&c);

It should be ("%c", &c); 应该是("%c", &c); but even if You fix this, it will still be wrong, because scanf leaves '\\n' character in input buffer, so next time, when you read a character with scanf("%c", &c) it will assing c='\\n' . 但是即使您解决了这个问题,它仍然是错误的,因为scanf在输入缓冲区中保留了'\\ n'字符,因此,下次,当您使用scanf(“%c”,&c)读取字符时,它将使c='\\n' You can fix this easily by changing to this 您可以通过更改为此内容轻松解决此问题

 scanf(" %c", &c);

Notice ' ' before %c. 在%c之前注意' ' This empty space tells scanf to eat all trailing white spaces(including \\n) before reading any character from input. 这个空的空间告诉scanf在从输入中读取任何字符之前先吃掉所有结尾的空格(包括\\ n)。

That's common problem with scanf used for reading mixed data and characters, so keep it always in mind. 这是scanf用于读取混合数据和字符的常见问题,因此请始终牢记。

From what I noticed your main has no return. 从我注意到你的主要没有回报。 put return 0; 放回return 0; at the end of function main . 在函数main的末尾。

You have a type in your last scanf() format spec. 您在上一个scanf()格式规范中有一个类型。 Because of that, scanf() does nothing (returning 0 as number of conversions). 因此, scanf()不执行任何操作(将0作为转换数返回)。 You should always check the result of scanf() . 您应该始终检查scanf()的结果。

The trailing enter from the previous scanf is taken up as input for the next scanf 前一个scanf的尾随输入将用作下一个scanf输入

You need to "eat up" the trailing newline: 您需要“吃掉”结尾的换行符:

with something like this : 用这样的东西:

while( (c=getchar()) != '\\n' && c!= EOF);

or simply use : 或简单地使用:

scanf(" %c", &c); //Notice a space

Also fix : 也解决:

scanf("&c",&c); to scanf("%c",&c); scanf("%c",&c);

In place of this (I presume &c is just a typo): 代替这个(我想&c只是一个错字):

scanf("&c",&c);

Try this: 尝试这个:

scanf("%c%*c", &c);

The %c specifier only consumes one character, leaving a newline in the stdin buffer; %c说明符仅占用一个字符,在stdin缓冲区中保留换行符; that gets read by the next call and fails if(c>='A' && c<='Z') . 在下一个调用中读取并失败if(c>='A' && c<='Z')

%*c tells it to read another character and discard ( * ) it, which should get rid of the newline. %*c告诉它读取另一个字符并舍弃( * ),这应该除去换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM