1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

while loop and nested if statement problems

Discussion in 'C++' started by Gaylle, Mar 24, 2009.

  1. Gaylle

    Gaylle New Member

    Joined:
    Mar 24, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    hi everyone. hope u'r all doin great 2day. i'm trying to a prob i have with loops
    so far the code i've writtin was successfully built but my answers are not right . cld somebody please check this code and tell me why i'm getting wrong answers with it?

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        //declarations
        int operation;
        double CirRadius, CirArea, Base, Height, TriArea;
        const double PI = 3.14159;
    
        // menu
        cin >> operation;
        while (operation !=4)
        {
            if (operation == 1)
            {
                cin >> CirRadius;
                  CirArea = PI * CirRadius * CirRadius;
                cout<<"The area of a circle with radius "<<CirRadius<<" is " <<setprecision(4)<<CirArea<<'\n\n';
                cin >> operation;
            }
            else if (operation == 2 || operation == 3)
            {
                cin >> Base;
                cin >> Height;
                TriArea = 0.5 * Base * Height;
                cout<<"The area of a triangle with base "<<Base<<" and height "<<Height<<" is "<<TriArea<<'\n\n';
                cin >> operation;
            }
            else if (operation == 5 || operation == 7)
            {
                cout<<"Error: "<<operation<<" is not a valid operation.\n\n";
    
            }
            
        }
    
        return 0;
    }
    heres the whole question

    Geometry Calculator:
    * Use nested if-else statements for all decision making in the program.
    * Declare PI as a double constant with value 3.14159
    * Use a while loop to repeat until the user types a 4 as shown below. If the operation code is not 1 through 4, then print an error message and go to the next data item.
    * If a negative data value is entered by the user (or read from file), make it positive and use it.
    * Use double for all your data variables and print all values with 4 decimal places.
    * When the operation is 4, print "Thank you for using my calculator" and end the program.
    * In the data given, the first figure is the operation value. An operation value of 1 refers a circle and the next value is the radius. An operation value of 2 or 3 refers to a triangle and the values after it are the base and height of the triangle. Operation values of 5 and 7 should show error messages

    Code:
    int operation;
    // display the menu
    cin >> operation;
    while (operation != 4)
    {
        // put your calculations here
        // display the menu
        cin >> operation;
    }
    
    OPERATIONS & DATA VALUES

    1 4.58
    2 6.34 5.8
    3 7 -13
    5
    2 -6 19.4443
    3 81.8 0.543
    1 -8976
    7
    2 12.58 3
    4

    After you have the program running on the screen, change the I/O to file I/O. Your output should be as shown in the sample below, with a blank line between each output:

    The area of a circle with radius 4.5800 is 14.3885
    Error: 5 is not a valid operation
    The area of a triangle with base 7.0000 and height 13.0000 is 45.5000
    Thank you for using my calculator
     
    Last edited by a moderator: Mar 31, 2009
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Have you solved this one as well and are about to make another post with the next bit?
    Or are you really stuck this time?
     

Share This Page