-1 to exit program ?

This is a discussion on -1 to exit program ? within the C++ Programming forums, part of the General Programming Boards category; Hello ALL, I know this is simple but I been working on this file for two days. I think I ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    90

    -1 to exit program ?

    Hello ALL,

    I know this is simple but I been working on this file for two days. I think I did fairly well up to the point where I completely when brain dead. I just did did something like this 3 weeks ago byt now I'm complely lost. I think I could have over did it for such that should be a simple operation i guest.

    If the user enter "-1" the programs is suppose to exit from any point. I'm so deep down in the program that my only hope to exit is at the END of the program with -1 if i'm lucky. Could someone show me how to correct this.

    "-1 to exit"


    Code:
    #include <iostream>
    using namespace std;
    
    void compare_1 (int a);
    void tryAgain (int a);
    
    void main_2 (int a);
    void compare_2 (int a);
    void tryAgain_2 (int a);
    
    void main_3 (int a);
    void compare_3 (int a);
    void tryAgain_3 (int a);
    
      int num1;
      int num2;
    
    int main ()
    {
    int x=3,y=2;
    num1 = x * y;
                cout << num1 << endl;
                    do {
                cout << "How much is 3 x 2 (-1 to exit): " << endl;
                                                            cout <<"? ";
                cin >> num2;
                compare_1 (num2);
    
      } while (num2==-1);
    
    cout << "That's all for now. Have a nice day! " << endl << endl;
    
      return 0;
    }
    //  ................................................         1 1 1 1
    //  ................................................
    void compare_1 (int a)
     {
                    if (num1 == num2)
                  {
                    cout <<" Very good! " <<endl <<endl;
                    main_2 (a);
                  }
                    else tryAgain (a);
    }
    
    void tryAgain (int a)
    {
      cout << "Try again.\n";
      main();
    }
    //  ................................................         2 2 2 2
    //  ................................................
    void main_2 (int a)
    {
    int x=3,y=0;
    num1 = x * y;
    
    cout << num1 << endl;
      do {
        cout << "How much is 3 x 0 (-1 to exit): " << endl;
                                                            cout <<"? ";
        cin >> num2;
        compare_2 (num2);
    
      } while (num2!=0);
    //  return 0;
    }
    
    
    void compare_2 (int a)
     {
                    if (num1 == num2)
                  {
                    cout <<" Very good! " <<endl <<endl;
                    main_3 (a);
                  }
                    else tryAgain_2 (a);
    }
    
    void tryAgain_2 (int a)
    {
      cout << "Try again.\n";
      main_2(a);
    }
    //  ................................................         3 3 3 3
    //  ................................................
    void main_3 (int a)
    {
    int x= -1,y= -0;
    num1 = x * y;
    
    cout << num1 << endl;
      do {
        cout << "How much is 1 x 0 (-1 to exit): " << endl;
                                                            cout <<"? ";
        cin >> num2;
        compare_3 (num2);
    
      } while (num2!=0);
    
    //  return 0;
    }
    
    void compare_3 (int a)
     {
                    if (num1 == num2)
                  {
                    cout <<" Very good! " <<endl <<endl;
                  }
                    else tryAgain_3 (a);
    }
    
    void tryAgain_3 (int a)
    {
      cout << "Try again.\n";
      main_3(a);
    }
    I could almost swear I had this down to a science just weeks ago. Goes to show, never mis-place or loss your work or examples. I'll be careful for now on! Too many OS's all at the same time and everything else to learn for.

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55
    ...indentation could do with improving.

    You need to check the path/execution of your code. Looks like it is never able to reach
    while (num2==-1);
    which is why it can't exit.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    One thing you need to understand about loops. Using
    Code:
    do
    {
        do {
            cout << "How much is 3 x 0 (-1 to exit): " << endl;
            cout <<"? ";
            cin >> num2;
            compare_2 (num2);
      } while (num2!=0);
    doesn't mean that the code in the loop stops executing as soon as num2 becomes != 0. That conditional is only hit and evaluated at the end of each iteration of the loop. If you want to break out of the loop as soon as -1 is entered, you must evaluate num2 immediately after it's entered by the user and do so.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why do you have the exact same functions 3 times with different names? One of each is all you need.
    Also, DO NOT call main() in your code, ever. main() is a special function that is not intended to be called by you.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    I just submitted the code below. It's a little better and I think it works. This was not a easy thing to do. I had to resort to stop using the brain and went gun-ho on trial and error. So many re-do's, I all most lost the one that worked, if it works.

    cpjust, I got rid of the call back to MAIN and made a procedure for it but it's hard not to have three of the same type functions to complete such instructions. Recursion could have been a better choice for such an operation. I hope soon that you or someone will post the best way of doing such a thing. I would really like to see how code for completing these type steps should have been written. Thanks cpjust
    ....
    ....
    You made your point loud and clear to me so I re-read about WHILE and DO-WHILE. I like the use of [DO] now that I have a better understanding of how it works. Thanks a lot rags_to_riches
    ....
    ....
    Thanks Tropod, I will be working on my indentation habits, but I like pushing some parts of the code far out so when I need to go back I catch the point I made 100x quicker.


    My first masterpiece :

    Code:
    #include <iostream>
    using namespace std;
    
    void do_Multiplcation_1();
    void do_Multiplcation_2();
    void do_Multiplcation_3();
    
    void compare_1 (int a);
    void compare_2 (int a);
    void compare_3 (int a);
    
      int num1;
      int num2;
    
    // ........
    // ........
    
    int main ()
    {                do {
    do_Multiplcation_1();
    
    { if (num2 != -1)
                  {
                compare_1 (num2);  }}
    // ..............................
      } while (num2 != -1);
    cout << "That's all for now. Have a nice day! " << endl << endl;
    return 0;
      }
    //  ................................................         1 1 1 1
    //  ................................................
    void do_Multiplcation_1()
    {
    int x=3,y=2;
    num1 = x * y;
    
    cout << "How much is 3 x 2 (-1 to exit): " << endl;
        cout <<"? ";
        cin >> num2;
    }
    void compare_1 (int a)
     {
                    if (num1 == num2)
                  {
                    cout <<" Very good! " <<endl <<endl;
                    do_Multiplcation_2();
                  }
                    else cout << "Try again.\n";  }
    //  ................................................         2 2 2 2
    //  ................................................
    void do_Multiplcation_2()
    {
    int x=3,y=0;
    num1 = x * y;
    
    cout << "How much is 3 x 0 (-1 to exit): " << endl;
        cout <<"? ";
        cin >> num2;
     if (num2 != -1)
                  {
                compare_2 (num2);  }}
    
    void compare_2 (int a)
     {
                    if (num1 == num2)
                  {
                    cout <<" Very good! " <<endl <<endl;
                    do_Multiplcation_3();
                  }
                    else cout << "Try again2.\n";  }
    //  ................................................         3 3 3 3
    //  ................................................
    void do_Multiplcation_3()
    {
    int x=1,y=0;
    num1 = x * y;
    
    cout << "How much is 1 x 0 (-1 to exit): " << endl;
        cout <<"? ";
        cin >> num2;
     if (num2 != -1)
                  {
                compare_3 (num2);  }}
    
    void compare_3 (int a)
     {
                    if (num1 == num2)
                  {
                    cout <<" Very good! " <<endl <<endl;
                    do_Multiplcation_3();
                  }
                    else cout << "Try again2.\n";
    }
    Last edited by sharris; 10-17-2010 at 11:34 PM.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    I wouldn't call it a masterpiece until it's properly indented. Right now, it's a mess.
    I must stress the importance of indentation to you. It is an absolute requirement. Think of it always when writing your code and don't skimp on it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by sharris View Post
    I just submitted the code below. It's a little better and I think it works. This was not a easy thing to do. I had to resort to stop using the brain and went gun-ho on trial and error. So many re-do's, I all most lost the one that worked, if it works.

    cpjust, I got rid of the call back to MAIN and made a procedure for it but it's hard not to have three of the same type functions to complete such instructions. Recursion could have been a better choice for such an operation. I hope soon that you or someone will post the best way of doing such a thing. I would really like to see how code for completing these type steps should have been written. Thanks cpjust

    You made your point loud and clear to me so I re-read about WHILE and DO-WHILE. I like the use of [DO] now that I have a better understanding of how it works. Thanks a lot rags_to_riches

    Thanks Tropod, I will be working on my indentation habits, but I like pushing some parts of the code far out so when I need to go back I catch the point I made 100x quicker.
    I'm going to give you your program, re-written to be a lot cleaner and less clunky. You were heading in the right direction, but I think your thought process wasn't quite grasping what you needed to do. As an excersize, I've left you a couple places where the program needs improvements - and it also won't compile straight away (there's a hidden bug or two). Just pay attention to the code and you should spot what needs to be 'fixed'.

    Heavily commented to try to help you figure out what's going on.

    Code:
    #include <iostream>
    //using namespace std; -- NO NO NO. Never do this. It pollutes the global namespace.
    
    int do_Multiplication(int first, int second);
    
    // Using global variables is usually a bad idea.
    // Only use them when there's no other option.
    
    int main()
    {
    	int first[3] = {3, 3, 1};
    	int second[2] = {2, 0, 0};
    
    	// FIXME: This is a fixed length loop.
    	// Can you write it in such a way that it can be any length?
    	for (int x = 0; x < 3; ++x)
    	{
    		// You can avoid globals by passing in parameters
    		// and using return values wisely.
    		int ret = do_Multiplication(first[x], second[x]);
    
    		if (ret != -1) // Check if the user wants to exit.
    		{
    			// compare(), inlined.
    			if (ret = (first[x] * second[x]))
    				std::cout << "\nVery good!\n" << std::endl;
    			else
    			{
    				std::cout << "\nTry again.\n" << std::endl;
    				--x;
    			}
    		}
    		else
    			break; // Here's where we stop execution if -1 is entered.
    	}
    
    	std::cout << "\nThat's all for now. Have a nice day!\n" << std::endl;
    
    	std::cin.ignore(); // Flush the extra \n from the buffer.
    	std::cin.get(); // Pause execution until keypress.
    	
    	return(0);
    }
    
    int do_Multiplication(int first, int second)
    {
    	int retval;
    
    	std::cout << "How much is " << first << " x " << second << "? (-1 to exit): ";
    
    	// FIXME: There's no error checking here to make sure a valid number was entered.
    	// In a well-formed application, there would be. As an excersize for you,
    	// write error checking code. This is also known as, "Never trust user input."
    	std::cin >> retval;
    
    	return(retval);
    }

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    Quote Originally Posted by sharris View Post
    If the user enter "-1" the programs is suppose to exit from any point. I'm so deep down in the program that my only hope to exit is at the END of the program with -1 if i'm lucky. Could someone show me how to correct this.
    Had you wrote an input function for integers in this program, you could have called in in such a way that if -1 is the resulting number, then main() returns. A similar and very ugly alternative, is to write the input routine but treat -1 as an exception, and throw an uncaught exception. It's classified as ugly because bad conditions that you reasonably expect to occur, such as this one, should not be treated as an exception but as a return code.

    I hope this answers your questions.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    I'll be working hard on this as I study for mid-term this week, using it as my #1 guide and will post the working code based on Lithorien example, with the readability that correct indentation has to offer. This example will take me far. Can't wait to get to it. Thanks everybody

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP with Program!
    By afnitti in forum C Programming
    Replies: 9
    Last Post: 04-15-2009, 08:06 PM
  2. exit a program at any point.
    By slightofhand in forum C Programming
    Replies: 5
    Last Post: 03-02-2008, 08:08 AM
  3. Program Terminating With Error On Exit
    By chriscolden in forum C Programming
    Replies: 19
    Last Post: 01-14-2006, 03:40 AM
  4. Program uses a lot of memory and doesnt exit properly
    By TJJ in forum Windows Programming
    Replies: 13
    Last Post: 04-28-2004, 03:13 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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