Because there was an unread character still in the input by the time your program got to line 48.
Remember, all input comes as a stream, or sequential list, of characters.
Imagine the following program, that produces output (in normal characters) and takes the input (in italics):
C:\Prog\foo> a.exe
What is your name? Mr Bean
How old are you? 58
Hello Mr 58.
C:\Prog\foo> _
|
Let's examine that carefully:
Output: "What is your name? "
Input "Mr Bean" + ENTER KEY
Output: "How old are you? "
Input "58" + ENTER KEY
Output: "Hello Mr 58.\n"
If you were to stick it all together, the input would be a string:
"Mr Bean\n58\n"
(The ENTER key becomes a newline '\n' in the input.)
Here's the trick.
When you read a string with
std::getline(), it reads and throws away the user's ENTER KEY/newline.
But when you read an integer with something like
cin >> n;, it does
not throw anything away. That ENTER KEY/newline is still unread in the input.
So, here we get to the last line of your program, which asks to read a single character from the input. The input has an unread ENTER KEY/newline sitting there, just waiting. It gets read. Program ends.
This is why you must never forget that
the user will always press ENTER at the end of every input. Which means that you must be careful to properly synchronize your inputs so that things like a misplaced newline or any other garbage the user enters is not left behind in the stream.
The
fflush(stdin); you had there was an attempt to fix that. Unfortunately, it is not valid C or C++, and even if it works for your compiler (it will if you use Microsoft's), it probably won't for someone else's compiler (like your teacher's).
So you must scan for the newline yourself.
1 2 3 4 5 6 7 8
|
// Obtain final input
scanf("%lf", &number3);
// Extract everything to the end of line, including the newline/ENTER key.
while (getchar() != '\n') ;
// At this point, we know that user's input is properly synchronized
// with the beginning of the next line of input from the user.
|
And, of course, you can now use that
getchar() at the end of your program, because there won't be any unread characters waiting in the user's input -- so the computer will wait for the user to give it some.
Hope this helps.