Reputation: 13
I am a beginner that has been coding in c++ for a few weeks now. I have written a my program to have a menu from which a user selects options to perform different tasks. I have 2 questions: firstly how do I make it so the user gets sent back to the menu after performing a task and secondly, how do I make it so when the user is assigning variables (is that what you call it?) the numbers stay on the same line?
#include <iostream>
using namespace std;
int main() {
int choice;
float no0,no1,no2,no3,sum0,ave,pi,rad,areaC,base,height,areaT;
pi=3.142;
cout<<"______________________________________________\n";
cout<<"| MENU |\n";
cout<<"|--------------------------------------------|\n";
cout<<"|1. Calculate the average of 4 numbers. |\n";
cout<<"|2. Calculate the area of a circle. |\n";
cout<<"|3. Calculate the area of a triangle. |\n";
cout<<"|4. Print 'Hello World! |\n";
cout<<"|--------------------------------------------|\n";
cout<<"Enter a number to make a selection... ";
cin>>choice;
switch (choice)
{
case 1:
cout<<"Enter 4 numbers to find their average: ";
cin>>no0>>no1>>no2>>no3;
sum0=no0+no1+no2+no3;
ave=sum0/4;
cout<<"The average of your 4 numbers is: "<<ave<<"\n";
break;
}
switch (choice)
{
case 2:
cout<<"Enter the radius of a cirlce to find its area: ";
cin>>rad;
areaC=pi*rad*rad;
cout<<"The area of your circle is:"<<areaC<<"\n";
break;
}
switch (choice)
{
case 3:
cout<<"Enter the base and height of a triangle to find its area.""\n";
cout<<"Base: "; cin>>base;
cout<<"Height: "; cin>>height;
areaT=base*height*0.5;
cout<<"The area of your triangle is: "<<areaT<<"\n";
break;
}
switch (choice)
{
case 4:
cout<<"Hello World!";
}
return 0;
}
Upvotes: 1
Views: 1687
Reputation: 441
I recommend use an alternative the cycle for reset your program, also group the different case in the same switch
for example:
#include <iostream>
using namespace std;
int main() {
float no0,no1,no2,no3,sum0,ave,pi,rad,areaC,base,height,areaT;
pi=3.142;
int choice;
do{
cout<<"______________________________________________\n";
cout<<"| MENU |\n";
cout<<"|--------------------------------------------|\n";
cout<<"|1. Calculate the average of 4 numbers. |\n";
cout<<"|2. Calculate the area of a circle. |\n";
cout<<"|3. Calculate the area of a triangle. |\n";
cout<<"|4. Print 'Hello World! |\n";
cout<<"|5. Quit. |\n";
cout<<"|--------------------------------------------|\n";
cout<<"Enter a number to make a selection... ";
cin>>choice;
switch (choice){
case 1:
cout<<"Enter 4 numbers to find their average: ";
cin>>no0>>no1>>no2>>no3;
sum0=no0+no1+no2+no3;
ave=sum0/4;
cout<<"The average of your 4 numbers is: "<<ave<<"\n";
break;
case 2:
cout<<"Enter the radius of a cirlce to find its area: ";
cin>>rad;
areaC=pi*rad*rad;
cout<<"The area of your circle is:"<<areaC<<"\n";
break;
case 3:
cout<<"Enter the base and height of a triangle to find its area.""\n";
cout<<"Base: "; cin>>base;
cout<<"Height: "; cin>>height;
areaT=base*height*0.5;
cout<<"The area of your triangle is: "<<areaT<<"\n";
break;
case 4:
cout<<"Hello World!\n";
break;
case 5:
cout<<"Bay\n";
break;
default:
cout<<"Wrong selection\n";
break;
} // End Switch
//If you want clear the screen you can use the instruction:cout<<"\033[2J\033[1;1H";
}while(choice != 5); // End Loop
return 0;
}
Upvotes: 1
Reputation: 597
You can add an endless loop that will return your user to the beginning of the program indefinitely. If you want it to stop, you can add a case that sets active
to false
.
I also fixed your switch cases. As someone mentioned, it's not necessary to switch
for each case - the program will automatically find the correct path.
Do something like this:
#include <iostream>
using namespace std;
int main() {
bool active = true;
while(active)
{
int choice;
float no0,no1,no2,no3,sum0,ave,pi,rad,areaC,base,height,areaT;
pi=3.142;
cout<<"______________________________________________\n";
cout<<"| MENU |\n";
cout<<"|--------------------------------------------|\n";
cout<<"|1. Calculate the average of 4 numbers. |\n";
cout<<"|2. Calculate the area of a circle. |\n";
cout<<"|3. Calculate the area of a triangle. |\n";
cout<<"|4. Print 'Hello World! |\n";
cout<<"|5. Quit. |\n";
cout<<"|--------------------------------------------|\n";
cout<<"Enter a number to make a selection... ";
cin>>choice;
switch (choice)
{
case 1:
cout<<"Enter 4 numbers to find their average: ";
cin>>no0>>no1>>no2>>no3;
sum0=no0+no1+no2+no3;
ave=sum0/4;
cout<<"The average of your 4 numbers is: "<<ave<<"\n";
break;
case 2:
cout<<"Enter the radius of a cirlce to find its area: ";
cin>>rad;
areaC=pi*rad*rad;
cout<<"The area of your circle is:"<<areaC<<"\n";
break;
case 3:
cout<<"Enter the base and height of a triangle to find its area.""\n";
cout<<"Base: "; cin>>base;
cout<<"Height: "; cin>>height;
areaT=base*height*0.5;
cout<<"The area of your triangle is: "<<areaT<<"\n";
break;
case 4:
cout<<"Hello World!";
break;
case 5:
active = false; // Could even just return 0 here
break;
} // End Switch
} // End Loop
return 0;
}
Upvotes: 2