Pergunta

#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 output 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 is it after the first printf its printing the printf twice but skipping me from input the first time. then how is it calculating 5.24+5.57+5.24 to equal 28.14?

Foi útil?

Solução

As others have mentioned, When you press Enter, two characters get inputted, the character you enter + the newline, You need to account for both of these.

Possible solutions are:

Approach 1: The C way

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

Add a space here, or the better approach,

Approach 2: The C++ way

simply use the C++ way of getting input from user.

cin >> item;

Why the result is Undefined?
Because you did not initialize the variable total, This results in Undefined Behavior giving you unexpected output.
total is a global so it will be Default Initialized to 0.0.
Real reason for Undefined result is in @Mystical's answer.

Outras dicas

enter is a keystroke - you need to account for it :)

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

Wasn't paying attention to the scoping - the real answer for the math is that the loop re-adds the previous cost when enter is pressed. This is noted in Mysticial's answer.

Since the newline has been mentioned, I'll answer the other question of why 28.14.

Notice that in your switch, the default is just return. cost is never set. Therefore, when it reads in the newline it skips the switch block and leaves cost untouched.

So the result is this:

total = 0;  // It's 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

The t that is entered last doesn't get added to total.

This is easily explained. When you enter a and hit the ENTER key, this places two characters in the input buffer, the a and the newline character.

That's why, for all but the first, you have a spurious prompt since it prints it and then gets the newline from standard input.

scanf is really a C compatibility thing in C++, you should be using cin >> something (or any of the streams-related stuff really) for C++-style input.

This double hit of charcaters also explains the errant total as well since, when you get that newline in, you add the current value of cost again in your main loop.

Your total is composed of two of each value due to the fact that you're adding cost regardless of the value entered.

With your entry of a,b,a, that would be 4.25 + 5.57 + 4.25 = 14.07 - a is 4.25, not 5.24. And 28.14 is exactly twice 14.07.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top