472,802 Members | 1,357 Online

# Validating User Inputs

Basically i have this task to get 2 inputs stored into num1 and num2 of int type. The program will then list all the possible numbers from 0 to num1, which are divisable by num2.

So if my input is 10 and 2
my output should be 2 4 6 8.

the problem now is that if i type in negative value or a character i won't get a correct output.

so i came up with this validation :

Expand|Select|Wrap|Line Numbers
1. if(num1 < 0 || num1==NULL){
2. printf("enter a number greater than 0");
3. return 0;
4. }
5.
which is not efficient at all. cause instead of re-prompting for another input of num1, it just exits. and if it is NULL a different message should be printed. like "Please enter in a number".

Is there a solution to this? assuming that you only know arrays, pointers, and conditions, and the only lib you know of is stdio.
Aug 15 '07 #1
7 2507
Basically i have this task to get 2 inputs stored into num1 and num2 of int type. The program will then list all the possible numbers from 0 to num1, which are divisable by num2.

So if my input is 10 and 2
my output should be 2 4 6 8.

the problem now is that if i type in negative value or a character i won't get a correct output.

so i came up with this validation :

Expand|Select|Wrap|Line Numbers
1. if(num1 < 0 || num1==NULL){
2. printf("enter a number greater than 0");
3. return 0;
4. }
5.
which is not efficient at all. cause instead of re-prompting for another input of num1, it just exits. and if it is NULL a different message should be printed. like "Please enter in a number".

Is there a solution to this? assuming that you only know arrays, pointers, and conditions, and the only lib you know of is stdio.

Well supernick I have a Temporary solution for you, although I've been informed from experts on this exact site that it's not a correct thing to do, I've had this problem myself and it's the only thing I found that has worked.

This is temporary until someone corrects me and / or you find a better way.

You can use goto to call back right before the prompt if the conditions are met .

so first you put an identifier? right before the prompt which can be almost anything (dog in this example) followed by a colon

Expand|Select|Wrap|Line Numbers
1. dog:
2. cout << "Enter a number ";
3. cin >> num1;
4. if(num1 < 0 || num1==NULL){
5. printf("enter a number greater than 0");
6. goto dog;
7. }
then continue with the code.
code in bold is what I added.
Aug 15 '07 #2
In order to omit the "goto", it's better to use a while-loop:
Expand|Select|Wrap|Line Numbers
1. bool validInput = false;
2. while(!validInput)
3. {
4.     cout << "Enter a number greater than 0 ";
5.     cin >> num1;
6.     if(num1 > 0) validInput = true;
7. }
Aug 15 '07 #3
ilikepython
844 Expert 512MB
In order to omit the "goto", it's better to use a while-loop:
Expand|Select|Wrap|Line Numbers
1. bool validInput = false;
2. while(!validInput)
3. {
4.     cout << "Enter a number greater than 0 ";
5.     cin >> num1;
6.     if(num1 > 0) validInput = true;
7. }
Yes, the loop is a much better solution than a goto. Pretty much anything with gotos can be accomplished without them. They should be avoided.
Aug 15 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
No one is taking any notice of bad input. That is, the fail bit is not considered.

A better solution is:
Expand|Select|Wrap|Line Numbers
1. int main()
2. {
3.      int test;
4.      bool rval;
5.      while (1)    //infinite loop. Need CTRL+C to stop program
6.      {
7.          rval = GetInt(test);
8.          if (rval)
9.          {
10.              cout << "The integer is: " << test << endl;
11.          }
12.          else
13.          {
14.              cout << "Error! Eating an input character" << endl;
15.              //You could use cin.get() here to see what you ate
16.              cin.ignore();
17.          }
18.
19.      }
20.
21.      return 0;
22.
23. }
24.
25. bool GetInt(int& data)
26. {
27.     cin >> data;
28.     if (cin.good())
29.     {
30.       return true;
31.     }
32.     else
33.     {
34.         cin.clear();    //clear the fail bit that got us here
35.         return false;
36.     }
37. }
38.
Here all of the displays are in main(). Putting displays inside functions is a bad idea as it scatters the screen layout throughtout the program. This acts like a kind of glue that prevents the functions from being used in other programs.

Here, GetInt() returns true only if an int has been entered. If an int is not entered, no corrective action is taken since the corrective action (like eating the offending character) may not eb the same in all programs. It is left to someone else to adjust the input buffer.
Aug 15 '07 #5
Thank you for all your suggestions!

The main problem about C is that they don't have boolean types. It is huge barrier for me since I come from a java background. But they do have ways to validate true and false which requires the use of 0s and 1s. so i'm assuming the equilivant of loop is

Expand|Select|Wrap|Line Numbers
1. int validinput=0
2.
3. while(validinput==0){
4.
5. printf("enter a number ");
6. scanf("%d",&userinput);
7.
8. if(userinput >0 || userinput != null)
9.     validinput =1;
10. else
11.     validinput=0;
12. }
13.
most of the suggestions are checking for negative/null values. but how would you validate character inputs?

Finally, for the last suggestion. i'm not pretty sure what does cin.good() do? does it work for C programming as well?

Aug 16 '07 #6
Ganon11
3,652 Expert 2GB
No, cin.good() is a function of cin, which is the standard input for C++. In C the common input is scanf. You can still test to see if it failed, however, using the following trick. scanf returns an int value, which is the number of values it correctly read for ending. So you can check to see if both values were read correctly by catching scanf's return value and comparing it to 2 - if equal, both values were legitimate integers, and you can proceed to check if they are negative or not.
Aug 16 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
The main problem about C is that they don't have boolean types.
Of course C has boolean types:

typdef short BOOL;

Now you have a BOOL.

Then you can:

BOOL x = TRUE;

and off you go.
Aug 16 '07 #8