Is there any "break" alternative to system pause other than getch

Aug 1, 2012 at 12:57am
I need some breaks in my program, but I can't use getch or getchar because there is a lot of getline(cin, variable) in the code I'm writing. I don't want to have to resort to system("pause")
Aug 1, 2012 at 1:05am
What's wrong with that?
Aug 1, 2012 at 1:13am
With what? system("pause")? Apparently it opens the cmd just to stop your program, then runs it, then closes cmd to resume your program. Very resource heavy.
Aug 1, 2012 at 1:30am
I'm sorry, I mean using getchar because there is a lot of getline.

Yes, you're right about system calls, I agree on that.
Last edited on Aug 1, 2012 at 1:30am
Aug 1, 2012 at 1:57am
closed account (3bfGNwbp)
Not to mention system calls leave huge security gaps in your program.
Aug 1, 2012 at 3:27am
but I can't use getch or getchar because there is a lot of getline(cin, variable)

Because you have a '\n' in your buffer. Try ignore():
1
2
3
4
5
6
7
8
9
string line;
getline(cin, line);  // getline extracts until it hits the delimiter, which defaults to '\n'
                     // getline(istream& in, string line, char delim = '\n')
cin.ignore(); // ignore(streamsize (an integer) amount, char delim = EOF)

cout << "You typed: " << line << endl;

// without ignore(), the get() returns the '\n' remaining from the getline()
cin.get(); // which is a C++ getchar() 
Last edited on Aug 1, 2012 at 3:30am
Topic archived. No new replies allowed.