While / Switch Statement weird output

#include <stdio.h> #include <iostream> using namespace std; float cost, total; bool loop(char item){ switch (toupper(item)) { case 'A': cost = 4.25; return true; case 'B': cost = 5.57; return true; case 'C': cost = 5.25; return true; case 'D': cost = 3.75; return true; case 'T': return false; } return true; } int main(){ char item; do { printf("\nEnter Item Ordered [A/B/C/D] or T to calculate total:"); scanf("%c", &item); total = total + cost; } while (loop(item)); printf("Total Cost: $%f\n", total); } 

Let me deduce the process:

 $ ./Case3.o Enter Item Ordered [A/B/C/D] or T to calculate total:a Enter Item Ordered [A/B/C/D] or T to calculate total: Enter Item Ordered [A/B/C/D] or T to calculate total:b Enter Item Ordered [A/B/C/D] or T to calculate total: Enter Item Ordered [A/B/C/D] or T to calculate total:a Enter Item Ordered [A/B/C/D] or T to calculate total: Enter Item Ordered [A/B/C/D] or T to calculate total:t Total Cost: $28.139999 

Why, after the first printf, it printff twice, but skips me the first time. then how does it calculate 5.24 + 5.57 + 5.24 equal to 28.14?

0
source share
4 answers

As already mentioned, when you press Enter, two characters are entered, the character you enter + the newline , you need to consider both of these parameters.

Possible solutions:

Approach 1: Path C

  scanf(" %c", &item); ^^^ 

Add a space or a better approach,

Approach 2: C ++ Method

just use the C ++ method to input data from the user.

 cin >> item; 

Why is the result undefined?
Since you did not initialize the variable total , this leads to Undefined Behavior , giving you an unexpected result.
total is global, so it will be Default Initialized equal to 0.0.
The real reason for the Undefined result is the @Mystical answer.

+1
source

enter is a keystroke - you need to consider it :)

As for your math, you never initialize total to 0 , so the initial value is undefined.

I did not pay attention to the field of view - the real answer for mathematics is that the cycle re-adds the previous value when you press enter . This is noted in Misty's answer.

+3
source

This is easy to explain. When you type a and press the ENTER key, it places two characters in the input buffer, the character a and newline .

So, for everyone but the first, you have a false prompt, as it prints it, and then receives a newline from standard input.

scanf is actually C compatible in C ++, you should use cin >> something (or any stuff related to streams) for input in C ++ style.

This double-clicking on charcaters characters also explains the erroneous result, since when you get this newline inside, you add the current value to the value again in your main loop.

Your amount consists of two of each value due to the fact that you add cost regardless of the value entered.

When entering a,b,a it will be 4.25 + 5.57 + 4.25 = 14.07 - a is 4.25 , not 5.24 . And 28.14 exactly twice on 14.07 .

+1
source

Since newline mentioned, I will answer another question about why 28.14 .

Note that on your switch, the default value is just a return. cost never set. Therefore, when he reads in newline , he skips the switch block and leaves the costs intact.

So the result is this:

 total = 0; // It actually undefined since you didn't initialize, but it probably started as zero. total += 4.25; // For a total += 4.25; // For '\n' after the 'a' total += 5.57; // For b total += 5.57; // For '\n' after the 'b' total += 4.25; // For a total += 4.25; // For '\n' after the 'a' 

Final answer: 28.14

t last entered is not added to total .

+1
source

Source: https://habr.com/ru/post/1385756/


All Articles