Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > General Computer Discussion > General Computer Support > C++ Area calculator question...

Reply
Thread Tools

C++ Area calculator question...

 
 
juststarted juststarted is offline
Junior Member
Join Date: Oct 2008
Posts: 1
 
      10-19-2008
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 re-run."

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 re-run program" << endl;
}
break;
case 'q':
case 'Q': cout << "Good-bye!\n";
break;

default: cout << "\nInvalid choice. Re-run program.\n";


}}

system ("pause");
return 0;
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MVC2: linking from one area view to another area view Andy B. ASP .Net 0 04-30-2010 07:46 AM
do structure definitions go in data area or in code area... hotadvice C Programming 14 10-02-2007 04:10 AM
prevent an sub area in a draggable area to detect dragging event Jim Javascript 0 11-21-2006 09:02 AM
Add point to graphics area without having to paint the whole area? Mark Java 1 06-27-2005 03:09 AM
calculator makko Perl 2 03-01-2005 06:06 AM



Advertisments