system("pause") and system("cls") or system("clear")

I need help with system("pause") and system("cls") or system("clear")

I am using the following:

#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>

I have done research online and everyone says don't use them but my teacher isn't accepting that. I also tried clrscr().

I just need something that pauses and something that clears the screen please. If you suggest the things I already above ...they don't work. If there is different versions or ways to do it besides a void that would be helpful. Any input would be greatly appreciated. I am using Dev C++ version 5.5.3.
For "pause", see this thread:
http://www.cplusplus.com/forum/beginner/1988/

To clear the screen, see this article:
http://www.cplusplus.com/articles/4z18T05o/
@jmilby42

Here is using both, in a program I threw together. I used Dev C++ v5.6.2.

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
#include <iostream>
#include <string>
#include <conio.h> // For using _kbhit()) _getch()

using namespace std;

void ClearScreen();
void WaitKey();

int main()
{
string  line (78,'\x03');
for (int x=0;x<20;x++)
	cout << line << endl;

	
	WaitKey();
	ClearScreen();
	cout << "\n\n\n\t\tHope you enjoyed.." << endl;
	return 0;
}

void ClearScreen()
  {
   DWORD n;
  DWORD size;
  COORD coord = {0};
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
  GetConsoleScreenBufferInfo ( h, &csbi );
  size = csbi.dwSize.X * csbi.dwSize.Y;
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
  SetConsoleCursorPosition ( h, coord );
  }

void WaitKey()
{
		cout << endl << endl << "\t\tPress any key to continue.";
		while (_kbhit()) _getch(); // Empty the input buffer
	_getch(); // Wait for a key
	while (_kbhit()) _getch(); // Empty the input buffer (some keys sends two messages)
}
...that said, however...

For a homework solution, if your teacher doesn't care, go ahead and use pause and cls. Just be aware that the commands are different for linux, so if it is to work for your teacher on his *nix box too, you'd have to code things like:

if (system( "cls" )) system( "clear" );

if (system( "pause" )) system( "read -n 1 -p \"Press any key to continue . . .\"" );


...that said, however...

Why are you using these things anyway? Console programs are not supposed to do that. Design your program to not need them.

The "keep the console window from closing" problem is an issue on windows, so you can safely do something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifdef _WIN32
#include <cstdlib>
#endif

...

int main()
{
   ...

  #ifdef _WIN32
  system( "pause" );
  #endif

  return 0;
}

Hope this helps.
Thank you everyone! I am going to try these out ...I hope they work!
@Duoas...I used your code:

if (system( "cls" )) system( "clear" );

if (system( "pause" )) system( "read -n 1 -p \"Press any key to continue . . .\"" );

It works perfect!! Thank you so much. I thought this would be the easiest and it was.

The teacher wanted us to do a menu and have the user select 1 for add, 2 for multiply and 3 to exit.

If they select 1 then they put in the first number, then the second number, then the program will output "The sum of (first number) and (second number) is (answer)." You get the idea. She wanted it to pause after you get the answer so you could read it, then clear everything to select your next option (but leave the menu). It works great now! I appreciate it! I had everything working but the cls and pause.


Topic archived. No new replies allowed.