Category Archives: C++ help
C++ Making a Menu
#include <iostream> //Programmer: N/A
#include <string> //MidTerm Assignment 3/10/12
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double radius, area, length1, length2, base, height; //declares variables throughout the program
float num1, num2, num1result1, num2result2;
string verb, place, action, descrip, circle, square, triangle;
string word1result1, word2result2;
char chX;
string object1, word1, word2;
cout <<”***************************************”<< endl; // this is the menu
cout <<”*\tPlease select an option *”<<endl;
cout <<”* *”<<endl;
cout <<”* 1: Calculate area of a shape *”<<endl;
cout <<”* 2: Do a small mad lib *”<<endl;
cout <<”* 3: Swap some variables *”<<endl;
cout <<”* 4: Exit program *”<<endl;
cout <<”* *”<<endl;
cout <<” What is your choice:” <<endl;
cin >> chX;
switch(chX)
{
case’1′ : chX = ’1′;// switching the char(chX) with 4 different selections
break;
case’2′ : chX = ’2′;
break;
case’3′ : chX = ’3′;
break;
case’4′ : chX = ’4′;
break;
default: cout <<”you gave the wrong input, Run the program again”<<endl;
}
system(“cls”);
if(chX == ’1′) //user input “1″ will run this section
{
cout <<”enter an object: circle, square, triangle: “; //selecting an object
cin >> object1;
if(object1 == “circle”) //will caluclate the area of a circle
{
cout <<endl;
cout <<”Enter the radius: “;
cin >> radius;
cout << endl;
area = 3.14 * pow(radius, 2);
cout << “The area is: ” << setprecision(10) << area <<endl; //setprecision is NOT working
}
if(object1 == “square”) //will calcualte the area of a square
{
cout <<endl;
cout <<”Enter the length of both sides: “;
cin >> length1 >> length2;
cout << endl;
area = length1 * length2;
cout << “The area is: ” << setprecision(10) << area <<endl; //I do NOT like setprecision
}
if(object1 == “triangle”)//will calculate the area of a triangle
{
cout <<endl;
cout <<”Enter the base and the height: “;
cin >> base >> height;
cout << endl;
area = .5 * base * height;
cout << “The area is: ” << setprecision(10) << area <<endl; //I have tried everything to
} // fix this precision error, conculsion = I dont like setprecision and it does not like me
else
{
cout <<endl; //if the user input something other than the shapes listed will out put this
cout <<”You gave the wrong input. Run the program again”<<endl;
}
cout << “Press enter to exit.”; //custom exit of the program
cin.ignore();
cin.get();
return 0;
}
if(chX == ’2′) //if # 2 is selected do the following lines of code
{
cout << endl;
cout <<”Enter a verb: “;
cin >> verb; //user input a verb
cout << endl;
cout <<”Enter a place: “;
cin >> place; //user input a place
cout << endl;
cout <<”Enter an action : “;
cin >> action; //user input an action
cout << endl;
cout <<”Enter a description : “;
cin >> descrip; // user input a description
cout << endl;
cout <<”This ” << verb << ” test at ” << place << ” is too ” << descrip <<”!”<<endl;
cout <<”It can go ” << action << ” off a cliff!”<<endl; //combines users inputs
cout << “Press enter to exit.”; //custom exit of the program
cin.ignore();
cin.get();
return 0;
}
if(chX == ’3′) //when #3 is selected the following lines of code will run
{
cout <<”Enter a number, two words ad another number”<<endl; //asking for input
cin >> num1 >> word1 >> word2 >> num2; //the actual input part
cout <<”Word one is: ” << word1 <<endl; //telling the user what they just typed
cout <<”Word two is: ” << word2 <<endl;
cout <<”Number one is: ” <<num1 <<endl;
cout <<”Number two is: ” <<num2 <<endl;
cout <<”switching!!!”<<endl;
word1result1 = word1; //swaps inputs around
word2result2 = word2;
num2result2 = num2;
num1result1 =num1;
cout <<”word one is now: ” << word2result2 <<endl; //displays the swap for the user
cout <<”word two is now: ” << word1result1 <<endl;
cout <<”number one is now: ” << num2result2 <<endl;
cout <<”Number two is now: ” << num1result1 <<endl;
cout << “Press enter to exit.”; //custom exit of the program
cin.ignore();
cin.get();
return 0;
}
if(chX == ’4′) // the following lines of code will run if #4 is selected
{
cout << “Press enter to exit.”; //custom exit of the program
cin.ignore();
cin.get();
return 0;
}
else(chX > 4);//if any number greater than 4 is enter this will outout
{
cout << “you gave the wrong input, Run the program again”<<endl;
}
system (“pause”);
return 0;
}

