# Thread: Implement a loop into my code?

1. ## 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<<"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:
break;
}
}
else    {
cout<<"Please input a value greater than 100 for b: ";
cin>> b;
cin.ignore();
if ( b >= 100)
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:
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<<"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:
break;
}
}
else    {
cout<<"Please input a value greater than 100 for b: ";
cin>> b;
cin.ignore();
if ( b >= 100)
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:
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.

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

2. 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)
else break;
}```
Also..you could chain your cout statements....that would make the code less messy.

3. 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!

4. 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. ## 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<<"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. Don't use recursion like that. It will use up stack space untill your program crashes. Sorta like a memory leak.
Code:
```void func(){
...
if(something)
func();
}```
use loops. Such as:
Code:
```void func(){
do{
...
}while(!something);
}```

7. You also need to work on your indentation.