简体   繁体   English

程序跳过if语句

[英]Program skips if statements

I've written a program with a lot of if sections.It´s written with Visual Studio 2013(scanf_s). 我编写了一个包含许多if部分的程序。 if是使用Visual Studio 2013(scanf_s)编写的。 It skips some if sections though these are met. if满足这些条件,它将跳过某些部分。 Can you please tell me why? 你能告诉我为什么吗? My suspicion: The first scanf command is executed cleanly. 我的怀疑:第一个scanf命令是干净执行的。 The other scanf commands don't work. 其他scanf命令无效。 I can't input anything. 我什么都不能输入。 The program goes strictly on. 该程序严格进行。 When I insert fflush(stdin) between the scanf commands, it works. 当我在scanf命令之间插入fflush(stdin) ,它可以工作。 I heard bad things about fflsuh because of this I wanna ask: How can I solve it in another way? 因为这个原因,我听到了关于fflsuhfflsuh ,我想问:我该怎么解决呢?

Here is my code: 这是我的代码:

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

int _tmain(int argc, _TCHAR* argv[])
{
char versand='n', stammkunde='t';
double warenwert=1;
printf("Wieviel kostet die Ware?");
scanf_s("%lf", &warenwert);
fflush(stdin);
printf("Wird die Ware abgeholt?(y,n)");
scanf_s("%c", &versand);
if (versand == 'n')
{
    if (warenwert < 100)
    {
        warenwert = warenwert + 7;
    }
    printf("Expressversand?(y,n");
    scanf_s("%c", &versand);
        //fflush(stdin); 

    if (versand == 'y')
    {
        warenwert = warenwert + 10;
    }
}
printf("Stammkunde?(y,n)");
scanf_s("%c", &stammkunde);
if (stammkunde = 'y')
{
    warenwert = warenwert * 0, 97;
}
printf("Endpreis inkl. Versandkosten:%lf", warenwert);
getchar();
return 0;
}

PS: Program output screenshot here: http://i.gyazo.com/01471ce3d563837f526fbcab8363e1f2.png PS:此处的程序输出屏幕截图: http//i.gyazo.com/01471ce3d563837f526fbcab8363e1f2.png

printf("Wird die Ware abgeholt?(y,n)");
scanf_s("%c", &versand);

When you enter input and hit the ENTER key, a character and return key are placed in the input buffer, they are namely: the entered character and the newline character.The character gets consumed by the scanf_s but the newline remains in the input buffer. 当您输入输入并按下ENTER键时,一个字符和返回键将被放置在输入缓冲区中,它们分别是:输入字符和换行符。该字符被scanf_s占用,但换行符仍保留在输入缓冲区中。

Further, 进一步,

printf("Expressversand?(y,n");
scanf_s("%c", &versand);

Your next scanf_s for reading the character just reads/consumes the newline and hence never waits for user input. 下一个用于读取字符的scanf_s仅读取/使用换行符,因此从不等待用户输入。

Way 1: Solution is to consume the extra newline by using: 方法1:解决方案是使用以下命令消耗多余的换行符:

scanf_s(" %c", &versand);
         ^  ---- note the space!

Way 2: You can try this also- 方式2:您也可以尝试以下操作:

fflush(stdin); // flush the stdin before scanning input!
printf("Expressversand?(y,n");
scanf_s("%c", &versand);

Fix this following bugs also- 修复以下错误-

printf("Stammkunde?(y,n)");
scanf_s(" %c", &stammkunde); // give space before %c
if (stammkunde == 'y') // for comparison use == not =
{
    warenwert = warenwert * 0, 97;
}

Edit: In this equation 编辑:在这个方程式中

warenwert = warenwert * 0, 97;

warenwert * 0 have evaluated first, due to high priority. 由于优先级高, warenwert * 0首先进行了评估。 so 所以

warenwert = 0 , 97;

Here = has high priority then , operator. 此处=具有高优先级,运算符。 so warenwert = 0 is assigned first. 因此warenwert = 0分配warenwert = 0 So you will get the result is 0 whenever this if (stammkunde = 'y') condition is true 因此, if (stammkunde = 'y')条件为true,则结果将为0

Sample Run1: - 样品运行1: -

sathish@ubuntu:~/c/basics$ ./a.out 
Wieviel kostet die Ware?
2
Wird die Ware abgeholt?(y,n)
n
Expressversand?(y,n)
y
Stammkunde?(y,n)
n
Endpreis inkl. Versandkosten:19.000000

Run 2: - 运行2: -

sathish@ubuntu:~/c/basics$ ./a.out 
Wieviel kostet die Ware?
2
Wird die Ware abgeholt?(y,n)
n
Expressversand?(y,n)
y
Stammkunde?(y,n) // here your input value becomes 19, due to last condition it becomes zero!
y
Endpreis inkl. Versandkosten:0.000000

And here comes Way 3 comes : 第三种方式来了:

After scanf_s something it changes stdin->_base and stdin->_cnt and that cause thatproblem and if you want to solve it you can write std->_base="\\0"; 在scanf_s之后,它会更改stdin-> _ base和stdin-> _ cnt并导致该问题,如果要解决该问题,可以编写std- std->_base="\\0"; and std->_cnt=0; std->_cnt=0; after every time you used scanf_s for something. 每次您使用scanf_s进行操作之后。 But if you read chars from string it can be different situation i said that for read one variable value. 但是,如果您从字符串中读取字符,可能会出现不同的情况,我说要读取一个变量值。

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

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