C++ Area calculator question...

Discussion in 'General Computer Support' started by juststarted, Oct 20, 2008.

  1. juststarted

    juststarted

    Joined:
    Oct 20, 2008
    Messages:
    1
    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;
     
    juststarted, Oct 20, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sai

    bandwidth usage calculator

    Sai, Nov 7, 2003, in forum: Cisco
    Replies:
    5
    Views:
    8,034
    shope
    Nov 7, 2003
  2. Jim Jones
    Replies:
    7
    Views:
    2,424
    Juan Pérez
    Apr 14, 2004
  3. Rudiger Flatula

    Loan Repayment Calculator

    Rudiger Flatula, Aug 10, 2004, in forum: Computer Support
    Replies:
    6
    Views:
    6,523
    tgilb
    Aug 11, 2004
  4. Matt

    ti 84+ se calculator

    Matt, Aug 22, 2004, in forum: Computer Support
    Replies:
    0
    Views:
    583
  5. BIGEYE

    Casio Algebra 2.0 Calculator Help

    BIGEYE, Oct 1, 2004, in forum: Computer Support
    Replies:
    3
    Views:
    628
    BIGEYE
    Oct 2, 2004
Loading...

Share This Page