What's the difference?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "stdafx.h"


#include <iostream>


using namespace std;

int main()

{
	int SecretNumber;
	cout << "What is your favorite number?" << endl;
	cin >> SecretNumber;
	cout << "Your new number is ";
	SecretNumber = SecretNumber + 30;
	cout << SecretNumber << endl;
	
	system("pause");
	return 0;
}


At the bottom, why can't I use cin.get() instead of system("pause")?

I heard system("anything") is very slow, and should try to be avoided.
In regards to system():

http://www.cplusplus.com/forum/articles/11153/

In this case, you can replace system("pause") with:

1
2
cin.ignore();
cin.get();


or

1
2
cin.get();
cin.get();


Hmmm thanks. They worked. Could you kindly explain a bit about why they work?
(Because you just added another cin.get() in the second one)

Lol didn't understand 100% about the system pause, maybe like 60%, but that's good enough for me right now, I'm just a beginner =D
cin.get() extracts a single character from the stream. In this case you already have a \n character in the stream from when you pressed Enter after your input, so your cin.get() will read that and the program will close. By adding a second cin.get(), you are assuring that the previous \n character was read and that the user will have to press Enter again. As for cin.ignore(), it will simply ignore the current \n character left in the stream and force the user to press Enter to continue.

It should be said that you won't always need to have two cin.get() statements. If there isn't a \n character hanging out in the stream from the user hitting Enter previously, then one cin.get() will suffice.
system("pause"); is like typing pause in the cmd. Try it.
Topic archived. No new replies allowed.