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 .
source share