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.