Help with sscanf()

Ello!
Have made a function to trap any errors when entering a number into my program from the keyboard. Doesnt allow any keys apart from 1 to 0 and also ESC, Backspace and Enter.
returns a non-zero if the user presses escape to cancel input, also takes in a length to restrict length of number. but i cant get sscanf() to output a LONG integer, it just put on a normal integer, so if i enter a number greater than 36k or whatever it just goes mental!
try it out and see, i called it with a length of 10.

int iinteger(long int *number,int length)
{
long int inc,ret,x,y,x1,y1;
char input,word[80];

x=wherex();
y=wherey();
inc=0;
ret=0;
*number=0;

while (inc0)
printf("%s",word);
x1=wherex();
y1=wherey();
input=getch();
word[inc+1]='';
switch (input)
{
case 0:break;
case 8:{
if(inc>0)
{
word[inc-1]='';
inc--;
if(x1>1)
gotoxy(x1-1,y1);
else gotoxy(80,y1-1);
printf(" ");
}
}
break;
case 13:{
if(inc>0)
{
word[inc]='';
inc=length+1;
}
}
break;
case 27:{
ret=1;
inc=length+1;
}
break;
default:{
if(inc=48 && input<=57)
{
word[inc]=input;
inc++;
}
}
}
}
}
printf("
");
sscanf(word,"%D",number);
return ret;
}

wondering if anyone can see the problem i have, i cant as of yet solve the problem. i want to be able to enter a LARGE number and it be converted into a long int before it is passed out (by reference)

Comments

  • [CODE]
    int iinteger(long int &number) {
    sscanf(word, "%li", number);
    }
    void main() {
    long int num;
    iinteger(num);
    }
    [/CODE]
    or
    [CODE]
    int iinteger(long int *number) {
    sscanf(word, "%li", number);
    }
    void main() {
    long int num;
    iinteger(&num);
    }
    [/CODE]
    I prefer the first, but it is only for use with C++, the second method is for both C and C++.
  • well.....
    was hoping to keep the rest of the function otherwise there aint no point in having a seperate function.
    The idea of this function was to limit what keys that can be used in order to avoid the errors when entering a character instead of a number.

    the problem rises at the end of the function when the string is converted into an integer using sscanf(). it doesnt convert it correctly into a LONG integer (+/- 2,147,483,647), it seems to only convert it into a standard integer (+/- 32,768).

    (btw am using borland libraries and the code i am using is strictly C, i aint doing C++ yet. So stuff like gotoxy() and wherex, wherey etc... may be compiler specific) :o)

  • : Ello!
    : Have made a function to trap any errors when entering a number into my program from the keyboard. Doesnt allow any keys apart from 1 to 0 and also ESC, Backspace and Enter.
    : returns a non-zero if the user presses escape to cancel input, also takes in a length to restrict length of number. but i cant get sscanf() to output a LONG integer, it just put on a normal integer, so if i enter a number greater than 36k or whatever it just goes mental!
    : try it out and see, i called it with a length of 10.

    I can't use that Borland specific stuff, but it should work..
    [code]
    int main()
    {

    long int some_long = iinteger( &some_long );

    printf("some long: %ld
    ", some_long);

    return 0;
    }

    long iinteger(long int *some_long)
    {

    char some_string[] = "2147483647";

    sscanf( some_string, "%ld", some_long );

    printf("some long: %ld
    ", *some_long);

    return *some_long;
    }
    [/code]
    : wondering if anyone can see the problem i have, i cant as of yet solve the problem. i want to be able to enter a LARGE number and it be converted into a long int before it is passed out (by reference)
    :

    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories