Thread: Cannot get my program to exit properly (switch related)

  1. #1
    N00b
    Join Date
    Feb 2005
    Location
    N00bville
    Posts
    7

    Cannot get my program to exit properly (switch related)

    I revamped one of my programs which used numerous "else if" statements.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int addition ( int a, int b );
    
    int substraction ( int c, int d );
    
    int multiplication ( int e, int f);
    
    int division ( int g, int h);
    
    int end ( int k );
    
    int main()
    {
        int i;
        
        int j = 1;//Might be modified at the end.
        
        int k;
        
        while ( j == 1 ) {//Is used to restart the program once a task is complete.
        cout<<"Please choose what type of mathematical operation you want to do:\n 1.Addition\n 2.Substraction\n 3.Multiplication\n 4.Division (might be imprecise)\n Any other number.End program\n";
        cin>> i;
        cin.ignore();
        
        switch ( i ) {
             case 1://This is the addition function.      
             int a;
             
             int b;
                      
             cout<<"Enter the two numbers to be added, pressing enter after each one: ";
             cin>> a >> b;
             cin.ignore();
             cout<<"The result is: "<< addition ( a, b )<<"\n";
             break;
             case 2://This is the substraction function.
             int c;
             
             int d;
             
             cout<<"Enter the two numbers to be substracted, pressing enter after each one: ";
             cin>> c >> d;
             cin.ignore();
             cout<<"The result is: "<< substraction ( c, d )<<"\n";
             break;
             case 3://This is the multiplication section.
             int e;
             
             int f;
             
             cout<<"Enter the two numbers to be multiplicated, pressing enter after each one: ";
             cin>> e >> f;
             cin.ignore();
             cout<<"The result is: "<< multiplication ( e, f )<<"\n";
             break;
             case 4: {//This is the division section.
             int g;
             
             int h;
             
             cout<<"Enter the two numbers to be divided, pressing enter after each one: ";
             cin>> g >> h;
             cin.ignore();
             cout<<"The result is: "<< division ( g, h )<<"\n";
             break;
             default:
                     end ( k );
                     break;
             }      
             }
             }
             cout<<"Thank you for using this program.\n  Press enter to quit.";
             cin.get();
             }
             
             int addition ( int a, int b )
             {
                 return a + b;
                 }
                 
                 int substraction ( int c, int d )
                 {
                     return c - d;
                     }
                     
                     int multiplication ( int e, int f )
                     {
                         return e * f;
                         }
                         
                         int division ( int g, int h )
                         {
                             return g / h;
                             }
                             
                             int end ( int k )
                              {//This variable is used to modify j.
                               int j;
                                   
             cout<<"Would you like to do another operation?  Press 1 if you would like to, otherwise press any number: ";
             cin>> k;
             cin.ignore();
             if ( k == 1) {
                  j = 1;//Translation -> "The loop will start again."
                  }
                  else {
                       j++;//Translation -> "The program will end."
                       }
                       }
    Normally the user should press a number like 2 when asked if he wants to do another operation, and that would send a little message telling him to press enter to close the program. But it simply restarts the loop. What is wrong with this code?

    By the way, sorry for posting the whole code, but I have no clue where the problem is.

  2. #2
    N00b
    Join Date
    Feb 2005
    Location
    N00bville
    Posts
    7
    Alright, my situation has evolved a bit (or devolved, depending on how you see it).

    I changed some parts of the code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int addition ( int a, int b );
    
    int substraction ( int c, int d );
    
    int multiplication ( int e, int f);
    
    int division ( int g, int h);
    
    int again ( int k );
    
    int end ( int j );
    
    int main()
    {
        int i;
        
        int j = 1;
        
        int k;
        
        cout<<"Please choose what type of mathematical operation you want to do:\n 1.Addition\n 2.Substraction\n 3.Multiplication\n 4.Division (might be imprecise)\n Any other number.End program\n";
        cin>> i;
        cin.ignore();
        
        switch ( i ) {
             case 1://This is the addition function.      
             int a;
             
             int b;
                      
             cout<<"Enter the two numbers to be added, pressing enter after each one: ";
             cin>> a >> b;
             cin.ignore();
             cout<<"The result is: "<< addition ( a, b )<<"\n";
             break;
             case 2://This is the substraction function.
             int c;
             
             int d;
             
             cout<<"Enter the two numbers to be substracted, pressing enter after each one: ";
             cin>> c >> d;
             cin.ignore();
             cout<<"The result is: "<< substraction ( c, d )<<"\n";
             break;
             case 3://This is the multiplication section.
             int e;
             
             int f;
             
             cout<<"Enter the two numbers to be multiplicated, pressing enter after each one: ";
             cin>> e >> f;
             cin.ignore();
             cout<<"The result is: "<< multiplication ( e, f )<<"\n";
             break;
             case 4: {//This is the division section.
             int g;
             
             int h;
             
             cout<<"Enter the two numbers to be divided, pressing enter after each one: ";
             cin>> g >> h;
             cin.ignore();
             cout<<"The result is: "<< division ( g, h )<<"\n";
             break;
             default:
                    end (j);
                     break;
             }             
             }
             if ( j == 1 ) {//Used to restart the program.
             again ( k ); 
             }
             else if ( j == 2) {
                  cin.get();
             }
             }
             
             int addition ( int a, int b )
             {
                 return a + b;
                 }
                 
                 int substraction ( int c, int d )
                 {
                     return c - d;
                     }
                     
                     int multiplication ( int e, int f )
                     {
                         return e * f;
                         }
                         
                         int division ( int g, int h )
                         {
                             return g / h;
                             }
                             
                             int again ( int k )
                              {
             cout<<"Would you like to do another operation?  Press 1 if you would like to, otherwise press any number: ";
             cin>> k;
             cin.ignore();
             if ( k == 1) {
                  main();//Translation -> "User will make another operation."
                  }
                  else {
                       int j;
                       
                       end ( j );//Translation -> "The program will end."
                       }
                       cin.get();
                       }
                       
                       int end ( int  j )
                       {
                            cout<<"Thank you for using this program.\n Press enter to quit.";
             j++;
             }
    Normallly, the program should add 1 to j when a user press any number other than 1, 2, or 4 at the start, because the end (j) ends with j++;. Since the again (k); function only runs when j = 1, it should normally not run when j = 2. The problem is, when the user presses 5 for example, he gets prompted with the normal "end" message, but right after that, the again(k); function runs even though j is set to 2.

    What is the problem? Any help would be appreciated.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Ugh, learn how to format!
    No wonder you can't find your way around the code if it looks like that
    Code:
    #include <iostream>
    
    using namespace std;
    
    int addition(int a, int b);
    
    int substraction(int c, int d);
    
    int multiplication(int e, int f);
    
    int division(int g, int h);
    
    int end(int k);
    
    int main()
    {
        int i;
    
        int j = 1;                  //Might be modified at the end.
    
        int k;
    
        while (j == 1) {            //Is used to restart the program once a task is complete.
            cout <<
                "Please choose what type of mathematical operation you want to do:\n 1.Addition\n 2.Substraction\n 3.Multiplication\n 4.Division (might be imprecise)\n Any other number.End program\n";
            cin >> i;
            cin.ignore();
    
            switch (i) {
            case 1:                //This is the addition function.      
                int a;
    
                int b;
    
                cout <<
                    "Enter the two numbers to be added, pressing enter after each one: ";
                cin >> a >> b;
                cin.ignore();
                cout << "The result is: " << addition(a, b) << "\n";
                break;
            case 2:                //This is the substraction function.
                int c;
    
                int d;
    
                cout <<
                    "Enter the two numbers to be substracted, pressing enter after each one: ";
                cin >> c >> d;
                cin.ignore();
                cout << "The result is: " << substraction(c, d) << "\n";
                break;
            case 3:                //This is the multiplication section.
                int e;
    
                int f;
    
                cout <<
                    "Enter the two numbers to be multiplicated, pressing enter after each one: ";
                cin >> e >> f;
                cin.ignore();
                cout << "The result is: " << multiplication(e, f) << "\n";
                break;
            case 4:{               //This is the division section.
                    int g;
    
                    int h;
    
                    cout <<
                        "Enter the two numbers to be divided, pressing enter after each one: ";
                    cin >> g >> h;
                    cin.ignore();
                    cout << "The result is: " << division(g, h) << "\n";
                    break;
            default:
                    end(k);
                    break;
                }
            }
        }
        cout << "Thank you for using this program.\n  Press enter to quit.";
        cin.get();
    }
    
    int addition(int a, int b)
    {
        return a + b;
    }
    
    int substraction(int c, int d)
    {
        return c - d;
    }
    
    int multiplication(int e, int f)
    {
        return e * f;
    }
    
    int division(int g, int h)
    {
        return g / h;
    }
    
    int end(int k)
    {                               //This variable is used to modify j.
        int j;
    
        cout <<
            "Would you like to do another operation?  Press 1 if you would like to, otherwise press any number: ";
        cin >> k;
        cin.ignore();
        if (k == 1) {
            j = 1;                  //Translation -> "The loop will start again."
        } else {
            j++;                    //Translation -> "The program will end."
        }
    }
    > //This variable is used to modify j.
    Except it modifies the local j, not the j in main

    In main, you need to do
    k=end(k);

    And return a value in end(), rather than falling off the end of the function.

    You might want to consider what the brace is doing in case 4
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    N00b
    Join Date
    Feb 2005
    Location
    N00bville
    Posts
    7
    Thanks for the help.

    By learning to format, you mean I should identify my sections better?

    Code:
    ////////////////////////////
    //FUNCTIONS///////////
    ////////////////////////////
    Would doing things like ^this^ help?

    Anyway, since I wanted end (j) to affect the j in the main function, it now looks like this:
    Code:
                       int end ( int  j )
                       {
                            cout<<"Thank you for using this program.\n Creator: Eric Blais, 2005\n Press enter to quit.";
             return 2;
             }
    The default case is now:
    Code:
             default:
                    end (j);
                     j = end (j);
                     break;
    And I removed the braces in case 4.

    It works! The only problem is that the end function runs twice now, before the program closes. -_- Would that be because the bolded line is not written correctly?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    end (j);
    j = end (j);

    Well DUH!!!
    Of course it's called TWICE

    Remove one of them - I recommend the first one.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    N00b
    Join Date
    Feb 2005
    Location
    N00bville
    Posts
    7
    YAY!!! it finally works, thanks.

    I had written it this way because I thought the first end (j); would actually start the function, and then j = end (j); would set the value of the main j to the returned value of end (j), but I had forgotten end (j) didn't only return a value, it also showed some text.

    *Whew* My brain is melting from the work. :/ Back to Starcraft...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Program Terminating With Error On Exit
    By chriscolden in forum C Programming
    Replies: 19
    Last Post: 01-14-2006, 04:40 AM
  3. program won't run properly, help needed asap
    By jlmac2001 in forum C Programming
    Replies: 2
    Last Post: 11-16-2002, 09:52 AM
  4. How to exit the program?
    By bigben in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2002, 09:06 AM
  5. plz hlp me. program not running properly
    By jfl in forum C Programming
    Replies: 5
    Last Post: 02-11-2002, 03:58 PM