I want to reinitiate my selection menu

Hello, I am doing a project mainly using functions and the if else statement (its what I have learned so far) and I am to get user input and simply do some math depending on what operators they picked. The code below seems a bit redundant but I am using everything I have covered up until now, so excuse my noobish ways.

So far I have like a menu
1) Add
2) Subtract
3) Divide
4) Multiply

User selects an option, they input 2 numbers, I calculate and print it on the screen. My code works fine, and I got it to do what I wanted but once the calculation is over, I want to clear all the text on the prompt and have the user see only the menu selection again incase they want to process another option. Thing is I have no idea how to reinitiate these menus, do I have to call main again?

Here is my code

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Project1.cpp - main focus is calling functions and if else statement

//Header files
#include "stdafx.h"
#include <iostream>
using namespace std;

void funcDiv()
{
	//Variables declared
	int firstNum;
	int secondNum;
	int totalAmt;

	 //DIV header display
	cout << "//////////" << endl;
	cout << "//Divide//" << endl;
	cout << "//////////" << endl;
	cout << endl;
	
	//user input - DIV
	cout << "Enter the 1st number you want to divide: ";
	cin >> firstNum;
	cout << "Enter the 2nd number you want to divide: ";
	cin >> secondNum;

	//calculate user input and print
	totalAmt = firstNum / secondNum;
	cout << firstNum << " / " << secondNum << " = " << totalAmt;
}

void funcMulti()
{
	//Variables declared
	int firstNum;
	int secondNum;
	int totalAmt;

	 //Multi header display
	cout << "////////////" << endl;
	cout << "//Multiply//" << endl;
	cout << "////////////" << endl;
	cout << endl;
	
	//user input - Multi
	cout << "Enter the 1st number you want to multiply: ";
	cin >> firstNum;
	cout << "Enter the 2nd number you want to multiply: ";
	cin >> secondNum;

	//calculate user input and print
	totalAmt = firstNum * secondNum;
	cout << firstNum << " * " << secondNum << " = " << totalAmt;
}

void funcSub()
{
	//Variables declared
	int firstNum;
	int secondNum;
	int totalAmt;

	 //SUB header display
	cout << "////////////" << endl;
	cout << "//Subtract//" << endl;
	cout << "////////////" << endl;
	cout << endl;
	
	//user input - SUB
	cout << "Enter the 1st number you want to subtract: ";
	cin >> firstNum;
	cout << "Enter the 2nd number you want to subtract: ";
	cin >> secondNum;

	//calculate user input and print
	totalAmt = firstNum - secondNum;
	cout << firstNum << " - " << secondNum << " = " << totalAmt;
}


void funcAdd()
{
	//Variables declared
	int firstNum;
	int secondNum;
	int totalAmt;

	//ADD header display
	cout << "///////" << endl;
	cout << "//Add//" << endl;
	cout << "///////" << endl;
	cout << endl;
	
	//user input - ADD
	cout << "Enter the 1st number you want to add: ";
	cin >> firstNum;
	cout << "Enter the 2nd number you want to add: ";
	cin >> secondNum;

	//calculate user input and print
	totalAmt = firstNum + secondNum;
	cout << firstNum << " + " << secondNum << " = " << totalAmt;
}

int main()
{
	//Variables declared
	int operatorSelect;

	//setting bg and text color
	system("color 02");

       //Main header display
	cout << "//////////////////////" << endl;
	cout << "//Calculator Program//" << "    Programmed by: Dee" << endl;
	cout << "//////////////////////" << endl;
	//selection menu
	cout << endl;
        cout << "[1]Add" << endl;
	cout << "[2]Subtract" << endl;
	cout << "[3]Multiply" << endl;
	cout << "[4]Divide" << endl;
	cout << "What would you like to do? (1-4) ";
	cin >> operatorSelect;

	//function calls according to user selection
	if (operatorSelect == 1)
		funcAdd();
	else
		if (operatorSelect == 2)
			funcSub();
		else
			if (operatorSelect == 3)
				funcMulti();
			else
				if (operatorSelect == 4)
					funcDiv();

	char ps;
	cin >> ps;
	return 0;
}

Last edited on
take a look at loops, you should be able to figure it out from that, you could call main if your compiler allows doesn't stop it, but it isn't standard c++ and shouldn't be used.
About clearing the screen, see this:
http://www.cplusplus.com/articles/4z18T05o/

You're technically not allowed to call main() anywhere in your program.
So don't do it!

If you want to go back to displaying the menu, you'll have to use a loop. (I hope you learned those?)
Last edited on
I see, its something extra I wanted to add to the project but it wasn't asked for. The next couple videos I am watching do go into the loops but I am still messing around with functions till I feel positive to go on to the rest. At least I know I will soon be able to code this into my program if I learn loops.

Thanks for the link to the article about clearing the screen, much appreciated!
Last edited on
Topic archived. No new replies allowed.