Hi
I'd like to find out how to let a user repeat a console program or terminate it, rather than have them reopen the program every time. I've tried a bit with do ... while ... but can' seem to figure it out. Plus there will have to be a more elegant way of doing it than that, I think.
Here's the code for the program, but this is more of an open question than anything specifically for one program.
CODE
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
int main(int argc, char *argv[])
{
int ap;
cout << "Do you want to work out an area problem or a perimeter problem? If area, type 1, if perimeter, type 2." << endl;
cin >> (ap);
if (ap==1){
cout << " What kind of shape are you looking at? For a square type 1, for a rectangle type 2, for a triangle type 3 and for a circle type 4."<< endl;
int shapetype;
cin >> shapetype;
if (shapetype==1){
cout << "What is the length of any side?" << endl;
float anyside;
cin >> anyside;
float area;
area = (anyside) * (anyside);
cout << "The area is " << area << " square units" << endl;
}
else if (shapetype == 2) {
cout << "What is the length?" << endl;
float length;
cin >> length;
cout << "What is the width?" << endl;
float width;
cin >> width;
float area;
area = length * width;
cout << "The area is " << area << " square units" << endl;
}
else if (shapetype == 3) {
cout << "What is the base length?" << endl;
float base;
cin >> base;
cout << "What is the height from base to apex?" << endl;
float height;
cin >> height;
float area;
area = ((0.5) * base) * height;
cout << " The area is " << area << " square units." << endl;
}
else if (shapetype == 4) {
cout << "What is the length of the radius?" << endl;
float radius;
float pi =3.14159265358979323846264338327950288419716939937510;
cin >> radius;
float area;
area = (pi) * (radius* radius);
cout << " The area is " << area << " square units." << endl;
}
}
else if (ap == 2) {
cout << "What kind of shape are you looking at? For a square type 1, for a rectangle type 2, for a triangle type 3 and for a circle type 4. "<< endl;
int shapetype;
cin >> shapetype;
if (shapetype == 1){
cout << "What is the length of any one side?"<< endl;
float length;
cin >> length;
float perimeter;
perimeter = length * 4;
cout << "The perimeter is " << perimeter << " units." << endl;
}
else if (shapetype == 2) {
cout << "What is the length?" << endl;
float length;
cin >> length;
cout << "What is the width?" << endl;
float width;
cin >> width;
float perimeter;
perimeter = (length * 2) + (width * 2);
cout << "The perimeter is " << perimeter << " units" << endl;
}
else if (shapetype == 3) {
cout << " What is the length of side 1?" << endl;
float side1;
cin >> side1;
cout << " What is the length of side 2?" << endl;
float side2;
cin >> side2;
cout << " What is the length of side 3?" << endl;
float side3;
cin >> side3;
float perimeter;
perimeter = side1 + side2 + side3;
cout << "The perimeter is " << perimeter << " units" << endl;
}
else if (shapetype == 4) {
cout << "What is the radius?" << endl;
float radius;
cin >> radius;
float diameter;
diameter = 2 * radius;
float pi;
pi = 3.14159265358979323846264338327950288419716939937510;
float circ;
circ = pi * diameter;
cout << "The circumference or perimeter is " << circ << " units";
}
}
system("PAUSE");
return EXIT_SUCCESS;
}