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
}
/*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()
{
intunsignedshort MainMenuUC = 0; //set choice to 0 (Default)
intunsignedshort 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;
}
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.
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.
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.
void ShowMain()
{
intunsignedshort MainMenuUC = 0; //set choice to 0 (Default)
intunsignedshort 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;