Welcome Guest ( Log In | Click here to Register a free account now! )
Welcome to Bleeping Computer, 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.![]() ![]() |
May 10 2006, 11:11 PM
Post
#1
|
|
![]() Member ![]() ![]() Group: Members Posts: 71 Joined: 15-January 06 From: Junee, NSW, Australia Member No.: 50,879 |
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 Alan -------------------- There are 10 types of people - those who understand binary and those who don't!!
Today is the Beta version of Tomorrow! |
|
|
|
May 11 2006, 12:14 AM
Post
#2
|
|
![]() The BSG Malware Fighter ![]() ![]() ![]() ![]() ![]() ![]() Group: HJT Team Coach Posts: 5,369 Joined: 20-April 06 From: Hamburg Member No.: 64,788 |
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 - - The HJT forum is very busy. If I've not posted back within 24 hrs., send a PM with your topic link. Thank you. - HELP REQUESTS VIA THE PM SYSTEM WILL BE IGNORED. The Forums are there for a reason! Thanks- ![]() |
|
|
|
May 13 2006, 12:37 AM
Post
#3
|
|
![]() Member ![]() ![]() Group: Members Posts: 71 Joined: 15-January 06 From: Junee, NSW, Australia Member No.: 50,879 |
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! |
|
|
|
May 13 2006, 02:44 AM
Post
#4
|
|
![]() The BSG Malware Fighter ![]() ![]() ![]() ![]() ![]() ![]() Group: HJT Team Coach Posts: 5,369 Joined: 20-April 06 From: Hamburg Member No.: 64,788 |
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 - - The HJT forum is very busy. If I've not posted back within 24 hrs., send a PM with your topic link. Thank you. - HELP REQUESTS VIA THE PM SYSTEM WILL BE IGNORED. The Forums are there for a reason! Thanks- ![]() |
|
|
|
May 15 2006, 10:49 AM
Post
#5
|
|
|
Member ![]() ![]() Group: Members Posts: 21 Joined: 25-August 05 Member No.: 32,281 |
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...
|
|
|
|
May 27 2006, 05:12 PM
Post
#6
|
|
|
Member ![]() ![]() Group: HJT Team Posts: 131 Joined: 24-April 05 Member No.: 18,017 |
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 |
|
|
|
Sep 25 2006, 10:30 PM
Post
#7
|
|
|
New Member ![]() Group: Members Posts: 1 Joined: 25-September 06 Member No.: 87,121 |
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 But the sting is that so many guys give advices but nothing more CODE 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 |
|
|
|
Sep 26 2006, 04:25 PM
Post
#8
|
|
![]() Hail Groovicus! ![]() ![]() ![]() ![]() ![]() ![]() Group: Site Admin Posts: 6,455 Joined: 5-June 04 From: Vermillion, SD Member No.: 689 |
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. -------------------- |
|
|
|
Sep 30 2006, 08:03 AM
Post
#9
|
|
![]() Member ![]() ![]() Group: Members Posts: 81 Joined: 3-June 06 From: Santiago Chile Member No.: 70,596 |
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. lol... how smooth... 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... -------------------- ![]() |
|
|
|
![]() ![]() |
| Lo-Fi Version | Time is now: 9th January 2009 - 04:25 PM |