I'm trying to write a code for the good ol' geometry calculator problem. I got most of it figured out (I think) but I cant get it to validate it correctly. Any idea?
Here's my code. I played around with the return, which kinda helps.
Btw it option 1 at the moment. Valid inputs should only be 1-1000 only, which anything else should end the program but it still gives an answer.
#include<iostream>
#include<cmath>
#include<iomanip>
usingnamespace std;
int main()
{
double area = 0, length = 0, width = 0;
double base = 0, height = 0, radius = 0, pie = 3.14159;
int choice = 0;
cout << "Geometry Calculator. " << endl;
cout << "1. Calculate The Area of a Circle ." << endl;
cout << "2. Calculate The Area of a Rectangle ." << endl;
cout << "3. Calculate The Area of a Triangle. " << endl;
cout << "4. Quit. " << endl;
cout << "Please enter a choice (1-4) : " << endl;
cin >> choice;
if (choice >= 1 && choice <= 4)
{
switch (choice)
{
case 1:
cout << "What is the radius of the circle?. ";
cin >> radius;
{
if (radius >= 1 && radius <= 1000);
else cout << "You've entered an invalid selection." ;
int main;
}
area = pie * pow(radius, 2);
cout << "The area of the Circle is " << area << endl;
break;
}
switch (choice)
{
case 2:
cout << "Please enter the width. ";
cin >> width;
cout << "Please enter the height. ";
cin >> height;
area = width * height;
cout << "The area of the Rectangle is " << area << endl;
break;
}
switch (choice)
{
case 3:
cout << "Please enter the base. ";
cin >> base;
cout << "Please enter the height. ";
cin >> height;
area = .5 * base * height;
cout << "This is the area of the Triangle. " << area << endl;
break;
}
}
return 0;
}
switch (choice)
{
case 1:
{
cout << "What is the radius of the circle?. ";
cin >> radius;
if (radius >= 1 && radius <= 1000)
{
area = pie * pow(radius, 2);
cout << "The area of the Circle is " << area << endl;
}
else
{
cout << "You've entered an invalid selection.";
}
break;
}
case 2:
{
cout << "Please enter the width. ";
cin >> width;
cout << "Please enter the height. ";
cin >> height;
area = width * height;
cout << "The area of the Rectangle is " << area << endl;
break;
}
case 3:
{
cout << "Please enter the base. ";
cin >> base;
cout << "Please enter the height. ";
cin >> height;
area = .5 * base * height;
cout << "This is the area of the Triangle. " << area << endl;
break;
}
}
#include<iostream>
#include<cmath>
#include<iomanip>
usingnamespace std;
int main()
{
double area = 0, length = 0, width = 0;
double base = 0, height = 0, radius = 0, pie = 3.14159;
int choice = 0;
cout << "Geometry Calculator. " << endl;
cout << "1. Calculate The Area of a Circle ." << endl;
cout << "2. Calculate The Area of a Rectangle ." << endl;
cout << "3. Calculate The Area of a Triangle. " << endl;
cout << "4. Quit. " << endl;
cout << "Please enter a choice (1-4) : " << endl;
cin >> choice;
if (choice >= 1 && choice <= 4)
{
switch (choice)
{
case 1:
cout << "What is the radius of the circle?. ";
cin >> radius;
{
if (radius >= 1 && radius <= 1000);
elseif (cout << "You've entered an invalid selection.")
break;
}
area = pie * pow(radius, 2);
cout << "The area of the Circle is " << area << endl;
break;
}
switch (choice)
{
case 2:
cout << "Please enter the width. ";
cin >> width;
{
if (width >= 1 && width <= 1000);
elseif (cout << "You've entered an invalid selection.")
break;
}
cout << "Please enter the height. ";
cin >> height;
{
if (height >= 1 && height <= 1000);
elseif (cout << "You've entered an invalid selection.")
break;
}
area = width * height;
cout << "The area of the Rectangle is " << area << endl;
break;
}
switch (choice)
{
case 3:
cout << "Please enter the base. ";
cin >> base;
{
if (base >= 1 && base <= 1000);
elseif (cout << "You've entered an invalid selection.")
break;
}
cout << "Please enter the height. ";
cin >> height;
{
if (height >= 1 && height <= 1000);
elseif (cout << "You've entered an invalid selection.")
break;
}
area = .5 * base * height;
cout << "This is the area of the Triangle. " << area << endl;
break;
}
}
return 0;
}
#include<iostream>
#include<cmath>
#include<iomanip>
usingnamespace std;
int main()
{
double area = 0, length = 0, width = 0;
double base = 0, height = 0, radius = 0, pie = 3.14159;
int choice = 0;
cout << "Geometry Calculator. " << endl;
cout << "1. Calculate The Area of a Circle ." << endl;
cout << "2. Calculate The Area of a Rectangle ." << endl;
cout << "3. Calculate The Area of a Triangle. " << endl;
cout << "4. Quit. " << endl;
cout << "Please enter a choice (1-4) : " << endl;
cin >> choice;
if (choice >= 1 && choice <= 4);
elseif (cout << "You've entered an invalid selection.");
{
switch (choice)
{
case 1:
cout << "What is the radius of the circle?. " << endl;
cin >> radius;
if (radius >= 1 && radius <= 1000);
elseif (cout << "You've entered an invalid selection.")
break;
area = pie * pow(radius, 2);
cout << "The area of the Circle is " << area << endl;
break;
case 2:
cout << "Please enter the width. " << endl;
cin >> width;
if (width >= 1 && width <= 1000);
elseif (cout << "You've entered an invalid selection.")
break;
cout << "Please enter the height. " << endl;
cin >> height;
if (height >= 1 && height <= 1000);
elseif (cout << "You've entered an invalid selection.")
break;
area = width * height;
cout << "The area of the Rectangle is " << area << endl;
break;
case 3:
cout << "Please enter the base. ";
cin >> base;
if (base >= 1 && base <= 1000);
elseif (cout << "You've entered an invalid selection.")
break;
cout << "Please enter the height. " << endl;
cin >> height;
if (height >= 1 && height <= 1000);
elseif (cout << "You've entered an invalid selection.")
break;
area = .5 * base * height;
cout << "This is the area of the Triangle. " << area << endl;
break;
case 4:
cout << "Press any key to exit program." << endl;
}
}
return 0;
}
You only need ONE switch keyword, then list your cases inside this.
Also your brace placement is pretty messed up.
You want to do (maybe) something like this:
I saw your example of where to place the brace's but, every time entered them VS 13 would place them there. Is that were the software wants them to be?
And thanks for the ONE switch keyword! Looks a bit better now!
You've pretty much ignored all my advice about bracing :)
You still have stuff like this in it:
1 2 3
if (width >= 1 && width <= 1000);
elseif (cout << "You've entered an invalid selection.")
break;
That semi colon at the end of the if should not be there. You want to also get into the habit of structuring if/else's like this:
1 2 3 4 5 6 7 8 9 10 11
if( blah)
{
// do something
}
else
{
// do something else
}
one more thing: this is the condition you want to calculate the area:
if (width >= 1 && width <= 1000)
but it looks like your code is treating this as the fail condition. If you look at my post again you'll see I swapped around the if/else conditions so the area is calculated if the user enters an integer between 1 and 1000, although i could be wrong in this. it's difficult to tell with your if/else structure :)