Thread: Switch statement problems

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    21

    Switch statement problems

    When the code below is compiled and I choose 1 which is add and when my add function is finished my subtract function starts running even though I don't want it to, help me pleas.

    Code:
    #include <iostream>
    
    int MenuAndMenuChoice();
    double Add();
    double Subtract();
    
    int main()
    {
        
        //Function - MenuAndMenuChoice is called to print the menu and get the user's choice
        switch (MenuAndMenuChoice())
        {
              
              //A descision is then made hear depending on what the user entered
              case 1: std::cout << "\nThe two numbers added together equal: " << Add();
              
              case 2: std::cout << "\nThe second number subtracted from the first number equals: " << Subtract();
              
        }
        
        std::cin.get();
        return 0;
    }
    
    //Function MenuAndMenuChoice code starts here
    int MenuAndMenuChoice()
    {
        
        int MenuChoice;
        
        std::cout << "1.Add\n";
        std::cout << "2.Subtract\n";
        std::cout << "3.Multiply\n";
        std::cout << "4.Divide\n";
        std::cout << "5.Circle Area\n";
        std::cout << "6.Circle Circumference\n";
        std::cout << "7.Exit\n\n";
    
        std::cout << "What would you like to do? ";
        std::cin >> MenuChoice;
        
        return MenuChoice;
        
    }
    
    //Function Add code starts here
    double Add()
    {    
         
        double FirstNumber, SecondNumber, AddAnswer;
         
        std::cout << "\nWhat is the first number: ";
        std::cin >> FirstNumber;
        std::cin.ignore(80,'\n');
        
        std::cout << "What is the second number: ";
        std::cin >> SecondNumber;
        std::cin.ignore(80,'\n');
        
        AddAnswer = FirstNumber + SecondNumber;
        
        return AddAnswer;
        
    }
    
    //Function Subtract code starts here     
    double Subtract()
    {
        
        double FirstNumber, SecondNumber, SubtractAnswer;
        
        std::cout << "\nWhat is the first number: ";
        std::cin >> FirstNumber;
        std::cin.ignore(80,'\n');
        
        std::cout << "What number would you like to take away from the first number: ";
        std::cin >> SecondNumber;
        std::cin.ignore(80,'\n');
        
        SubtractAnswer = FirstNumber - SecondNumber;
        
        return SubtractAnswer;
        
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Common mistake. After each set of statements after a case add a "break;". It will skip to the end of the switch block.

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    switch statements are defined to have fall-through behavior. You need a break statement if you don't want it to go to the next one.
    Code:
        switch (MenuAndMenuChoice())
        {
              //A descision is then made hear depending on what the user entered
              case 1:
                 std::cout << "\nThe two numbers added together equal: " << Add();
                 break;
              
              case 2:
                 std::cout << "\nThe second number subtracted from the first number equals: " << Subtract();
                 break;
        }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    21
    Thanks, I'm back on track but another question. How do I get my program to quit when 7 is entered.
    Last edited by ComDriver; 02-21-2005 at 11:03 AM.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    look up exit(); I think it's in cstdio
    You're only born perfect.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    If you're in main, the simpler solution is to simply return 0. Once you return a value the function stop executing and control returns to the parent function, or, in this case, the Operating System.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting the switch statement to work.
    By mtymightymike in forum C Programming
    Replies: 7
    Last Post: 10-15-2008, 06:32 PM
  2. Stack operations from switch statement elements
    By mlsrar in forum C Programming
    Replies: 15
    Last Post: 10-02-2008, 01:12 PM
  3. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:17 PM
  4. switch problems
    By ckeener in forum C++ Programming
    Replies: 5
    Last Post: 05-17-2005, 06:56 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM