Returning to inital function

I am new to c++ anything i know i learned from a self help book. I am atempting to create a console application that will do simple math equasions. The equasions are selected by the user from the main menu. After they use one equasion i want it to return to the main menu. I cannot get the program to return to the main menu after running equasion_one or equasion_two.the code goes like this. I am new to posting on forms and i apologise for any mistakes in my quieston.

void equasion_one()
{
// code code code
}

void equasion_two()
{
//code code code
}

int main()
{
// request user input and then route accordingly to equasion_one or equasion_two
}

Please use code tags - the <> button on the right under format. It make code much easier to read.

This thread should be in the beginners section.

A basic design for a calculator is this:

1
2
3
4
//Get a Number
//Get an Operator
//Get a number
//Calc answer 


So this should give you 3 functions to start.

Declare functions before main, and put the definitions of them after main.

Let us know how you are going when you have some more code.

I have all of it! is it acceptable to post all of it?


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
145
146
147
148
149
150
151
152
153
154
155
156
157
/*quick math*/

//The following librairies will be included...

#include <iostream>
#include <math.h>
#include <stdlib.h>


/*--------------------------------------------Area of a square and rectangle---------------------------------------------------------------*/
		void Area_SquareAndRectangle()
		{
			double SL1 = 0;															//clear memory
			double SL2 = 0;															//clear memory
			
			std::cout <<"***********************" <<std::endl
					  <<"* Area of a Square    *" <<std::endl
					  <<"*         &           *" <<std::endl
					  <<"* Area of a rectangle *" <<std::endl
					  <<"***********************" <<std::endl
					  <<std::endl
					  <<"Please enter the lenght of one side of the figure:";
			std::cin >> SL1;																//record first side lenght
			std::cout<<"Please enter the lenght of a perpindicular side:";
			std::cin >> SL2;																//record second side lenght
	
			double SRA = SL1*SL2;															//B*H

			std::cout <<std::endl
					  <<"The area of your shape is:" << SRA									//display results	
					  <<std::endl;
			system("pause");
		}

/*-----------------------------------------------area of a triangle-----------------------------------------------------------------------*/
		void Area_Triangle()
		{		
			double BL = 0;																//reset Base Lenght
			double HL = 0;																//reset Hight lenght

			std::cout <<"***********************" <<std::endl								//Title
					  <<"* Area of a Triangle  *" <<std::endl
					  <<"***********************" <<std::endl
					  <<"std::endl"
					  <<"Please enter the lenght of the base:";								//request base lenght
			std::cin >> BL;																	//record base lenght as 'BL'
			std::cout <<"Please enter the lenght of the hight:";							//Request hight lenght
			std::cin >> HL;																	//record hight lenght as 'HL'
			std::cout <<std::endl;

			double TA = BL*HL;																//calculate area and store it as TL

			std::cout <<"The area of your triangle is :" << TA
					  <<std::endl;								//display results

		}

/*----------------------------------------------------area of a circle--------------------------------------------------------------------*/

		void Area_Circle()
		{	
			double R = 0;															//reset radius value to 0

			std::cout <<"***********************" <<std::endl							//Title
					  <<"* Area of a Circle    *" <<std::endl
					  <<"***********************" <<std::endl
					  <<std::endl
					  <<"What is the lenght of the Radius:";							//request radius lenght
			std::cin >> R;																//record radius lenght
			std::cout <<std::endl;
			
			double CA = (3.14159265)*pow(R,2);												//pi*r2

			std::cout <<"The area of your circle is:" << CA								//display results
				<<std::endl;

		}

/*-------------------------------------------------------area of trapizoid----------------------------------------------------------------*/

		void Area_Trapezoid()
		{
			double TZB1 = 0;															//reset memory for base one
			double TZB2 = 0;															//reset memory for base two
			double TZH  = 0;															//reset memory for height
			std::cout <<"***********************" <<std::endl								//Title
					  <<"* Area of a Trapazoid *" <<std::endl
					  <<"***********************" <<std::endl
					  <<std::endl
					  <<"Please enter the lenght of base one:";
			std::cin >> TZB1;																//record base one
			std::cout <<"Please enter the lenght of base two:";
			std::cin >> TZB2;																//record base two
			std::cout <<"Please enter the height of the shape:";
			std::cin >> TZH;																//record height
			std::cout <<std::endl;

			double TZA = ((TZB1+TZB2)*TZH)/2;												//a=((b1+b2)*h)/2

			std::cout <<"The area of your shape is:" << TZA
					  <<std::endl;
		}

/*------------------------------------------------Main Menu-------------------------------------------------------------------------------*/
		int main()		
		{
			int unsigned short MainMenuUC = 0;													//set choice to 0 (Default)
			int unsigned short ReturnMain = 0;													//set exit function to 0
		
			std::cout <<std::endl <<std::endl <<std::endl <<std::endl 
					  <<"******************************************"<<std::endl					//this will explain to the user how to use the program and 
					  <<"* Welcome to Quick Math!(V1.5)           *"<<std::endl 
					  <<"*                                        *"<<std::endl
				      <<"*                                        *"<<std::endl
				      <<"* Quick Math uses a DOS based interface. *"<<std::endl
					  <<"* To select an option enter the number   *"<<std::endl
					  <<"* that proceded your desired option.     *"<<std::endl
					  <<"******************************************"<<std::endl;
				
			std::cout <<"Please select and equation from the list below"						//Thease are the options for the user at this time
				   <<std::endl
				   <<"<------Area Formulas------>"<<std::endl
				   <<"(1) Square/Rectangle"       <<std::endl
				   <<"(2) Triangle"               <<std::endl
				   <<"(3) Circle"				  <<std::endl
				   <<"(4) Trapizoid"			  <<std::endl
				   <<"<---------Other----------->"<<std::endl
				   <<"(5) Quadratics formula"	  <<std::endl
				   <<"(6) Find Hypotenuse"		  <<std::endl
				   <<"(7) Tempature converter"	  <<std::endl
				   <<"Enter your choice:";													//request user input
			std::cin >> MainMenuUC;															//record input
		
			std::cout << std::endl <<std::endl << std::endl;


			switch(MainMenuUC)
			{
			case 1: Area_SquareAndRectangle();
			
				break;
		
			case 2: Area_Triangle();

				break;

			case 3: Area_Circle();

				break;

			case 4: Area_Trapezoid();

				break;

		return 0;

		}
Last edited on
Ok let's see what you have got.
one way to do it is to put everything in your main function in a while loop.
your while loop could then test your MainMenuDC value to see if it is the exit option (maybe you can add an option [9] for exit.

something like:
1
2
3
4
5
6
   while (MainMenuDC != 9)
   {

      //Do Mains work here

   }


to make things more maintainable for future you may even want to put a function in place of [//Do Mains work here] that executes mains original code and returns your MainMenuDC as integer return parameter.
Ill be happy to give that a try. i will let you know how it works when i get a chance.
OK, so your code is not too bad - quite a good effort really.

I like how you have used some functions - that is good.

You could have a ShowMenu and a ProcessMenu function. The Showmenu function would print out all the text that is your Menu, The ProcessMenu would have your switch statement, which would call the functions for each menu option.

Another way to continually loop around the stuff in main could look like this:

1
2
3
4
5
6
7
8
9
bool Quit = false;

while (!Quit)  {  //same as Quit == false
//code goes here

//some end condition sets Quit to true to make the loop finish
if (MenuOption == 'Q')
Quit = true;
}


Check out this skeleton code I did to help someone else - see how simple main is.

http://www.cplusplus.com/forum/beginner/76482/4/#msg412946


The code uses classes - you don't have to do that, you could have all the functions in one file still.

Also note how the functions are declared before main, and defined after main - this is better because one doesn't have to scroll through heaps of code to get to main.

Hope this helps.

if i made two seperate functions would i put the show menu at the top? like this....

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

	                 void ShowMain()		
		{
			int unsigned short MainMenuUC = 0;													//set choice to 0 (Default)
			int unsigned short ReturnMain = 0;													//set exit function to 0
		
			std::cout <<std::endl <<std::endl <<std::endl <<std::endl 
					  <<"******************************************"<<std::endl					//this will explain to the user how to use the program and 
					  <<"* Welcome to Quick Math!(V1.5)           *"<<std::endl 
					  <<"*                                        *"<<std::endl
				      <<"*                                        *"<<std::endl
				      <<"* Quick Math uses a DOS based interface. *"<<std::endl
					  <<"* To select an option enter the number   *"<<std::endl
					  <<"* that proceded your desired option.     *"<<std::endl
					  <<"******************************************"<<std::endl;
				
			std::cout <<"Please select and equation from the list below"						//Thease are the options for the user at this time
				   <<std::endl
				   <<"<------Area Formulas------>"<<std::endl
				   <<"(1) Square/Rectangle"       <<std::endl
				   <<"(2) Triangle"               <<std::endl
				   <<"(3) Circle"				  <<std::endl
				   <<"(4) Trapizoid"			  <<std::endl
				   <<"<---------Other----------->"<<std::endl
				   <<"(5) Quadratics formula"	  <<std::endl
				   <<"(6) Find Hypotenuse"		  <<std::endl
				   <<"(7) Tempature converter"	  <<std::endl
				   <<"Enter your choice:";													//request user input
			std::cin >> MainMenuUC;															//record input
		
			std::cout << std::endl <<std::endl << std::endl;



and then have ProcessMain at the bottom

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

int ProcessMain()

while(MainMenuUC == 0)
{
 ShowMain;
}

switch(MainMenuUC)
			{
			case 1: Area_SquareAndRectangle();
			
				break;
		
			case 2: Area_Triangle();

				break;

			case 3: Area_Circle();

				break;

			case 4: Area_Trapezoid();

				break;

		return 0;
This method worked. I declared MainMenuUC as a global variable. Thank you for your help! it was much apreciciated.
Topic archived. No new replies allowed.