system pause

you guys told me not to use system("pause")...should i us this insteadcin.get();...also what can i use instead of system("CLS")
im also doing the same thing.

im using

cout << "Press enter to close.\n"; or cout << press any key....
cin.get();

but its not pausing like i want it to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <conio.h>
#include <iostream>

int main()
{
	/* code */

	std::cout << "Press any key to exit...";
	
	while( ! _kbhit() )
	{ /* do nothing until a key is pressed */ }

	return 0;
}


This will wait for a key press before exiting the loop and returing 0 from main. No need to hit enter to continue.
Last edited on
is conio.h part of dev c++ bloodsheds compiler? (is it relevant to all compilers?)

also i dont know what _kbhit() is...
As far as I know, yes. It's the console input output header file.

As for _kbhit(), if a key is being pressed it will return ture, else it will return false.
http://msdn.microsoft.com/en-us/library/58w7c94c%28v=vs.80%29.aspx
Last edited on
Why does this not pause at the cin.get();?
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
// This program averages 3 test scores. It repeats as
// many times as the user wishes.
#include <iostream>
#include <string>
using namespace std;

int main()
{
   int score1, score2, score3; // Three scores
   double average;             // Average score
   char again;                 // To hold Y or N input

   do
   {
      // Get three scores.
      cout << "Enter your 3 test scores and I will average them: \n";
      cin >> score1;
      cin >> score2;
      cin >> score3;
        if (score1 < 0 || score2 < 0 || score3 < 0 )
        {
            cout << "Invalid input, score cannot be less than zero.\n";
            continue;
        }
      
      // Calculate and display the average.
      average = (score1 + score2 + score3) / 3.0;
      cout << "The average is " << average << ".\n";
      
      // Does the user want to average another set?
      cout << "Do you want to average another set of scores? (Y/N) ";
      cin >> again;
      
      cout << "Press enter to close.\n";
      cin.get();
      
   } while (again == 'Y' || again == 'y');
   
   
   return 0;
}
Last edited on
Your cin >> again leaves an endline character in the buffer, which is passed to cin.get() later on. You have to clear the buffer somehow, and protect cin.get() from input errors.

Try this function for pausing:
1
2
3
4
5
6
7
void pause()
{
	std::cout << "Press ENTER to continue.";
	std::cin.clear(); //clear errors
	std::cin.sync(); //clear the buffer
	std::cin.get();
}
1
2
3
cout << "Press enter to close.\n";
cin.ignore (); //add this here
cin.get();


Add cin.ignore(). Do this because when you enter a 'again', again is a char(1 character). When you press enter, what you actually entered was: y\n.

'\n' is a constant for NEWLINE( enter ). This is left in the buffer for cin. ignore() clears this, thus pausing at cin.get()

EDIT:
Or what Cuddlebuddie928 said!
/EDIT:

Also, this:
1
2
3
4
5
if (score1 < 0 || score2 < 0 || score3 < 0 )
{
    cout << "Invalid input, score cannot be less than zero.\n";
    break; //this line!!!!!
}


The above, line 4. If you change break; to continue;, it will jump back to the start of the loop. Re-asking for the 3 values.
Last edited on
Lynx, thanks ill change that, however the program stops completely and closes instead of running the loop again... How do i fix that?

Also what if i move the
1
2
cout << "Press enter to close.\n";
cin.get();

out of the loop, then it shouldn't relate to the previous cin >> again; but still doesnt work right.
Last edited on
Again, you'll need to add ignore(), as you have declared 'again' as a char( 1 character ).

y\n is you entering y and then pressing '\n' enter. You need to get rid of the '\n' still.
Sweet, thanks, i misread what you wrote before, but now i understand, thanks. Damn hidden buffers...
No problem.

Cuddlebuddie928's suggestion is good too! create a function for it, then if you want to pause more than once, you just call that function, rather than typing the code each time.

Also, mark this topic as solve, at the top (:

EDIT:
lol! It's not your topic!
Last edited on
Yes his function idea would be great but i was looking for a simple line or 2 fix, so the cin.ignore/get will suffice. and no its not my topic but it definitely suited me.
ok no one answered my question about what i can do instead of system("CLS"); to clear the screen?
I used this and it works for me.
1
2
3
4
cout << "Press enter to close.\n";   //pauses the program to view results before closing.
cin.ignore();     //use this to clear the buffered data from previous code, you may or may not need this.
cin.get();     // this accepts the enter key and then follows the closing code.
return 0;
Last edited on
Clear the screen http://www.cplusplus.com/articles/4z18T05o/
Keep the console open long enough to see your program's output http://www.cplusplus.com/forum/articles/7312/

conio.h is not part of the standard
Topic archived. No new replies allowed.