Need help with if /else statements - maybe switch

This is a discussion on Need help with if /else statements - maybe switch within the C++ Programming forums, part of the General Programming Boards category; My code compiles and runs but it gives the number of the menu item I've chosen and is not assigning ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    4

    Question Need help with if /else statements - maybe switch

    My code compiles and runs but it gives the number of the menu item I've chosen and is not assigning it a value ie.. square. Each time the variable shapeSelected comes up I need it to say the value of square, circle, etc., not 1, 2, or 3. Hope you understand what I'm trying to say. I know it's something simple and was hoping someone could help me where I'm messing up. Excuse my newbiness to the language, but I'm trying. Thanks for any help.
    __________________________________________________ _
    Code:
    /*  Computes the area of a user selected shape (square, circle, or 
        equilateral triangle) when given any number as a length for the 
        Variable X which is also assigned by the user.
    */
    
    #include<iostream>
    #include<cmath>
    
    using namespace std;
    
    int main ()
    {
        double lengthX;
        double shapeArea;
        int shapeSelected;
        char square;
        char circle;
        char equilTriangle;
    
        cout << "This program will give you the area of the shape slected \n"
             << "(Circle, Square, or Equilateral Triangle) based on any given \n"
             << "length of the Variable X, which you, the user will provide. \n"
             << "\n"
             << "\n";
        cout << "Please enter a length for the Variable X:";
    
        cin >> lengthX;
        cout << "\n";
        
        cout << "1 - Square" << endl;
        cout << "2 - Circle" << endl;
        cout << "3 - Equilateral Triangle" << endl;
        cout << "\n";
        cout << "\n";
        cout << "Please look at the above men and enter the number that \n"
             << "corresponds to the shape you would like to compute the  \n"
             << "area of:";
        cin >> shapeSelected;
    
    
      {
        if      (shapeSelected == 1)
                square = shapeSelected;
        else if (shapeSelected == 2)
                circle = shapeSelected;
        else if (shapeSelected == 3)
                equilTriangle = shapeSelected;
      } 
        
    
    //  Calculates the area of chosen shape.
    
      {
        if      (square == shapeSelected)
                shapeArea = (lengthX * lengthX);
        else if (circle == shapeSelected)
                shapeArea = (3.14) * (lengthX * lengthX);
        else if (equilTriangle == shapeSelected)
                shapeArea = ((sqrt(3)/4)) * (lengthX * lengthX);  
        }      
                                  
        cout << "The length you chose for Variable X is:" << lengthX;
        cout << "\n";
        
        cout << "The shape you selected to get the area of was a/an:" << shapeSelected;
        cout << "\n";
        
        cout << "The area of the" << shapeSelected << "is:" << shapeArea << endl;
      
        
        return 0;
    }

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    nowhere in your code do you specify/assign the word of shape to any variable. By displaying:
    Code:
    cout << "The shape you selected to get the area of was a/an:" << shapeSelected;
    you are only displaying the number from the menu, 1,2, or 3.

    I would have another string in your initial if statements that hold the word of the shape. You cannot put "square" into shapeSelected as it is an int.

    so:

    Code:
    string shapeName;
    //....
    if      (shapeSelected == 1){
                square = shapeSelected;
                shapeName = "square";
    }
    .....
    cout << "The shape you selected to get the area of was a/an:" << shapeName;
    also, you could do everything in the initial if statements, calculations that is...you don't need two.

    axon

    //EDIT:
    you also should do some error checking; what if the user enters 5? on the end of youe if/else statements you should have a default that tells user it is an invalid option and loops him back to the start...or if you did not get so far have an exit()
    Last edited by axon; 09-22-2003 at 12:50 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    I think a good choice could be a array of char * which contains adresses of the char strings you want as the names of your shapes. Actually, I know that we're into C++ but I used to be a C programmer and these methods probably suits me.
    But if you want to use "pure" C++, you should use a map of strings and int as keys.

    Another thing, you should use switches instead of if/else statements.

    Also, I did not understand the use of the square, circle and triangle variables. They are pretty useless serving as constants, don't they?

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    4
    I used yours and "lyx's" suggestions. I changed the code to be a switch instead an if/else statement, although it did all run correctly under a single if/else statement as you suggested. I've included a default error message but I am having trouble figuring out how to end the program end() as you suggested, or performing a loop back to the beginning. I haven't really gotten into loops yet, probably will this week. Any pointers you can give me on ending the program or having it loop back would be appreciated. Again, thank you "and lyx", as I stated, I'm new at this.

    P.S. The new code is below.

    Darrin

    Originally posted by axon
    nowhere in your code do you specify/assign the word of shape to any variable. By displaying:
    Code:
    cout << "The shape you selected to get the area of was a/an:" << shapeSelected;
    you are only displaying the number from the menu, 1,2, or 3.

    I would have another string in your initial if statements that hold the word of the shape. You cannot put "square" into shapeSelected as it is an int.

    so:

    Code:
    string shapeName;
    //....
    if      (shapeSelected == 1){
                square = shapeSelected;
                shapeName = "square";
    }
    .....
    cout << "The shape you selected to get the area of was a/an:" << shapeName;
    also, you could do everything in the initial if statements, calculations that is...you don't need two.

    axon

    //EDIT:
    you also should do some error checking; what if the user enters 5? on the end of youe if/else statements you should have a default that tells user it is an invalid option and loops him back to the start...or if you did not get so far have an exit()
    Code:
    /*  Computes the area of a user selected shape (square, circle, or 
        equilateral triangle) when given any number as a length for the 
        Variable X which is also assigned by the user.
    */
    
    #include<iostream>
    #include<cmath>
    #include<string>
    
    using namespace std;
    
    int main ()
    {
        double lengthX;
        double shapeArea;
        int shapeSelected;
    	string shapeName;
    
     
        cout << "This program will give you the area of the shape slected \n"
             << "(Circle, Square, or Equilateral Triangle) based on any given \n"
             << "length of the Variable X, which you, the user will provide. \n"
             << "\n"
             << "\n";
        cout << "Please enter a length for the Variable X:";
    
        cin >> lengthX;
        cout << "\n";
        
        cout << "1 - Square" << endl;
        cout << "2 - Circle" << endl;
        cout << "3 - Equilateral Triangle" << endl;
        cout << "\n";
        cout << "\n";
        cout << "Please look at the above men and enter the number that \n"
             << "corresponds to the shape you would like to compute the  \n"
             << "area of:";
        cin >> shapeSelected;
    
    
    // Assigns shape selected and performs area calculations
    	
    	
    		switch (shapeSelected)
    		{
    		case 1:
    			shapeSelected = 1;
    			shapeName = "square";
    			shapeArea = (lengthX * lengthX);
    			break;
    	
    		case 2:
    			shapeSelected = 2;
    			shapeName = "circle";
    			shapeArea = (3.14) * (lengthX * lengthX);
    			break;
    	
    		case 3:
    	
    			shapeSelected = 3;
    			shapeName = "equilaterl triangle";
    			shapeArea = ((sqrt(3)/4)) * (lengthX * lengthX);
    			break;
    
    		default: 
    			cout << "The number you entered was invalid.  Please re-run the program.\n";
    			break;
    		}
    	
                          
        cout << "The length you chose for Variable X is: " << lengthX;
        cout << "\n";
        
        cout << "The shape you selected to get the area of was a/an: " << shapeName;
        cout << "\n";
        
        cout << "The area of the " << shapeName << " is: " << shapeArea << endl;
      
        return 0;
    }
    Last edited by dwinslett; 09-22-2003 at 02:52 PM.

  5. #5
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Why do you enclose my nickname into quotation marks? Does it mean anything special?

    Anyway, talking of your code, well, it looks somehow cleaner than before. For you experiencing troubles with termainating or restarting the program, I guess you should read some paper on loops. But it's easy once you know a little about them.
    Or you can use labels, but it is highly criticize so...

    And you should try with an array instead of using a variable which stores the string adress. It is about as fast as the method you're using but it doesn't get you assigning the variable for each case. In the case you do, shapeSelected would be the index of the array.

    Switch is better in that case because you're testing the same variable again and again so the compiler can make an optimized code from that.

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    4

    Thanks lyx

    Doesn't mean anything special having your name in quotaions, just wanted to include you and for some reason put you in quotes. Yeah I know I will be reading up on loops as well as arrays. I'm just gettiing into this stuff in school and have alot ahead of me and these boards come in handy since I work full-time and can't go talk to my professor or a lab assistant. I spent hours last night (was fuzzy because of earlier weekend activities) trying to figure out that simple problem I was having earlier. Actually I'm at work now, just doing a little homework while I'm here.... shh don't tell anyone.

    Anyway...

    Thanks,

    Darrin

  7. #7
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    I was asking because as you can see, I'm French. (well, nationality at least is) I see, so you're some student in programming. ^^ Maybe one day will I study it as well. (I'm currently discovering High School so it's not for now I guess)

  8. #8
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    >>I've included a default error message but I am having trouble
    >>figuring out how to end the program end()

    what compiler are you using? the following code will not work on MSVC++, but does work on GCC. I'm not sure about other compilers. In order for the program to exit you can use exit(0);
    exit() is within the <cstdlib>.

    This should be sufficient for now, as you did not learn about loops yet. But here is a hint if you want to play around with it: have the whole program in a while loop, in you default condition in the switch statement have a continue; and at the end of the program have a question if the user wants to continue.

    Code:
    bool notDone = true;
    
    while(notDone){
         //program body here
        //question at the end to run again?
        if(whatever==yes) notDone = true;
        else{
           notDone = false;
           cout << "exiting......" << endl;
           exit(0);}
    }
    axon
    Last edited by axon; 09-22-2003 at 05:07 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    4
    Thanks Axon. Actually I am using MSVC++ 6.0. I figured out if I put a return 0; after the default error message it would stop there and like the message says, prompt the user to re-run the program. I turned in this assignment and got full credit, now I'm on to the next. Thanks again.

    Darrin

    Originally posted by axon
    >>I've included a default error message but I am having trouble
    >>figuring out how to end the program end()

    what compiler are you using? the following code will not work on MSVC++, but does work on GCC. I'm not sure about other compilers. In order for the program to exit you can use exit(0);
    exit() is within the <cstdlib>.

    This should be sufficient for now, as you did not learn about loops yet. But here is a hint if you want to play around with it: have the whole program in a while loop, in you default condition in the switch statement have a continue; and at the end of the program have a question if the user wants to continue.

    Code:
    bool notDone = true;
    
    while(notDone){
         //program body here
        //question at the end to run again?
        if(whatever==yes) notDone = true;
        else{
           notDone = false;
           cout << "exiting......" << endl;
           exit(0);}
    }
    axon

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Files Inside Switch Statements
    By arealfind08 in forum C Programming
    Replies: 11
    Last Post: 03-17-2009, 05:49 PM
  2. Explanation of switch statements
    By ammochck21 in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2006, 02:59 PM
  3. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 05:44 PM
  4. Switch Statements
    By blackgingr in forum C Programming
    Replies: 3
    Last Post: 10-07-2002, 03:36 PM
  5. Switch statements for strings
    By cxs00u in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 04:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21