Is there a better way to restart the program according to user input?

This is how I would loop a program according to whether or not the user wants to.
The code would be, for example, a quadratic equation solver. So if "again" is yes or whatever then the program has obviously been through once so the screen is cleared. Then "again" is set to nothing, the code executes and the user is asked again whether or not they want to restart.

I can't help but feel there must be a better way to do this. So, is there? I don't have a problem with this code because it works and that's all that matters really for simple console window programs, I'm just wondering.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;

void clear_screen();

int main()
{
	string again;

	do{
		if(again == "yes" || again == "Yes" || again == "YES" || again == "y")
		{
			clear_screen();
		}

		again = "";



		*[CODE HERE]*



		while( again == "" )
		{
			cout << endl << endl << "Start the program again? -> ";
			cin >> again;

			while(again != "no" && again != "No" && again != "NO" && again != "n" && again != "yes" && again != "Yes" && again != "YES" && again != "y");
			{
				cout << endl << "ERROR: Enter either 'yes' or 'no'" << endl << endl;
			}
		}

	} while(again == "yes" || again == "Yes" || again == "YES" || again == "y");

	return 0;
}
	
void clear_screen()
{
  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 );
}
Topic archived. No new replies allowed.