Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Validade user input - Just Integer [C]

user input

  • Please log in to reply
5 replies to this topic

#1 CheckList

CheckList

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 24 December 2011 - 12:19 PM

Hi,

I already tried a lot of things to validade an user input, I need to validate the input to be an integer.. I search for days and can't do that, and it might be that dificult...

Already tried with "isdigit", already tried read as string and convert to integer, well dont know how to do this..

Any help :confused:

my try:

....
available = 10;
 contador2 = 0;
 for(;;)
   {
    printf(" ## insert number you want\n");
    scanf("%d", &chosen);

    if(chosen>=available)
        printf(" ## invalid input\n");

    if(chosen < contador2)
        printf(" ## invalid input\n");
    else 
        break;
}

..........

Problem.. if I insert any character diferent of number(integer), it makes the chosen = 1.. I want to just exit from loop when I have a real input of number... between 0 and 10.

Edited by dargueta, 27 December 2011 - 01:11 AM.

  • 0

#2 notes

notes

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 88 posts
  • Location:Poland
  • Programming Language:C++
  • Learning:Java, C#

Posted 25 December 2011 - 03:37 AM

Basically in c++ if you put wrong datatype the variable wont be assigned with datum. You can check it with if (cin.fail());
In C you can check scanf input with tools in cctype.h
check : cctype (ctype.h) - C++ Reference
  • 0
Remebre about KISS & DRY

#3 CheckList

CheckList

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 25 December 2011 - 04:28 AM

well I made it perfect I think, a lot of work to reach this lol.. check it

Original Idea:
-validate input of user:
- it must be a number;
- it must be integer;
- it must be positive;
-it must be lower than variable "tamanho" that is 9 for now;
- it must be clean, that means no crashes and no errors.
.................
[INDENT][INDENT]char chose [3];
int contador2, test, chosen, tamanho;


tamanho =9;
contador2 = 0;
test==1;


while(test != 1)
{
    for(;;)
    {
        printf(" ## Insert number you want\n");
        scanf("%s", chose);


        if(strlen(chose) > 2)
            printf(" ## Does not match\n");


        else if(strlen(chose)<2)
            if(isdigit(chose[0]) == 0)
                printf(" ## Does not match\n");
            else
                break;
        else if(strlen(chose)>1)
            if( isdigit(chose[1]) == 0)
                printf(" ## Does not match\n");
            else
                break;
    }


    for(;;)
    {
        chosen = atoi(chose);
        
        if(chosen>=tamanho)
            test = 0;
        else if(chosen < contador2)
            test = 0;
        else
            test = 1;
        break;
    }
    
    if(test == 0)
        printf(" ## Does not match\n");
}

[/INDENT]
[/INDENT]
............................

Well in that way don't exists any errors in any input the user insert.. so its perfect now :w00t::w00t:

the question now is: -There is any cleaner way to do the same? with less lines of code and obviously with another functions?

Regards

Edited by dargueta, 27 December 2011 - 01:15 AM.

  • 0

#4 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 27 December 2011 - 01:24 AM

I see some logic errors in here. Try entering the string "a1" and see what happens. Just so you see what's going on:

char chose [3];
int contador2, test, chosen, tamanho;


tamanho =9;
contador2 = 0;
[COLOR=BLUE]You want to assign test to 1 here? Right now you're comparing it. Be careful with == and =.[/COLOR]
test [COLOR=RED]=[/COLOR] 1;


while(test != 1)
{
    for(;;)
    {
        printf(" ## Insert number you want\n");
        scanf("%s", chose);


        if( strlen(chose) > 2 )
        {
            printf(" ## Does not match\n");
        }
        else if( strlen(chose) < 2 )
        {
            [COLOR=BLUE]If the string length is less than 2, then it's either
            1 or 0. Here we only check the first digit.[/COLOR]
            if(isdigit(chose[0]) == 0)
            {
                printf(" ## Does not match\n");
            }
            else
            {
                break;
            }
        }
        else if( strlen(chose) > 1)
        {
            [COLOR=BLUE]And here's your problem. You only check the second character
            here, so if the first is non-numeric, then it'll pass the test.[/COLOR]
            if( isdigit(chose[1]) == 0)
            {
                printf(" ## Does not match\n");
            }
            else
            {
                break;
            }
        }
        
        [COLOR=BLUE]Note: You don't have an else at the end of this block.[/COLOR]
    }


    ... other stuff ...
}

  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#5 CheckList

CheckList

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 28 December 2011 - 02:31 PM

Thank you.. I did already spotted some errors, but spotted some of errors now because of you.. thanks a lot :w00t::w00t:

that thing of "=" and "==" I really don't know when it work well.. lol because in "IF" it must be "=="

by the way.. there is any way to automatic indent my code in emacs? instead of "tab" or "ctrl+x"+"tab"..

I'm not a really programmer or so.. so don't blame ;)
  • 0

#6 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 28 December 2011 - 04:23 PM

In your ~/.emacs file, put this:

(setq c-default-style "linux" c-basic-offset 4)

  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'






Also tagged with one or more of these keywords: user input

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download