Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Getchar() Problem In C


  • Please log in to reply
8 replies to this topic

#1 Alan-LB

Alan-LB

  • Members
  • 71 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Junee, NSW, Australia
  • Local time:11:17 AM

Posted 10 May 2006 - 11:11 PM

I am using Standard C compiled with GCC under Linux Fedora Core 4

When I run this program and enter a character at the prompt, I have to press the
ENTER key as well. This gives me 2 input characters - 'a' and '\n' (Hex 61 and 0a)

It seems as though the getchar() function needs ENTER to terminate reading stdin.

I am trying to get the program to respond when I press one key only (ie without needing
to press ENTER as well).

Program
~~~~~~~
#include <stdio.h>
#include <stdlib.h>

int main()
{
char x; //Input character
while (1)
{
printf(">"); //Print prompt '>'
x = getchar(); //Get input character
printf("%d %02x \n", x, x); //Print character in decimal & Hex
if (x == 'q') //Exit if the character is 'q'
exit(0);
}
}

Output
~~~~~
>a < input character
97 61
>10 0a
>b < input character
98 62
>10 0a
>c < input character
99 63
>10 0a
>d < input character
100 64
>10 0a
>q < input character
113 71

I have tried using scanf() but got the same results.

Any ideas as to how I can input ONE character only? TIA :thumbsup:

Alan
There are 10 types of people - those who understand binary and those who don't!!

Today is the Beta version of Tomorrow!

BC AdBot (Login to Remove)

 


#2 Yourhighness

Yourhighness

    The BSG Malware Fighter


  • Malware Response Team
  • 7,943 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Hamburg
  • Local time:01:17 AM

Posted 11 May 2006 - 12:14 AM

Hi Alan,
been a while, but I recalled correctly. Pls habe a look at the getchar() part on the site provided.
http://www.friedspace.com/CTutorial2.html

rgds, Johannes

"How did I get infected?" - "Safe-hex" - Member of UNITE -
Posted Image


#3 Alan-LB

Alan-LB
  • Topic Starter

  • Members
  • 71 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Junee, NSW, Australia
  • Local time:11:17 AM

Posted 13 May 2006 - 12:37 AM

I followed up your link. I added another getchar() so the program is now
....
int x;
.....
x = getchar();
getchar();
....
The second getchar() seems to "absorb" the '\n' from pressing ENTER

Now works fine Thanks for your help

Alan
There are 10 types of people - those who understand binary and those who don't!!

Today is the Beta version of Tomorrow!

#4 Yourhighness

Yourhighness

    The BSG Malware Fighter


  • Malware Response Team
  • 7,943 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Hamburg
  • Local time:01:17 AM

Posted 13 May 2006 - 02:44 AM

Hi Alan,
exactly! The second getch() is only for the return. There are methods using string, but its been now like 4 years since i have done programming ^^.

regards,

"How did I get infected?" - "Safe-hex" - Member of UNITE -
Posted Image


#5 Happy_Reaper

Happy_Reaper

  • Members
  • 21 posts
  • OFFLINE
  •  
  • Local time:08:17 PM

Posted 15 May 2006 - 10:49 AM

The way it works is that getchar() reads a character from the input buffer. Therefore, if the buffer is empty, it waits for it to obtain values before it reads one. Input is only sent to the buffer from the keyboard after you press the enter key. That is why you are having problems.

So, when you input your first character, it is not yet placed in the buffer, so getchar doesn't read it. Then, when you press enter, both the previous character and the newline are placed in the buffer, so your first getchar would get you the desired character while your second one would get you a newline (which is why the above code snippet gets rid of it in order to avoid unexpected results).

For more info, see : http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

That is just a general FAQ of topics, you'll have to find the one that best suits your needs.

If you just want to get one character from the user, you could probably just use scanf and you would be fine.
Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" <-- It's great to see the educational system moving in the right direction...

#6 Swandog46

Swandog46

  • Members
  • 134 posts
  • OFFLINE
  •  
  • Local time:08:17 PM

Posted 27 May 2006 - 05:12 PM

scanf would work, if you make sure to flush the stdin buffer afterwards:

scanf("%c", &my_char);
fflush(stdin);

depending on your exact implementation, you might try getch() also, which is declared in curses.h:
http://www.opengroup.org/pubs/online/79087...s/curses.h.html

#7 frealx

frealx

  • Members
  • 1 posts
  • OFFLINE
  •  
  • Local time:08:17 PM

Posted 25 September 2006 - 10:30 PM

Hi young peoples,

I'm surprised - so many posts and no solution at all ?! No solution, because no one from above mentioned answers is not correct. For examle, this one:

I followed up your link. I added another getchar() so the program is now
....
int x;
.....
x = getchar();
getchar();
....
The second getchar() seems to "absorb" the '\n' from pressing ENTER

Now works fine Thanks for your help

Alan


will work only if the user press the ONLY ONE KEY before pressing ENTER. The second getchar will "absorb" only the second character and that's it :thumbsup:
But the sting is that so many guys give advices but nothing more :flowers: So, voilą one way to take only one character from the prompt:
int i;
		char choice='0', *pn;

		...
		pn = &choice;
		while ( (atoi(pn) > i) || (atoi(pn) <= 0) ) // if you need any additional conditions
		{
			printf("Your choice [1"); if ( i > 1 ) printf("-%i", i); printf("] :");
			do scanf("%c",&choice); while ( getchar() != '\n' );
		}
		printf("Your choice was: %i",atoi(pn));

Good luck,

freealx

#8 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:17 PM

Posted 26 September 2006 - 04:25 PM

In general, we try to not just give the people answers to their programming issues, but rather try to point them to resources that will help them find a solution on their own. Your solution is not necessarily any better than the one Alan-LB found on his own. And seeing how his specifications were to read a single letter character, your solution is actually contrary to the specifications, which makes your solution wrong.

It's all a matter of perspective, isn't it. :thumbsup:

#9 petocities

petocities

  • Members
  • 102 posts
  • OFFLINE
  •  
  • Location:Santiago Chile
  • Local time:09:17 PM

Posted 30 September 2006 - 08:03 AM

In general, we try to not just give the people answers to their programming issues, but rather try to point them to resources that will help them find a solution on their own. Your solution is not necessarily any better than the one Alan-LB found on his own. And seeing how his specifications were to read a single letter character, your solution is actually contrary to the specifications, which makes your solution wrong.

It's all a matter of perspective, isn't it. :thumbsup:


lol... how smooth... :flowers:
Actually, I came intending to help, but everything has been said, and I don't remember C that well anymore... so I'm kinda useless here, hehe...
Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users