#include <iostream>
#include <string>
usingnamespace std;
/////SQUARE AREA FUNCTION/////
int Sqareaf (float height, float width){
float sqarea;
sqarea = height*width;
return(sqarea);
}
int main(){
/////DECLARING VARIABLES/////
string shape;
string part;
/////ENTRY TEXT/////
cout << "Hello. Welcome to the geometry program.";
cout << endl << "At any time you may type 'help' to see options";
cout << endl << "Press return to continue";
cin.get();
/////ASKING FOR SHAPE/////
cout << endl << "Please enter the shape we will be working with. -> ";
getline (cin, shape);
/////HELP FOR SHAPE SELECTION/////
do {
cout << "You may enter:" << endl << "rectangle" << endl << "circle" << endl << "sphere" << endl << "cube " << endl;
cout << endl << "Please enter the shape we will be working with. -> ";
getline (cin, shape);}
while (shape=="help");
/////SHAPE INVALID SELECTION/////
/////NAVIGATING RECTANGLE//////
if (shape=="rectangle"){
cout << endl << "Would you like to know the 'area' or 'perimeter' of the rectangle? -> ";
getline (cin, part);
/////RECTANGLE HELP/////
do{
cout << "You may enter:" << endl << "perimeter" << endl << "area";
cout << endl << endl << "Would you like to know the 'area' or 'perimeter' of the rectangle? -> ";
getline (cin, part);
}while (part=="help");
/////RECTANGLE PERIMETER/////
if(part=="perimeter"){
float a;
float b;
int c;
cout << "Enter squares height -> ";
cin >> a;
cout << endl << "Enter squares width -> ";
cin >> b;
c=(2*a)+(2*b);
cout << endl << "The perimeter of your rectangle is " << c;
cin >> c;
}
/////RECTANGLE AREA/////
elseif(part=="area"){
float a;
float b;
int c;
cout << "Enter squares height -> ";
cin >> a;
cout << "Enter squares width -> ";
cin >> b;
float sqarea;
sqarea = Sqareaf (a,b);
cout << "The area of the square is " << sqarea << ".";
cin >> c;}
/////INVALID ENTRY FOR RECTANGLE/////
/////IF CIRCLE IS THE CHOSEN SHAPE/////
if (shape=="circle"){
cout << "Would you like to know the circumference, or area of the circle? -> ";
getline (cin,part);
/////HELP FOR CIRCLE/////
if(part=="help")
do {
cout << "You may enter:" << endl << "area" << endl << "circumference";
cout << endl << "Would you like to know the circumference, or area of the circle? -> ";
getline (cin, part);
} while (part=="help");
/////CIRCLE AREA/////
elseif(part=="area"){
float a;
int c;
cout << "Circles radius -> ";
cin >> a;
c=3.14159*a*a;
cout << endl << "The area of your circle is " << c;
cin >> c;}
/////CIRCLE CIRCUMFERENCE/////
elseif(part=="circumference"){
float a;
int c;
cout << "Circles radius -> ";
cin >> a;
c=2*a*3.14159;
cout << "Your circles circumference is " << c << ".";
cin >> c;
}
/////CIRCLE INVALID ENTRY/////
}
}
I am however, not bein able to code in where if they do not input a valid selection, it will prompt for the entry again. You can see where I intend to place them in the /////INVALID ENTRY///// places in the code. Help me out please?
Thanks. I was tinkering around with it and finally made it work. Ill post it so that if someone reads this with the same problem they can see it....
1 2 3 4 5
if (shape!="help" && shape!="rectangle" && shape!="circle"){
do {cout << "That is an invalid selection.";
cout << endl << "Please enter the shape we will be working with -> ";
getline (cin, shape);}
while (shape!="help" && shape!="rectangle" && shape!="circle");
Make sure this peice is the if statement. NOT the else if statements.
FWIW, if you find yourself copy/pasting code, you're doing it wrong.
In particular that code you just posted is redundant. It checks the same condition twice (once in an if and again in a do-while).
You could shorten it to this:
1 2 3 4 5 6
while (shape!="help" && shape!="rectangle" && shape!="circle")
{
cout << "That is an invalid selection.";
cout << endl << "Please enter the shape we will be working with -> ";
getline (cin, shape);
}