Switched compilers; need pause script

I very recently learned how to program at all, and I'm starting in c++.

I started with a helloworld type program, and it worked fine.

I made a program that repeats what you input, and that worked fine as well.

I made a program that is a basic calculator. It worked fine.

I did all this using Borland 5.5

I found a much better compiler, MinGW

I uninstall borland, delete my .exes and all the files except for the .cpps, and I compile them all again using MinGW.

The helloworld and repeater worked fine, exactly as before.

The problem is that the calculator closes immediately after the calculation (the final output)

I compared the repeater:

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
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
  
  char word[80];
  
  cout << "Type a line of text.\n";
  
  cin.get(word, 79);
  
  cout << "Did you type:\n";
  
  cout << word << "?" << endl;
  
  cin.ignore();
  
  cout << "Press enter to end program.";
  
  cin.ignore();
  
  return 0;

}


With the calculator:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <cstdlib>
#include <iostream>
using namespace std;


int main()
{
	//float is used for numbers with possible decimals, int is used for any number
	float num1;
	float num2;
	int num3;
	float num4;
	
	cout << "Enter a number." << endl;
	
	cin >> num1;
	
	cout << "Enter a second number." << endl;
	
	cin >> num2;
	
	cout << "Choose an operation" << endl;
	
	cout << "1 = add, 2 = subtract, 3 = multiply, 4 = divide" << endl;
	
	cin >> num3;
	
	if (num3 > 4 || num3 < 1)
	{
  	
  	cout << "You have entered an invalid operation." << endl;
  	
  	cout << "Press enter to end the program. If you would like to try again, follow the instructions next time!";
  	
  	cin.ignore();
  	
  	return 0;
  	
	}
	
	else
	
	{
  	
  	if (num3 == 1)
  	
  	{
    	
    	num4 = num1 + num2;
    	
    	cout << "Your answer is: " << num4 << endl;
    	
  	}
  	
  	else if (num3 == 2)
  	
  	{
    	
    	num4 = num1 - num2;
    	
    	cout << "Your answer is: " << num4 << endl;
    	
  	}
  	
  	else if (num3 == 3)
  	
  	{
    	
    	num4 = num1 * num2;
    	
    	cout << "Your answer is: " << num4 << endl;
    	
  	}
  	
  	else if (num3 == 4)
  	
  	{
    	
    	num4 = num1 / num2;
    	
    	cout << "Your answer is: " << num4 << endl;
    	
  	}
  	
	}
	
	cout << "Press enter to end the program.";
	
	cin.ignore();
	
	return 0;

}


And I can't seem to find the problem.

In place of the "cin.ignore();" in both of them I had "getchar();" but I tried changing it. Still, same problem.

I know the problem lies in that, which is essentially a pause before the end of the program.

I have searched the forums and google and other programming sites, and I haven't found anything that worked.

I just want a simple pause script that will let me see the output before the prompt closes.

I've been searching for two hours now, any help is greatly appreciated.
The problem is lines like these:

cin >> num3;

These expect formatted input, so they will leave junk in the buffer when they are done. You have two options here:

1) Get input the way you are, and after getting it, make sure to get everything until the next newline (\n)
2) Get input as a string using std::getline(), and then use a stringstream to parse the integer yourself
Thanks for the quick response

1) So basically
cin >> num3\n
??
2)I literally started less than 10 hours ago, I have no concept of programming lingo.
What do you mean that you're using MingW? MingW is a binary of the gcc, which is purely a compiler that you must run from the command prompt anyways. It should never close unless you specifically tell it to.

That aside, what firedraco was saying is to use the cin.ignore after each cin >> statement like so:
1
2
3
cin >> num1; // Accepts an integer
cin.ignore(256,'\n'); // Ignores anything after the integer
//for 256 characters or the enter key was pressed, which ever comes first 
Last edited on
2) When you type something into the console, you are putting it into the buffer. The cin will extract from the buffer in various ways, depending upon how you tell it to. When you use cin (via the keyboard) it is waiting for you to hit enter. Type in 1234 and hit enter and your buffer looks like this:
[1, 2, 3, 4, '\n'}. cin >> number; would extract the 1234, but leave the '\n' character in the buffer.

So you know that cin.ignore(), basically makes your program stop until you hit enter. However, if there is already something in the buffer, it will simply ignore it and continue the program.

Do what Volatile Pulse says and your program should start pausing again.

~Btw, shouldn't that be cin.ignore(256, '\n')?
Last edited on
...I have no clue what you're talking about LowestOne...
Thanks so much for the help.

So basically just add a "cin.ignore" line after every "cin" line, and then the pause at the end of the program will have nothing in it that would cause it to think there is already an input from the user?

Would I have to add that after every "cin" line (I imagine that would cause lots of problems in complicated programs) or would having two "cin.ignore" lines at the end achieve the same result?
No, it doesn't cause issues. A typical user will press enter after they input their answer.

The cin.ignore(...) I posted will ignore spaces letters numbers, and everything else after the user entered an integer. As soon as they press enter, you code moves on. Unless someone is purposely trying to break it, there should be no negative side effects from using the cin.ignore. And then you can use the cin.ignore() at the end of your program will pause and wait for some input.
Okay, that fixed it. Thanks for the help.
Last edited on
Topic archived. No new replies allowed.