hi, i have created a maths app, int choice is the menu, i was wondering after say the user clicks number 1 and has done the area of a circle how can i get it to ask the user if they want to go back to the meanu and how do i get it to go back to the menu?
#include <iostream>
usingnamespace std;
int main()
{
int choice;
cout<< "Maths Helper";
cout<< "\n\n**************************************\n";
cout<< "\n\n Please choose one of the following:\n";
cout<< "\n\n 1 - Area of a circle ";
cout<< "\n 2 - Circumference of a circle ";
cout<< "\n 3 - Factorial of a number ";
cout<< "\n 4 - Hypotenuse of a triangle ";
cout<< "\n 5 - Exit";
cout<< "\n\n\n**************************************\n";
cout<< "\n\n Enter you choice and press return:";
cin>>choice;
switch (choice)
{
case 1:
cout<< "\n\nArea of a circle ";
// define identifier PI with a constant
#define PI 3.14159
// define identifier TWO with a constant
#define TWO 2.0
{
float area, radius;
// user radius input
cout<<"\n\nEnter a radius of a circle: ";
cin>>radius;
// circle area = PI*radius*radius
area = PI * radius * radius;
// circle area
cout<<"\nCircle area = "<<area<<endl;
break;
case 2:
cout<< "Circumference of a circle ";
break;
case 3:
cout<< "Factorial of a number ";
break;
case 4:
cout<< "Hypotenuse of a triangle ";
break;
case 5:
cout<< "Exit";
break;
default:
cout<< "\nNot a valid choice.";
cout<< "\nchoose again";
cin>> choice;
break;
}
}
return 0;
}
Have you looked at subroutines (functions) yet? If so, you can have the menu as a function, and inside, you request input from the user to choose an operation. For instance, if they choose 2, the return value of the function would be 2.
For instance, you could have something along the lines of:
int menu() {
...
int choice;
cout << "Please choose one of the following:\n";
...
cin >> choice // The user inputs 2
return choice; // return the value 2
}
//You can then do your case statements, though instead check the return value of the function:
int r_choice = menu() // r_choice would be 2, since the user choice was 2
*EDIT
int main() {
switch (r_choice) {
...
}
return 0;
}
After this you could request user input as to whether they wish to return to the main menu. If the input is 'y', call the menu function again, though if the input is 'n', return from main() and exit the process.
A less flexible, though simple alternative would be to utilise a while or do-while loop, with the loop only being broken if the user wishes to exit. Hope this helps!
#include <iostream>
usingnamespace std;
int menu()
{
int choice;
cout<< "Maths Helper";
cout<< "\n\n**************************************\n";
cout<< "\n\n Please choose one of the following:\n";
cout<< "\n\n 1 - Area of a circle ";
cout<< "\n 2 - Circumference of a circle ";
cout<< "\n 3 - Factorial of a number ";
cout<< "\n 4 - Hypotenuse of a triangle ";
cout<< "\n 5 - Exit";
cout<< "\n\n\n**************************************\n";
cout<< "\n\n Enter you choice and press return:";
cin>>choice;
return choice;
}
int r_choice = menu()
*Edit
int main()
{
switch (r_choice)
case 1:
cout<< "\n\nArea of a circle ";
// define identifier PI with a constant
#define PI 3.14159
// define identifier TWO with a constant
#define TWO 2.0
{
float area, radius;
// user radius input
cout<<"\n\nEnter a radius of a circle: ";
cin>>radius;
// circle area = PI*radius*radius
area = PI * radius * radius;
// circle area
cout<<"\nCircle area = "<<area<<endl;
break;
case 2:
cout<< "Circumference of a circle ";
break;
case 3:
cout<< "Factorial of a number ";
break;
case 4:
cout<< "Hypotenuse of a triangle ";
break;
case 5:
cout<< "Exit";
break;
default:
cout<< "\nNot a valid choice.";
cout<< "\nchoose again";
cin>> choice;
break;
}
}
return 0;
}
Remove the '*edit', that is the problem. Sorry, I did not mean for that to be part of the code, I was trying to state where I edited my post. Also line 21 should be in main(). Please note that I did NOT compile the code. It was merely a demonstration of what you could implement so a few changes would obviously need to be made.
ive tried a few thing but nothing this is what it is saying
24 C:\Users\ben\Console App\test - edit.cpp expected `,' or `;' before "int"
73 C:\Users\ben\Console App\test - edit.cpp expected declaration before '}' token
#include <iostream>
usingnamespace std;
int menu()
{
int choice;
cout<< "Maths Helper";
cout<< "\n\n**************************************\n";
cout<< "\n\n Please choose one of the following:\n";
cout<< "\n\n 1 - Area of a circle ";
cout<< "\n 2 - Circumference of a circle ";
cout<< "\n 3 - Factorial of a number ";
cout<< "\n 4 - Hypotenuse of a triangle ";
cout<< "\n 5 - Exit";
cout<< "\n\n\n**************************************\n";
cout<< "\n\n Enter you choice and press return:";
cin>>choice;
return choice;
}
int r_choice = menu()
int main{
switch (r_choice){
case 1:
cout<< "\n\nArea of a circle ";
// define identifier PI with a constant
#define PI 3.14159
// define identifier TWO with a constant
#define TWO 2.0
}
float area, radius;
// user radius input
cout<<"\n\nEnter a radius of a circle: ";
cin>>radius;
// circle area = PI*radius*radius
area = PI * radius * radius;
// circle area
cout<<"\nCircle area = "<<area<<endl;
break;
case 2:
cout<< "Circumference of a circle ";
break;
case 3:
cout<< "Factorial of a number ";
break;
case 4:
cout<< "Hypotenuse of a triangle ";
break;
case 5:
cout<< "Exit";
break;
default:
cout<< "\nNot a valid choice.";
cout<< "\nchoose again";
cin>> choice;
break;
}
}
}
return 0;
}
#include <iostream>
usingnamespace std;
int menu()
{
int choice;
cout<< "Maths Helper";
cout<< "\n\n**************************************\n";
cout<< "\n\n Please choose one of the following:\n";
cout<< "\n\n 1 - Area of a circle ";
cout<< "\n 2 - Circumference of a circle ";
cout<< "\n 3 - Factorial of a number ";
cout<< "\n 4 - Hypotenuse of a triangle ";
cout<< "\n 5 - Exit";
cout<< "\n\n\n**************************************\n";
cout<< "\n\n Enter you choice and press return:";
cin>>choice;
return choice;
}
int main
{
int r_choice = menu()
switch (r_choice)
{
case 1:
cout<< "\n\nArea of a circle ";
// define identifier PI with a constant
#define PI 3.14159
// define identifier TWO with a constant
#define TWO 2.0
float area, radius;
// user radius input
cout<<"\n\nEnter a radius of a circle: ";
cin>>radius;
// circle area = PI*radius*radius
area = PI * radius * radius;
// circle area
cout<<"\nCircle area = "<<area<<endl;
break;
case 2:
cout<< "Circumference of a circle ";
break;
case 3:
cout<< "Factorial of a number ";
break;
case 4:
cout<< "Hypotenuse of a triangle ";
break;
case 5:
cout<< "Exit";
break;
default:
cout<< "\nNot a valid choice.";
cout<< "\nchoose again";
cin>> choice;
} // the closing brace for switch
return 0;
} // the closing brace for main()
There were a couple of small problems with the previous code. Line 22 should read int main() rather than just int main, and at line 63 choice has not been defined.
But also I think there should be a while or do-while loop around the code in main(), in order to go back to the menu each time. And it's better to use const rather than #define for values such as PI.
#include <iostream>
usingnamespace std;
char menu()
{
char choice;
cout<< "\nMaths Helper";
cout<< "\n\n**************************************\n";
cout<< "\n\n Please choose one of the following:\n";
cout<< "\n\n 1 - Area of a circle ";
cout<< "\n 2 - Circumference of a circle ";
cout<< "\n 3 - Factorial of a number ";
cout<< "\n 4 - Hypotenuse of a triangle ";
cout<< "\n 5 - Exit";
cout<< "\n\n\n**************************************\n";
cout<< "\n\n Enter you choice and press return: ";
cin >> choice;
return choice;
}
int main()
{
constdouble PI = 3.1415926535897932385;
double radius, area;
char choice;
do
{
choice = menu();
switch (choice)
{
case'1':
cout << "\n\nArea of a circle "
<< "\n\nEnter the radius: ";
cin >> radius;
area = PI * radius * radius;
cout << "\nCircle area = " << area << endl;
break;
case'2':
cout<< "Circumference of a circle ";
break;
case'3':
cout<< "Factorial of a number ";
break;
case'4':
cout<< "Hypotenuse of a triangle ";
break;
case'5':
cout<< "Exit";
break;
default:
cout<< "\nNot a valid choice.";
}
} while (choice != '5');
return 0;
}