Validating User Inputs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • supernick
    New Member
    • Aug 2007
    • 2

    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 :

    Code:
    if(num1 < 0 || num1==NULL){
    printf("enter a number greater than 0");
    return 0;
    }
    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.
  • Hunderpanzer
    New Member
    • Jul 2007
    • 60

    #2
    Originally posted by supernick
    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 :

    Code:
    if(num1 < 0 || num1==NULL){
    printf("enter a number greater than 0");
    return 0;
    }
    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

    Code:
    [B]dog:[/B]
    cout << "Enter a number ";
    cin >> num1;
    if(num1 < 0 || num1==NULL){
    printf("enter a number greater than 0");
    [B]goto dog;[/B]
    }
    then continue with the code.
    code in bold is what I added.

    Comment

    • Wizard1981
      New Member
      • Aug 2007
      • 11

      #3
      In order to omit the "goto", it's better to use a while-loop:
      Code:
      bool validInput = false;
      while(!validInput)
      {
          cout << "Enter a number greater than 0 ";
          cin >> num1;
          if(num1 > 0) validInput = true;
      }

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by Wizard1981
        In order to omit the "goto", it's better to use a while-loop:
        Code:
        bool validInput = false;
        while(!validInput)
        {
            cout << "Enter a number greater than 0 ";
            cin >> num1;
            if(num1 > 0) validInput = true;
        }
        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.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          No one is taking any notice of bad input. That is, the fail bit is not considered.

          A better solution is:
          [code=cpp]
          int main()
          {
          int test;
          bool rval;
          while (1) //infinite loop. Need CTRL+C to stop program
          {
          rval = GetInt(test);
          if (rval)
          {
          cout << "The integer is: " << test << endl;
          }
          else
          {
          cout << "Error! Eating an input character" << endl;
          //You could use cin.get() here to see what you ate
          cin.ignore();
          }

          }

          return 0;

          }

          bool GetInt(int& data)
          {
          cin >> data;
          if (cin.good())
          {
          return true;
          }
          else
          {
          cin.clear(); //clear the fail bit that got us here
          return false;
          }
          }
          [/code]

          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.

          Comment

          • supernick
            New Member
            • Aug 2007
            • 2

            #6
            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

            Code:
            int validinput=0
            
            while(validinput==0){
            
            printf("enter a number ");
            scanf("%d",&userinput);
            
            if(userinput >0 || userinput != null)
                validinput =1;
            else
                validinput=0;
            }
            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?

            Thanks for all your time!

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              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.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by suopernick
                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.

                Comment

                Working...