Jump to content

Validade user input - Just Integer [C]

- - - - -

  • Please log in to reply
5 replies to this topic

#1
CheckList

CheckList

    Newbie

  • Members
  • Pip
  • 7 posts
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.


#2
notes

notes

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
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
Remebre about KISS & DRY

#3
CheckList

CheckList

    Newbie

  • Members
  • Pip
  • 7 posts
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.


#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
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 ...
}

sudo rm -rf /

#5
CheckList

CheckList

    Newbie

  • Members
  • Pip
  • 7 posts
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 ;)

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
In your ~/.emacs file, put this:

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

sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users