Looping switch

I want to loop back the main menu. Whenever i input a number it'll output the inorder, preorder, and postorder. After outputting, it'll display a message "Press any key to continue..." and it'll go back to main menu and let you input another number. It should not exit.

help?
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
61
62
63
64
65
66
67
68
69
70
  #include<iostream>
using namespace std;

struct node
{
	int info;
	struct node *left, *right;
};

struct node *newNode(int num)
{
	struct node * node = (struct node*)malloc(sizeof(struct node));
	node->info = num;
	node->left = NULL;
	node->right = NULL;
	return node;
}

void inorder(struct node *);
void postorder(struct node *);
void preorder(struct node *);

int main()
{
	int choice;
	bool done = false;
	struct node *root = newNode(60);
	root->left = newNode(30);
	root->right = newNode(80);
	root->left->left = newNode(20);
	root->left->right = newNode(40);

	cout << "=========================\n";
	cout << "     [1] Inorder\n";
	cout << "     [2] Postorder\n";
	cout << "     [3] Preorder\n";
	cout << "     [4] Exit\n";
	cout << "=========================\n";

	while (!done)
	{
		cout << "Enter your choice: ";
		cin >> choice;
		done = true;

		switch (choice)
		{
		case 1:
			cout << "Inorder Traversal: ";
			inorder(root);
			break;
		case 2:
			cout << "Postorder: ";
			postorder(root);
			break;
		case 3:
			cout << "Preorder: ";
			preorder(root);
			break;
		case 4:
			exit(1);
		default:
			cout << "Invalid choice!";
			done = false;
		}
	}

	system("pause>0");
	return 0;
}
Last edited on
Just put the menu inside of the while loop
i tried it and it still exits.
Just add a label
vandal:
cout << "=========================\n";
cout << " [1] Inorder\n";
cout << " [2] Postorder\n";
cout << " [3] Preorder\n";
cout << " [4] Exit\n";
cout << "=========================\n";

Then jjust add a "goto vandal;" in the cases

case 1:
cout << "Inorder Traversal: ";
inorder(root);
goto vandal;
break;
Thank you!
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
int main()
{
	int choice;
	struct node *root = newNode(60);
	root->left = newNode(30);
	root->right = newNode(80);
	root->left->left = newNode(20);
	root->left->right = newNode(40);

	menu:
	cout << "=================================\n";
	cout << "         [1] Inorder\n";
	cout << "         [2] Postorder\n";
	cout << "         [3] Preorder\n";
	cout << "         [4] Exit\n";
	cout << "=================================\n";

	cout << "Enter your choice: ";
	cin >> choice;

	switch (choice)
	{
	case 1:
		cout << "=================================\n";
		cout << "Inorder Traversal: ";
		inorder(root);
		system("pause>0");
		system("cls");
		goto menu;
		break;
	case 2:
		cout << "=================================\n";
		cout << "Postorder: ";
		postorder(root);
		system("pause>0");
		system("cls");
		goto menu;
		break;
	case 3:
		cout << "=================================\n";
		cout << "Preorder: ";
		preorder(root);
		system("pause>0");
		system("cls");
		goto menu;
		break;
	case 4:
		exit(1);
	default:
		cout << "=================================\n";
		cout << "Invalid choice!";
	}

	system("pause>0");
	return 0;
}
No problem!
Topic archived. No new replies allowed.