cin.get function not being executed

I'm not entirely sure how to phrase this, mostly because I don't understand what's happening.
However, I'm trying to write a simple program to run a number trick. In order to pace the program well, I have inserted "cin.get()" function calls after a number of the outputs. This is working well for a large portion of the program, but the first "cin.get" call I make is being ignored every time I run the compiled program.

Here's the script as it currently stands:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string.h>

using namespace std;

int mult (int x); //user-entered integer for number trick

int main()

{
  int x;          // declaring user-entered variable
  char answer[1]; // declaring character variable

    cout<<"Want to try a number trick? y/n \n";
    cin>> answer;
    if (stricmp ("y", answer) == 0 ) //if statement comparing character strings
    {
    cout<<"Ok!  Please enter an integer.\n";
    cin>> x;
    cout<<"Great," << x <<" is a great number.  This trick will turn " << x << " into 7.\n";
    sleep(2);  //Wait so user can read
    cout<<"First, I'm going to add 9.\n = " << x + 9 <<" (enter to continue)\n";
    cin.get();  //Wait until user hits enter
    cout<<"Great, now I'm going to multiply by 2.\n = " << (x + 9) * 2 <<" (enter to continue)\n";
    cin.get();
    cout<<"Now, I'm going to subtract 4.\n = " << ((x + 9) * 2) - 4 <<" (enter to continue)\n";
    cin.get();
    cout<<"Next I'll divide by 2.\n = " << (((x + 9) * 2) - 4) / 2 <<" (enter to continue)\n";
    cin.get();
    cout<<"And now the last step, I will subtract " << x <<"\n = "<< (((((x + 9) * 2) - 4) / 2) - x) <<" (enter to continue)\n";
    cin.get();
    cout<<"TA-DA! Be amazed!\n";
    }  
  else   //other side of if statement
    cout<<"Well forget you, then.";
}  
  


I'm sure it's rather clumsy, and the solution is probably pretty obvious. However, I'm very new to coding, and I haven't had any luck on the forums, so any and all help is greatly appreciated. Thank you!
personally cin.get() never seams to work for me, I use getche(), it always works for me and it does what it looks like your trying to do.
It doesn't work because the cin>>x; leaves the newline the buffer. The first cin.get() reads that as the user having pressed enter. You can use cin.ignore() to throw away input before you wait for an enter.
And that's why getline is just better when working on the console.
Ah, I knew it was going to be something stupid like that...thanks, the script works just how I wanted it to now.
Topic archived. No new replies allowed.