Here is the source for a calculator I am working on...
My question is, how do I get the if/else statements to work?
I am trying to get just an output of "invalid number, please rerun."
but the way I have it written the computer still computes the area and perimeter of the circle/triangle/rectangle after the "invalid" message...
Anyhelp would be greatly appreciated...
/*
10/18/2008
This program will calculate the perimeter and area of a Circle, Triangle, and
Rectangle/Square and compute an error message if an incorrect input is entered*/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const double PI = 3.14159;
int main()
{
double radius1,
width,
length,
sideA,
sideB,
sideC,
base,
height;
double CIRCLEAREA = PI;
cout << fixed << showpoint << setprecision(2) << endl;
cout << "Geometry Calculator; " << endl;
cout << "C. Circle Calculatror" << endl;
cout << "R. Rectangle Calculator" << endl;
cout << "T. Triangle Calculator" << endl;
cout << "Q. Quit" << endl;
char choice;
cout << fixed << showpoint << setprecision(2) << endl;
cout << "Enter your choice C, R, T or Q): " << endl;
cin >> choice;
switch (choice)
{
case 'c':
case 'C': cout << "You entered the circle calculator!\n\n",
cout << "Please enter the radius: \n";
cin >> radius1;
cout << "Circle Area = " << CIRCLEAREA*(radius1*radius1) << endl;
cout << "Circumference = " << (radius1+radius1)*PI << endl;
break;
case 'r':
case 'R': cout << "You entered the rectangle calculator!\n\n",
cout << "Please enter the length followed by the width: \n";
cin >> width >> length;
cout << "Rectangle/Square area: = " << width*length << endl;
cout << "Rectangle/Square Perimeter = "<< length+length+width+width << endl;
break;
case 't':
case 'T': cout << "You entered the Triangle calculator!\n\n";
cout << "Enter the length of side A: ";
cin >> sideA;
cout << "Enter the length of side B: ";
cin >> sideB;
cout << "Enter the length of side C: ";
cin >> sideC;
cout << "Enter the length of base: ";
cin >> base;
cout << "Enter height: ";
cin >> height;
cout << "Triangle Area = " << (base/2)*height << endl;
cout << "Triangle Perimeter = " << sideA+sideB+sideC <<endl;
{ if (sideA < 0  sideB < 0  sideC < 0  height < 0  width < 0)
{
cout << "invalid number please rerun program" << endl;
}
break;
case 'q':
case 'Q': cout << "Goodbye!\n";
break;
default: cout << "\nInvalid choice. Rerun program.\n";
}}
system ("pause");
return 0;
