Thread: Need some help

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    34

    Need some help

    I need a little help trying to figure out why my program is not doing what I need it to do. Here is the program:

    #include <iostream.h>
    #include <iomanip.h>

    using std::cout;
    using std::cin;
    using std::endl;

    int main()
    {
    int choice1;
    float area1, radi1, leng1, heig1, heig2, bas1;

    do
    {
    cout << "The Geometry Calculator" << endl;
    cout << endl;
    cout << "1. Claculate the area of a circle." << endl;
    cout << "2. Claculate the area of a rectangle." << endl;
    cout << "3. Calculate the area of a triangle." << endl;
    cout << "4. Quit." << endl;
    cout << endl;
    cout << "Enter your choice (1-4) : " << flush;
    cin >> choice1;

    if ( choice1 = 1 )
    {
    cout << "Please enter the radius of the circle: ";
    cin >> radi1;
    area1 = 3.14159 * ( radi1 * radi1 );
    cout << endl;
    cout << "The area of the circle with radius " << radi1 << " is " << setprecision ( 4 )
    << setiosflags (ios::fixed | ios::showpoint ) << area1 << endl;
    }

    else if ( choice1 = 2 )
    {
    cout << "Please enter the length of the rectangle: ";
    cin >> leng1;
    cout << "Please enter the height of the rectangle: ";
    cin >> heig1;
    area1 = leng1 * heig1;
    cout << endl;
    cout << "The area of your rectangle with length " << leng1 << " and height " << heig1 << " is "
    << setprecision ( 2 ) << setiosflags ( ios::fixed | ios::showpoint ) << area1 << endl;
    }

    else if ( choice1 = 3 )
    {
    cout << "Please enter the height of the triangle: ";
    cin >> heig2;
    cout << "Please enter the base of the triangle: ";
    cin >> bas1;
    area1 = bas1 * ( 0.5 * heig2 );
    cout << endl;
    cout << "The area of your triangle with height " << heig2 << " and base " << bas1 << " is "
    << setprecision ( 2 ) << setiosflags ( ios::fixed | ios::showpoint ) << area1 << endl;
    }

    else if ( choice1 = 4 )
    {
    cout << endl;
    cout << "Thanks for making me think! Please Come Again!!!" << endl;
    break;
    }

    }
    while (( choice1 >=1 ) && ( choice1 <= 4 ));
    return 0;
    }

    Now what it is supposed to do is run a continuous loop till option 4 is entered. Everytime I run it, I will only go to option 1 and no other ones. It also makes it tough to escape the loop. What am I doing wrong?
    http://members.ebay.com/aboutme/the_ski/

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    what is it doing wrong?
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    34
    When I run the program it will ask for an option and if you choose any other option it still feels that I entered 1 as the option and does the work for "if ( choice = 1 )" If I take out the elses then it will go through each if action like the question is not even there.

    Hopefully this will help.
    http://members.ebay.com/aboutme/the_ski/

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    34
    Got it figured out!

    I am a dummy!

    I am new to C++ programming.

    I forgot you need to use double equals (==)to do the comparison for an if statement rather then single (=) which means assign

    The Ski
    http://members.ebay.com/aboutme/the_ski/

  5. #5
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    ah a good one...hehe....old comp teacher swore he'd thouroghly embarrass any student who did that....of course that didn't stop me from doing it at least once a month.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

Popular pages Recent additions subscribe to a feed