console closing down even after using cin.ignore(),cin.get()

closed account (E3h7X9L8)
dont just give me the solution explain me why this happens, i could just start debugging using ctrl f5 but that wont satisfy me, i either dont want to make a function bigger than my whole program just to keep my console open.

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
 #include <string>
using std::string;
using std::cout;
using std::endl;
using std::cin;

#include "GradeBook.h"

int main()
{
	string courseName, instructorName;

	cout << "Input course name: ";
	getline(cin, courseName);

	cout << "\nInput instructor name: ";
	getline(cin, instructorName);

	GradeBook gradebook1(courseName, instructorName);
	gradebook1.inputGrades();
	gradebook1.displayGrades();

	cout << "\nPress enter to terminate the program.";

	cin.ignore();
	cin.get();

	return 0;

}
Use this:

1
2
3
4
5
6
#include <limits>

cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.get();

return 0;


cin.ignore(); only ignores the very last character in your stream. So if there's more than one, then it will be caught by the cin.get(); and the program won't be paused. The above code has cin.ignore(); ignore all the characters in the stream.
closed account (E3h7X9L8)
still not working ... still closing
I don't have the contents of the "GradeBook.h" file, so I commented a couple of lines. But this should work fine:

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

//#include "GradeBook.h"

int main()
{
	string courseName, instructorName;

	cout << "Input course name: ";
	getline(cin, courseName);

	cout << "\nInput instructor name: ";
	getline(cin, instructorName);

	//GradeBook gradebook1(courseName, instructorName);
	//gradebook1.inputGrades();
	//gradebook1.displayGrades();

	cout << "\nPress enter to terminate the program.";

	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	cin.get();

	return 0;

}


It wants you to press enter twice, but I don't know what happens in GradeBook.h so I can't be sure how to fix it. You could copy and paste the entire file here if it isn't too big.
Last edited on
explain me why this happens
Because when your program finishes, it gives control to OS. As you did not start command line session directly, your program was running in "temporary" one, which is destroyed as soon as it is not needed anymore.
All IDE worth using knows how (or there is a setting to enable it) to not terminate command line session immideately (they usually run a special runner process which keeps window open after program exits).
This subprogram is disabled when program is run under debugger, as you do not want for anything to interfere with debugging.

You should not interfere with this process: this is the behhavior of all proper console applications.
* If you do not need debugger right now, run without debugger. (This should be default setting, do not know why VS authors decided otherwise)
* If you need debugger, you have problems you need to solve and do not really need to keep window open. In extreme cases, place a breakpoint on return statement in main().
* If you want to run your program outside IDE, do it from command line. Console applications should not be launched by double-clicking on them!
Last edited on
closed account (E3h7X9L8)
thanks for answers i fixed it by using cin.ignore method but i used cin.clean() before it.
Topic archived. No new replies allowed.