Thread: Implement a loop into my code?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    3

    Implement a loop into my code?

    Hey Guys and Girls,

    I have been working my way through the excellent beginners tutorials on this website, and decided to put some of what I have learned to the test.

    Here is what I have done so far:
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()                                  // Most important part of the program!
    {
    
    
      int a, b, input;                                                 // Sets intergers 'a' , 'b' and 'input'
    
    
      cout<<"Please input value for a: ";
      cin>> a;                                                  // remembers value for a
      cin.ignore();                                             //ignores the enter
         if ( a <= 100 )  {                               //if (a < or = 100 ) = TRUE then proceed with function
         cout<<"Please input value for b: ";
         cin>> b;
         cin.ignore();
         if ( b >= 100 ) {                                  //If (b > or = 100) = TRUE then proceed
             cout<<"1. Addition\n";
             cout<<"2. Subtraction\n";
             cout<<"3. Multiplication\n";
             cout<<"4. Divide\n";
             cout<<"Selection: ";
             cin>> input;
    
    
             switch ( input ) {
             case 1:                                // Note the colon, not a semicolon
                cout << " \n";
                cout << "a + b = ";
                cout << (a + b);
                cout << " \n";
                cout << " \n";
                break;
             case 2:
                cout << " \n";
                cout << "a - b = ";
                cout << (a - b);
                cout << " \n";
                cout << " \n";
                break;
             case 3:
                cout << " \n";
                cout << "a x b = ";
                cout << (a * b);
                cout << " \n";
                cout << " \n";
                break;
             case 4:
                cout << " \n";
                cout << "b / a = ";
                cout << (b / a);
                cout << " \n";
                cout << " \n";
                break;
             default:
                cout<<"Error, bad input, quitting\n";
                break;
             }
         }
         else    {                                               
            cout<<"Please input a value greater than 100 for b: ";
            cin>> b;
            cin.ignore();
            if ( b >= 100)
             cout<<"1. Addition\n";
             cout<<"2. Subtraction\n";
             cout<<"3. Multiplication\n";
             cout<<"4. Divide\n";
             cout<<"Selection: ";
             cin>> input;
    
    
             switch ( input ) {
             case 1:            
                cout << " \n";
                cout << "a + b = ";
                cout << (a + b);
                cout << " \n";
                cout << " \n";
                break;
             case 2:            
                cout << " \n";
                cout << "a - b = ";
                cout << (a - b);
                cout << " \n";
                cout << " \n";
                break;
             case 3:            
                cout << " \n";
                cout << "a x b = ";
                cout << (a * b);
                cout << " \n";
                cout << " \n";
                break;
             case 4:            
                cout << " \n";
                cout << "b / a = ";
                cout << (b / a);
                cout << " \n";
                cout << " \n";
                break;
             default:            
                cout<<"Error, bad input, quitting\n";
                break;
         }
    
    
              cin.get();
         }
         }
     else if ( a >= 100) {
            cout<<"Please input a value less than 100 for a: ";
            cin>> a;
            cin.ignore();
         if ( a <= 100 ) {
            cout<<"Please input a value for b: ";
            cin>> b;
            cin.ignore();
         if ( b >= 100 ) {
            cout<<"1. Addition\n";
            cout<<"2. Subtraction\n";
            cout<<"3. Multiplication\n";
            cout<<"4. Divide\n";
            cout<<"Selection: ";
            cin>> input;
    
    
                switch ( input ) {
                case 1:            
                    cout << " \n";
                    cout << "a + b = ";
                    cout << (a + b);
                    cout << " \n";
                    cout << " \n";
                    break;
                case 2:
                    cout << " \n";
                    cout << "a - b = ";
                    cout << (a - b);
                    cout << " \n";
                    cout << " \n";
                    break;
                case 3:
                    cout << " \n";
                    cout << "a x b = ";
                    cout << (a * b);
                    cout << " \n";
                    cout << " \n";
                    break;
                case 4:
                    cout << " \n";
                    cout << "b / a = ";
                    cout << (b / a);
                    cout << " \n";
                    cout << " \n";
                    break;
                default:
                    cout<<"Error, bad input, quitting\n";
                    break;
             }
         }
         else    {                                              
            cout<<"Please input a value greater than 100 for b: ";
            cin>> b;
            cin.ignore();
            if ( b >= 100)
             cout<<"1. Addition\n";
             cout<<"2. Subtraction\n";
             cout<<"3. Multiplication\n";
             cout<<"4. Divide\n";
             cout<<"Selection: ";
             cin>> input;
    
    
             switch ( input ) {
             case 1:            
                cout << " \n";
                cout << "a + b = ";
                cout << (a + b);
                cout << " \n";
                cout << " \n";
                break;
             case 2:
                cout << " \n";
                cout << "a - b = ";
                cout << (a - b);
                cout << " \n";
                cout << " \n";
                break;
             case 3:
                cout << " \n";
                cout << "a x b = ";
                cout << (a * b);
                cout << " \n";
                cout << " \n";
                break;
             case 4:
                cout << " \n";
                cout << "b / a = ";
                cout << (b / a);
                cout << " \n";
                cout << " \n";
                break;
             default:
                cout<<"Error, bad input, quitting\n";
                break;
         }
    
    
              cin.get();
             }
         }
     }
    }
    At the moment, if a value greater than 100 is entered for integer 'a' (or less than 100 for integer 'b'), then the program will specifically ask for a value less than 100 (for a) or greater than 100 (for b) and if a false value is entered again the program terminates.

    What I want to know is how (if it is possible) to implement a loop so that the program will keep asking for a value less than/greater than 100 until an appropriate value is entered, rather than terminating the program.

    Thanks for your time

    P.S. sorry for my messy code and noob questions still getting used to this... but enjoying.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Good job...(if you've done it yourself)

    What I want to know is how (if it is possible) to implement a loop so that the program will keep asking for a value less than/greater than 100 until an appropriate value is entered, rather than terminating the program.
    Well... the possibility is only limited by your imagination.

    Here is an example of what you seem to want:
    Edit: (iMalc posted a more foolproof example...in the next post)
    Code:
    while(true)
    {
        cin>>n;
        if(n>100)
            cout<<"Your error message"<<std::endl;
        else break;
    }
    Also..you could chain your cout statements....that would make the code less messy.
    Last edited by manasij7479; 11-26-2011 at 01:29 AM.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Sure! One easy way would be:
    Code:
        do {
            cout << "Please input a value less than 100 for a: ";
        } while (!(cin >> a) || a >= 100)
    This also makes sure that if non-numeric data is entered then it asks again.

    Did you mean to allow them to type in negatives though? -469 is less than 100...

    Also, I would recommend putting that switch statement into its own function and then just calling it from the various places rather than duplicating the code. This way your code is shorter and you only have to change one place to change it for all.

    Oh and welcome to the forums!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    3
    Awesome, thanks manasiji7479 and iMalc for replying so quickly that is exactly what I was after.

    @iMalc
    Putting the code into its own function is a smart idea . Should have thought of that...

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    3

    Solved

    Thanks again to both of you for your help, with that little bit of information I was able to figure out how to fix my code into exactly what I wanted:

    Finished Product

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int a, b, input;                                                    // Sets intergers 'a' , 'b' and 'input'
    
    
    void options()                                                      //Adds a new function called options.
    {
             cout << "\n";
             cout<<"1. Addition\n";
             cout<<"2. Subtraction\n";
             cout<<"3. Multiplication\n";
             cout<<"4. Divide\n";
             cout<<"5. New numbers without restrictions\n";
             cout << "\n";
             cout<<"Selection: ";
             cin>> input;
    
    
             switch ( input ) {
             case 1:                                                    // Note the colon, not a semicolon
                cout << " \n";
                cout << "a + b = ";
                cout << (a + b);
                cout << "\n";                                           //Creates a blank space or gap so that it is easier to read in the command prompt.
                cout << "Therefore:\n";
                cout << a << " + " << b << " = " << (a + b);
                cin >> a;
                cin >> b;
                cout << " \n";
                cout << " \n";
                break;
             case 2:
                cout << " \n";
                cout << "a - b = ";
                cout << (a - b);
                cout << "\n";
                cout << "Therefore\n";
                cout << a << " - " << b << " = " << (a - b);
                cin >> a;
                cin >> b;
                cout << " \n";
                cout << " \n";
                break;
             case 3:
                cout << " \n";
                cout << "a x b = ";
                cout << (a * b);
                cout << "\n";
                cout << "Therefore\n";
                cout << a << " x " << b << " = " << (a * b);
                cin >> a;
                cin >> b;
                cout << " \n";
                cout << " \n";
                break;
             case 4:
                cout << " \n";
                cout << "b / a = ";
                cout << (b / a);
                cout << "\n";
                cout << "Therefore:\n";
                cout << a << " / " << b << " = " << (a / b);
                cin >> a;
                cin >> b;
                cout << " \n";
                cout << " \n";
                break;
             case 5:                                                        //Allows the user to add new numbers without the
                cout << "\n";                                               // < or = 100 and > or = 100 constraints.
                cout << "Input new number for a: \n";
                cout << "\n";
                cout << "\n";
                cin >> a;
                cin.ignore();
                cout << "\n";
                cout << "Input new number for b: \n";
                cout << "\n";
                cout << "\n";
                cin >> b;
                cin.ignore();
                options();
                break;
             default:
                cout << "\n";
                cout<<"Error, Try Again\n";
                cout << "\n";
                cout << "\n";
                break;
             }
    }
    
    
    void start()                                //Creates another new function.
    {
        do
        {
          options();                            //Starts the options function (previous function)
        } while (a = a);                        // when a = a, thus creating a never ending loop.
    
    
    
    
    }
    
    
    int main()                                  // Most important part of the program!
    {
    
    
    
    
      cout<<"Please input value for a: ";
      cin>> a;                                                  // remembers value for a
      cin.ignore();                                             //ignores the enter
         if ( a <= 100 )  {                               //if (a < or = 100 ) = TRUE then proceed with function
         cout<<"Please input value for b: ";
         cin>> b;
         cin.ignore();
         if ( b >= 100 ) {                                  //If (b > or = 100) = TRUE then proceed
           options();
           start();
         }
         else    {
            do{
            cout << "Please input a value greater than 100 for b: ";
            } while (!(cin >> b) || b < 100);                                         //Will continue to ask for a value greater than 100 for b, until one is input.
            cin.ignore();
            options();                                  //Starts the options function.
            start();                                    //Restarts the options function, causing a never ending loop.
    
    
         }
         }
     else if ( a >= 100) {
         do{
            cout << "Please input a value less than 100 for a: ";
            } while (!(cin >> a) || a > 100);
            cin.ignore();
         if ( a <= 100 ) {
            cout<<"Please input a value for b: ";
            cin>> b;
            cin.ignore();
         if ( b >= 100 ) {
            options();
         }
         else    {
            do{
                cout << "Please input a value greater than 100 for b: ";
                } while (!(cin >> b) || b < 100);
                cin.ignore();
                options();
             }
            }
           }
          }
    It's quite alot shorter now that I incorporated functions . Thanks again.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Don't use recursion like that. It will use up stack space untill your program crashes. Sorta like a memory leak.
    Instead of this:
    Code:
    void func(){
      ...
      if(something)
         func();
    }
    use loops. Such as:
    Code:
    void func(){
      do{
        ...
      }while(!something);
    }
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You also need to work on your indentation.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop code problem
    By Laika1986 in forum C Programming
    Replies: 12
    Last Post: 10-19-2011, 06:04 PM
  2. trying to implement this code in c
    By centenial in forum C Programming
    Replies: 6
    Last Post: 02-16-2010, 06:56 PM
  3. Replies: 9
    Last Post: 11-12-2007, 03:29 PM
  4. End of Code Loop Question
    By JuanSverige in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 10:35 AM
  5. how to loop back for this code ?
    By Jasonymk in forum Windows Programming
    Replies: 3
    Last Post: 02-28-2003, 12:40 PM