Switch-case help

This is a discussion on Switch-case help within the C++ Programming forums, part of the General Programming Boards category; Hello all, New to the forum and new to C++... Sorry if I sound like I'm a noob but I ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    6

    Question Switch-case help

    Hello all,

    New to the forum and new to C++... Sorry if I sound like I'm a noob but I am... I just started playing with C++ yesterday and am following the tutorials on this site. I'm having a little problem with the default part in the switch. It doesn't seem to work and just makes an infinite loop. Any help would be appreciated! Also any input on making the code more efficient or any input at all would be very helpful! Thanks!

    Here's my code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int x;
    int y;
    int ans;
    char cont;
    
    int add ();
    int subtract ();
    int multiply ();
    int divide ();
    
    int main()
    {
        do {
           int input;
        
           cout<<"1. Add two numbers\n";
           cout<<"2. Subtract two numbers\n";
           cout<<"3. Multiply two numbers\n";
           cout<<"4. Divide two numbers\n";
           cout<<"Selection: ";
           cin>> input;
           switch ( input ) {
                      case 1:
                        add ();
                        break;
                        case 2:
                        subtract ();
                        break;
                      case 3:
                        multiply ();
                        break;
                      case 4:
                        divide ();
                        break;
                      default:
                        cout<<"Error, bad input\n";
                        break;
           }
           cout<<"Your answer is : "<< ans <<"\n";
           cin.get();
           cout<<"Would you like to continue? (y/n)\n";
           cin>> cont;
           cin.ignore();
        } while ( cont != 'n' );
    }
    
    int add () {
             cout<<"Please enter 2 numbers: \n";
             cin>> x >> y;
             ans = x + y;
             cin.get();
    }
    
    int subtract () {
             cout<<"Please enter 2 numbers: \n";
             cin>> x >> y;
             ans = x - y;
             cin.get();
    }
    
    int multiply () {
             cout<<"Please enter 2 numbers: \n";
             cin>> x >> y;
             ans = x * y;
             cin.get();
    }
             
    int divide () {
             cout<<"Please enter 2 numbers: \n";
             cin>> x >> y;
             ans = x / y;
             cin.get();
    }
    It's an improvise of the code on http://www.cprogramming.com/tutorial/lesson5.html. Don't know what the problem is. =/

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6
    I think I figured out my problem. At first I inputed a letter when it asked for a selection. I guess since I declared it as an int then it has to be a number. Any suggestions on how to bypass the
    Code:
    cout<<"Your answer is : "<< ans << endl;
    part? Is there like end or exit function?

    Sorry... noob here....

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    try declaring your x and y int variables locally in your functions. I will give you one example:

    Code:
    int add ()
     {
            int x;
            int y;
    
             cout<<"Please enter 2 numbers: \n";
             cin>> x >> y;
             ans = x + y;
             cin.get();
    
             return ans;     //add( ) has been prototyped to return an int
    }

    also, don't forget to have return statements for all functions that are not prototyped as being void.




    Also, based on how you got the switch/case structure set up.. you'll probably have to do something like this:
    Code:
    int the_answer;
    
     switch ( input ) 
    {
                      case 1:   the_answer = add ();    //This call to add( ) will resolve to an integer
                                 break;
    
    etc...
    because your functions have return values, you'll want to save the return value so you can use it when you display your final result.
    Last edited by The Brain; 06-19-2005 at 12:28 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    The reason why it might be going into an infinite loop is if you are inputing non integer values or non whole numbers, e.g 2.35

    A way to overcome this would be to declare int x,y and ans as a float.

    Code:
    float x;
    float y;
    float ans;

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Any suggestions on how to bypass the
    Code:
    cout<<"Your answer is : "<< ans << endl;
    part?
    Delete it.

    But, then what's the point of performing a calculation if you don't show the user the answer?

    None of your add(), mutiply(), etc. functions will work because the 'ans' variable is not recognized inside the functions. Generally, a variable has to be declared inside a block:
    Code:
    {
    
         //code
    
    }
    for it to be known inside the block. All of your functions contain blocks of code surrounded by braces, yet 'ans' is not declared inside those blocks.
    Last edited by 7stud; 06-19-2005 at 01:48 PM.

  6. #6
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    None of your add(), mutiply(), etc. functions will work because the 'ans' variable is not recognized inside the functions. Generally, a variable has to be declared inside a block:
    You have a serious problem if you can't see that that is wrong (or not if I'm looking at his code after he made a correction to that effect).

    You guys sure aren't good at solving problems; it has nothing to do with if his variables are static external linkage or not - see the bolded red stuff

    Code:
    #include <iostream>
    
    using namespace std;
    
    int x;
    int y;
    int ans;
    char cont;
    
    int add();
    int subtract();
    int multiply();
    int divide();
    
    int main()
    {
        do 
        {
            int input;
        
            cout<<"1. Add two numbers\n";
            cout<<"2. Subtract two numbers\n";
            cout<<"3. Multiply two numbers\n";
            cout<<"4. Divide two numbers\n";
            cout<<"Selection: ";
            cin>> input;
            while(!cin)
            {
                cin.clear();
                cin.ignore();
                cout<<"Selection: ";
                cin>>input;
            }
            switch ( input ) 
            {
                case 1:
                    add();
                break;
                case 2:
                    subtract();
                break;
                case 3:
                    multiply();
                break;
                case 4:
                    divide();
                break;
                default:
                    cout<<"Not a choice."<<endl;
                    return 0;        
            }
            cout<<"Your answer is : "<< ans <<"\n";
            cin.get();
            cout<<"Would you like to continue? (y/n)\n";
            cin>> cont;
            cin.ignore();
        } while( cont != 'n' );
    }
    
    int add() 
    {
        cout<<"Please enter 2 numbers: \n";
        cin>> x >> y;
        ans = x + y;
        cin.get();
    }
    
    int subtract() 
    {
        cout<<"Please enter 2 numbers: \n";
        cin>> x >> y;
        ans = x - y;
        cin.get();
    }
    
    int multiply() 
    {
        cout<<"Please enter 2 numbers: \n";
        cin>> x >> y;
        ans = x * y;
        cin.get();
    }
             
    int divide () 
    {
        cout<<"Please enter 2 numbers: \n";
        cin>> x >> y;
        ans = x / y;
        cin.get();
    }
    Last edited by homeyg; 06-19-2005 at 08:32 PM.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6

    Smile

    Here's what I have:
    Code:
    #include <iostream>
    #include <stdlib.h> 
    
    using namespace std;
    
    float add ();
    float subtract ();
    float multiply ();
    float divide ();
    char cont;
    
    int main()
    {
        do {
           int input;
           float answer;
           
           system ("cls");
           cout<<"1. Add two numbers\n";
           cout<<"2. Subtract two numbers\n";
           cout<<"3. Multiply two numbers\n";
           cout<<"4. Divide two numbers\n";
           cout<<"Selection: ";
           cin>> input;
           switch ( input ) {
                      case 1:
                        answer = add ();
                        break;
                      case 2:
                        answer = subtract ();
                        break;
                      case 3:
                        answer = multiply ();
                        break;
                      case 4:
                        answer = divide ();
                        break;
                      default:
                        cout<<"Error, bad input\n";
                        cont = 'n';
                        break;
           }
           while ( cont != 'n' ) {
                 cout<<"Your answer is : "<< answer << endl;
                 cin.get();
                 cont = 'n';
           }
           cout<<"Would you like to continue? (y/n)\n";
           cin>> cont;
           cin.ignore();
        } while ( cont != 'n' );
    
        return 0;
        
    }
    
    float add () {
             float x;
             float y;
             float ans;
             cout<<"Please enter 2 numbers: \n";
             cin>> x >> y;
             cin.get();
             ans = x + y;
             return ans;
    }
    
    float subtract () {
             float x;
             float y;
             float ans;
             cout<<"Please enter 2 numbers: \n";
             cin>> x >> y;
             cin.get();
             ans = x - y;
             return ans;
    }
    
    float multiply () {
             float x;
             float y;
             float ans;
             cout<<"Please enter 2 numbers: \n";
             cin>> x >> y;
             cin.get();
             ans = x * y;
             return ans;
    }
             
    float divide () {
             float x;
             float y;
             float ans;
             cout<<"Please enter 2 numbers: \n";
             cin>> x >> y;
             cin.get();
             ans = x / y;
             return ans;
    }
    I think it works out ok... Any input?

    I thought that it would be a lot easier to declare the variables x, y, and ans globally instead of declaring it multiple times within the function... but I changed it cause of the recommendations...
    Last edited by devilboybf; 06-19-2005 at 11:25 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Look at each of those functions. Do you see something in common in all of them? Let me help:
    Code:
             float x;
             float y;
             float ans;
             cout<<"Please enter 2 numbers: \n";
             cin>> x >> y;
             cin.get();
    What if you were to put all of that before you call the switch? Then, pass both x and y to your math functions, and you get something like this:
    Code:
    float add( float x, float y )
    {
        return x + y;
    }

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 09:42 AM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 01:07 AM

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