How do get a number from getche() ?? 
Author Message
 How do get a number from getche() ??

When checking the input from getche(), All i can see is the ASCII
number of the input. How can get what was actually typed? Do i need to
cast?
EX:
int input;
while(1){
   input = getche();
   if(isdigit(input)){
      printf("input = %d\n",input);
   }else{
      break;
   }

Quote:
}

if i input a number, the ascii value returned
so if i enter '2' i get 'input = 50'
how can i read the variable as '2'?
isdigit() seems to be able to tell letters from numbers from getche().


Sat, 05 Feb 2005 02:06:19 GMT  
 How do get a number from getche() ??

Quote:
> When checking the input from getche(), All i can see is the ASCII
> number of the input. How can get what was actually typed? Do i need to
> cast?
> EX:
> int input;
> while(1){
>    input = getche();

Non-standard function. I'll assume it works like getchar().

Quote:
>    if(isdigit(input)){
>       printf("input = %d\n",input);
>    }else{
>       break;
>    }
> }
> if i input a number, the ascii value returned
> so if i enter '2' i get 'input = 50'
> how can i read the variable as '2'?
> isdigit() seems to be able to tell letters from numbers from getche().

No, when you input a character, it is returned. Characters happen to be
a subrange of integers in C. So if you input '2', what getchar() (or
getche() on your platform) returns is '2'. But when you printf() this
with "%d" you get to see that character's integer value, which on your
encoding happens to be 50. I assume that if you typed 'A' you would get
to see 65.
Simply change "%d" in your printf() call to "%c" to make printf() print
the actual character glyph.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"Immanuel Kant but Genghis Khan."
   - The Official Graffitist's Handbook



Sat, 05 Feb 2005 02:11:59 GMT  
 How do get a number from getche() ??
If you want to actually get the number itself you can say

some_int_variable = atoi(input);



Quote:

> > When checking the input from getche(), All i can see is the ASCII
> > number of the input. How can get what was actually typed? Do i need to
> > cast?
> > EX:
> > int input;
> > while(1){
> >    input = getche();

> Non-standard function. I'll assume it works like getchar().

> >    if(isdigit(input)){
> >       printf("input = %d\n",input);
> >    }else{
> >       break;
> >    }
> > }

> > if i input a number, the ascii value returned
> > so if i enter '2' i get 'input = 50'
> > how can i read the variable as '2'?
> > isdigit() seems to be able to tell letters from numbers from getche().

> No, when you input a character, it is returned. Characters happen to be
> a subrange of integers in C. So if you input '2', what getchar() (or
> getche() on your platform) returns is '2'. But when you printf() this
> with "%d" you get to see that character's integer value, which on your
> encoding happens to be 50. I assume that if you typed 'A' you would get
> to see 65.
> Simply change "%d" in your printf() call to "%c" to make printf() print
> the actual character glyph.

> --

> | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
> | http://www.helsinki.fi/~palaste       W++ B OP+                     |
> \----------------------------------------- Finland rules! ------------/
> "Immanuel Kant but Genghis Khan."
>    - The Official Graffitist's Handbook



Sat, 05 Feb 2005 02:18:02 GMT  
 How do get a number from getche() ??

Quote:
> If you want to actually get the number itself you can say
> some_int_variable = atoi(input);

atoi() is defined as:
int atoi(const char *nptr);
It takes a _string_ and returns the numeric value that it represents.
Integers are not strings.
Please read a book about C before giving bogus advice like this.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"I am lying."
   - Anon



Sat, 05 Feb 2005 02:21:01 GMT  
 How do get a number from getche() ??

Quote:

> When checking the input from getche(), All i can see is the ASCII
> number of the input. How can get what was actually typed? Do i need to
> cast?
> EX:
> int input;
> while(1){
>    input = getche();
>    if(isdigit(input)){
>       printf("input = %d\n",input);
>    }else{
>       break;
>    }
> }

> if i input a number, the ascii value returned
> so if i enter '2' i get 'input = 50'
> how can i read the variable as '2'?
> isdigit() seems to be able to tell letters from numbers from getche().

A character *is* its numeric equivalent in the character set.  You simply
want it to be *printed* as that character.

Just tell printf to print your value as a character (%c) instead of an
integer (%d).

printf("input = %c\n", input);

HTH,
Ryan.



Sat, 05 Feb 2005 02:02:13 GMT  
 How do get a number from getche() ??
Thanks for the answers fellas!
I guess i had the ascii value all along ;)
My program was using "atoi(gets(value));" to get a number from
the console. I could then use this number as an index of an array.
then I changed it to getch().  Using getche() as an array index always
returns null. and using atoi() on the return of getche() makes the
prog crash. ;(
I guess i have a lot more learning to do.
Quote:

>When checking the input from getche(), All i can see is the ASCII
>number of the input. How can get what was actually typed? Do i need to
>cast?
>EX:
>int input;
>while(1){
>   input = getche();
>   if(isdigit(input)){
>      printf("input = %d\n",input);
>   }else{
>      break;
>   }
>}

>if i input a number, the ascii value returned
>so if i enter '2' i get 'input = 50'
>how can i read the variable as '2'?
>isdigit() seems to be able to tell letters from numbers from getche().



Sat, 05 Feb 2005 02:43:09 GMT  
 How do get a number from getche() ??

Quote:
> Thanks for the answers fellas!
> I guess i had the ascii value all along ;)
> My program was using "atoi(gets(value));" to get a number from
> the console. I could then use this number as an index of an array.

Note that this only works if the input doesn't overflow the space
allocated for the variable "value". Unfortunately gets() is a badly
designed function and cannot guard against this. For future concern,
you might consider replacing gets() with fgets().

Quote:
> then I changed it to getch().  Using getche() as an array index always
> returns null. and using atoi() on the return of getche() makes the
> prog crash. ;(

Both getch() and getche() are non-standard C functions, but I'll
assume they return the same thing as getchar(). What do you mean by
"using getche() as an array index"? An index to which array?
Using atoi() on the return value of getche() causes undefined
behaviour, as does using atoi() on *any* int value. This undefined
behaviour happens to manifest itself as a prog crash on your
platform.

Quote:
> I guess i have a lot more learning to do.

Have you tried replacing %d with %c in your program yet?

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"You have moved your mouse, for these changes to take effect you must shut down
and restart your computer. Do you want to restart your computer now?"
   - Karri Kalpio



Sat, 05 Feb 2005 02:46:25 GMT  
 How do get a number from getche() ??
Yes, using %c instead of %d worked.
for an explanation of the array:
im rewriting my (console) shoutcast player. I have a static array
of station ip address (i know its bad form, but the prog is just for
my personal use)
i found it annoying to have to to hit enter when changing stations, so
instead of using 'atio(gets())' i wanted to use getche to have control
return back to the program immediatly. the value returned by gets()
was used directly as the index of the station array [this worked
fine], then close the current stream and create a new one with the new
ip address.
getche() is acting funny, but im not experienced enough to peek the
console buffer.
Quote:


>> Thanks for the answers fellas!
>> I guess i had the ascii value all along ;)
>> My program was using "atoi(gets(value));" to get a number from
>> the console. I could then use this number as an index of an array.

>Note that this only works if the input doesn't overflow the space
>allocated for the variable "value". Unfortunately gets() is a badly
>designed function and cannot guard against this. For future concern,
>you might consider replacing gets() with fgets().

>> then I changed it to getch().  Using getche() as an array index always
>> returns null. and using atoi() on the return of getche() makes the
>> prog crash. ;(

>Both getch() and getche() are non-standard C functions, but I'll
>assume they return the same thing as getchar(). What do you mean by
>"using getche() as an array index"? An index to which array?
>Using atoi() on the return value of getche() causes undefined
>behaviour, as does using atoi() on *any* int value. This undefined
>behaviour happens to manifest itself as a prog crash on your
>platform.

>> I guess i have a lot more learning to do.

>Have you tried replacing %d with %c in your program yet?



Sat, 05 Feb 2005 03:08:26 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Getting a file version number as a number

2. Doing arithmetic with very large numbers

3. Problems doing realloc, getting heap runtime error?

4. DAO3.60 - getting error 3260 when doing CDaoRecordset::Update

5. Just what *is* gets() doing?

6. Getting line numbers where exceptions occurs

7. Getting Number of Months and Days between two dates

8. Getting and using numbers in a text file

9. Getting and using numbers in a text file

10. getting calender week number

11. getting week number wirh strftime()

12. Getting hard drive serial number through API?

 

 
Powered by phpBB® Forum Software