0

I've written a program with a lot of if sections.It´s written with Visual Studio 2013(scanf_s). It skips some if sections though these are met. Can you please tell me why? My suspicion: The first scanf command is executed cleanly. The other scanf commands don't work. I can't input anything. The program goes strictly on. When I insert fflush(stdin) between the scanf commands, it works. I heard bad things about fflsuh because of this I wanna ask: How can I solve it in another way?

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;
}

P.S: Program output screenshot here: http://i.gyazo.com/01471ce3d563837f526fbcab8363e1f2.png

TobiMcNamobi
  • 4,687
  • 3
  • 33
  • 52
Harald Wiesinger
  • 91
  • 1
  • 1
  • 8

2 Answers2

3
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.

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.

Way 1: Solution is to consume the extra newline by using:

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

Way 2: You can try this also-

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. so

warenwert = 0 , 97;

Here = has high priority then , operator. so warenwert = 0 is assigned first. So you will get the result is 0 whenever this if (stammkunde = 'y') condition is true

Sample Run1:-

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:-

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
Sathish
  • 3,740
  • 1
  • 17
  • 28
  • `scanf_s` require the buffer size to be specified for parameters of `%c`, `%s`. http://msdn.microsoft.com/en-us/library/w40768et.aspx – BLUEPIXY Sep 05 '14 at 11:42
  • `fflush(stdin)` is not defined by the standards (although it might work as expected on most systems) – Ingo Leonhardt Sep 05 '14 at 11:46
  • @sathish thx for your answer! I changed all the things. The next problem is, that i always get the value back i´ve typed in at the beginning. in the if conditions are calculations which are not considered. :/ – Harald Wiesinger Sep 05 '14 at 11:59
  • 1
    @HaraldWiesinger what value you are getting? same as input or 97? – Sathish Sep 05 '14 at 12:08
  • 2
    The behavior of `fflush` is not defined for input streams; the operation is nonsensical. If it does clear the input stream, that's by accident, not design. – John Bode Sep 05 '14 at 12:11
  • @JohnBode It is not a standard but it is defined in MSVC. [fflush](http://msdn.microsoft.com/en-us/library/9yky46tz.aspx) – BLUEPIXY Sep 05 '14 at 12:18
  • this `warenwert = warenwert * 0, 97;`also won't compile, not even on a german machine. – ths Sep 05 '14 at 14:04
  • @Sathish and what would it mean? – ths Sep 05 '14 at 20:50
  • @speising See my New edits! it is working correctly! Executing all if conditions. i am using gcc on ubuntu 12.04 – Sathish Sep 06 '14 at 05:34
  • not so correctly if you arrive at a final price of 0.0 instead of 18.43. – ths Sep 06 '14 at 09:59
0

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"; and std->_cnt=0; after every time you used scanf_s for something. But if you read chars from string it can be different situation i said that for read one variable value.

oknsnl
  • 351
  • 1
  • 11