Menu

help with switch function

Kilzhelm
2010-04-12
2012-09-26
  • Kilzhelm

    Kilzhelm - 2010-04-12

    Hi, I've written this code and I've run into a few problems. I got it to run
    but when it comes time for the user to enter the selection, it just keeps
    going down the line of switch functions such as if I enter 1 as a selection it
    goes to the area of the circle, but if i put in any number it automatically
    goes to the length of the rectangle, then the width, etc. I'm trying to run
    the math equations after the initial selection but I'm not sure how to do
    that. I'm pretty sure i have to somehow link the math equations with the
    selection but when i tried doing that it gave me an error. Under the math
    equations i originally had

    1=Circle
    2=Rectangle
    3=Triangle
    

    However that gave me an error so I took it out to see how the program would
    run without it. The program ran but then i ran into the above mentioned
    problem. Anyway I'm completely lost and this is way past due for class so any
    help would be appreciated. Here's the code:

    #include <iostream>
    #include <math.h>
    #include<stdio.h>
    using namespace std;
    
    int main()
    {
          int number1;
          int number2;
          int number3;
          int number4;
         float Circle;
         float Rectangle;
         float Triangle;
         int choice;
         double pi;
         float length;
         float width;
          float base;
          float height;
          float radius;
    
    number1 = 1;
    number2 = 2;
    number3 = 3;
    number4 = 4;
    pi = 3.14159;
    Circle = pi * radius * radius;
    Rectangle = length * width;
    Triangle = base * height * .5;
    
    cout << "Geometry Calculator" << endl;
    
    cout << "1. Calculate the Area of a Circle" << endl;
    cout << "2. Calculate the Area of a Rectangle" << endl;
    cout << "3. Calculate the Area of a Triangle" << endl;
    cout << "4. Quit" << endl << endl;
    
    cout << "Enter your choice (1-4):" << endl;
    cin >> choice;
    
    switch(choice)
    { 
           case 1 : cout << "Enter the radius of the circle:" << endl;
           cin >> radius;
           case 2 : cout <<  "Enter the length of the rectangle:" << endl;
         cin >> length;
         cout << "Enter the width of the rectangle:" << endl;
         cin >> width;
         case 3 : cout << "Enter the height of the triangle:" << endl;
         cin >> height;
         case 4 : cout << "Goodbye!" << endl;
         default: cout << "5 is not a valid entry." << endl;
    }
    return 0;
    }
    
     
  • cpns

    cpns - 2010-04-12

    Unlike say Pascal, in C/C++ a case is not terminated by the presence of the
    next case. If you do not want the case to "fall through", you need to
    terminate it with a break statement.

    case 1 : 
        cout << "Enter the radius of the circle:" << endl;
        cin >> radius;
    break ;
    
    case 2 : 
        ...
    break ;
    
    // etc.
    
     
  • laxman varada

    laxman varada - 2010-08-16

    hi,

    I'm trying to run the math equations after the initial selection but I'm not
    sure how to do that. I'm pretty sure i have to somehow link the math equations
    with the selection but when i tried doing that it gave me an error.

    regards,
    phe9oxis,
    http://www.guidebuddha.com

     

Log in to post a comment.