Validity checks using Loops C++

I am trying to compose validity checks using any type of loops on C++. For example, if the user does not input a certain integer or character that fits in a certain range, it will respond back to you saying invalid input and not let you go to the next question until it receives a variable in the proper range. How would I go about doing that, any help will be greatly appreciated, thank.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main () // Initiates the main sequence to allow for coding
{

int empID; // Data in which user will input info that will be have arithmetic done on it and will be displayed 
cout << "empID ";
cin >> empID;
do
{
     cin >> empID;
     cout << "Invalid Input, must be a value between 100 - 800 ";
     
} while (empID < 100 && empID > 800);


This is the top portion of my code and even though it compiles and runs, when it comes to the first output question which is empID, it does not give me the invalid response when I put a integer thats below 100 or above 800, what am I doing wrong? Appreciate any help.
Try writing out your problem in a pseudo code format.

You know you'll have to use a loop, so

1
2
3
4
5
6
7
/*while the user input isn't correct
     user is prompted for input

     if the user input isn't valid,
          print "Invalid input" and restart the loop
     if the input is valid,
          end the loop*/


All you have to do from there is write each line out as code, hope this helps.

---

EDIT:

How you have it, the condition isn't being checked until after the loop has ended. Use an if/else statement to check the condition and print statement before it loops around.
Last edited on
thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main () // Initiates the main sequence to allow for coding
{

int empID; // Data in which user will input info that will be have arithmetic done on it and will be displayed 
cout << "empID ";
cin >> empID;
while (empID < 100 || empID > 800);
{
     cout << "Invalid Input, must be a value between 100 - 800 ";
} 


ok revised the code, am I closer? It still wont work, what am I doing wrong? appreciate the input
Now it checks the user input right away, but you need to add something inside the while loop to change the input so you don't end up with an infinite amount of "Invalid Inputs..." flying at you.

after the cout statement, ask the user for input again.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main () // Initiates the main sequence to allow for coding
{

int empID; // Data in which user will input info that will be have arithmetic done on it and will be displayed 
cout << "empID ";
cin >> empID;
while (empID < 100 || empID > 800)
{
     cout << "Invalid Input, must be a value between 100 - 800 ";
     cout << "empID";
} 
 


ok I did but it now goes berserk (matrix like viewing) if I type an integer thats not in the range
In your while statement you have
 
cout << "empID";


Try using cin instead :) Without letting the value of empID be changed within the loop, whenever you hit an incorrect value, it becomes an infinite loop. This is why you're getting the matrix like display.
Last edited on
cool thanks I fixed it now a new prob, it compiles but when I put H or h, it still gives me an invalid response?

char payrollType;
char H;
char h;
cout << "payrollType ";
cin >> payrollType;
while (payrollType != H || payrollType != h)
{
cout << "Invalid Input, must be either H or h\n";
cout << "payrollType ";
cin >> payrollType;
}
You need to enclose the characters you want to test your char variables against in single quotation marks (if you were testing strings, you would need to enclose the strings in regular quotation marks).

so while (payrollType != H || payrollType != h)

would be while (payrollType != 'H' || payrollType != 'h')
did, still wont work?
Wait, sorry. I see what you did.

you were right in not having the single quotes in your while statement, but with how you have written your code, you will need to initialize your two char variables before the while statement.

1
2
char H = 'H';
char h = 'h';


doing this, you should be able to keep the while statement how it is. Alternatively, you could get rid of the two h variables and change the while statement to how I had it the first time!
did, still keeps giving me invalid...
Could you post the entire updated code again?
1
2
3
4
5
6
7
8
9
10
11
char payrollType;
char H = 'H';
char h = 'h';
cout << "payrollType ";
cin >> payrollType;
while (payrollType != H || payrollType != h)
{
     cout << "Invalid Input, must be either H or h\n";
     cout << "payrollType ";
     cin >> payrollType;
} 
You want a logical AND (&&) inside your while loop condition. The current OR condition will always be true.
Thanks a lot for all the help guyshttp://www.cplusplus.com/forum/beginner/37805/#
Topic archived. No new replies allowed.