Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » [C++] How to get ONE character?
Closed Thread
Old 11-09-2004, 08:42 PM   #1 (permalink)
 
Newb Techie

Join Date: Nov 2004

Posts: 5

selkie

Default [C++] How to get ONE character?

Getchar() seems to make the most sense here.

Currently I'm doing [variable] = getchar();

... to get a single character. But this doesn't work: it waits for a carriage return before accepting input. The function that processes that input then runs twice instead of once: it runs once (I'm assuming) for the actual character, and once for the carriage return.

How do I get getchar() to do what I want here? I want it to process the input after every single character, without waiting for a CR. Should I be using something different?

Thanks!
Charles
selkie is offline  
Old 11-09-2004, 11:05 PM   #2 (permalink)
 
Ultra Techie

Join Date: Jun 2004

Posts: 973

intercodes

Send a message via Yahoo to intercodes
Default

selkie,
Yup ,getchar() function gets the character and terminates only at a Carriage Return.

If you want to process like what you said, this code might help, I guess:rolleyes:


while (getchar() != '\n')
{
[Do The Processing ]
}


The loop continues as long as no enter key is pressed.

Sign
Codes

intercodes is offline  
Old 11-10-2004, 10:12 AM   #3 (permalink)
 
Newb Techie

Join Date: Nov 2004

Posts: 5

selkie

Default

Codes,

Thanks for your help! I tried what you wrote, and it did eliminate the double-processing problem. However, it still waits for a <CR> before doing any processing.

I'm using VS.Net 03, and one of my friends said that its getchar() handling was different compared to Borland. Any idea if that's the case?

Thanks again!
Charles
selkie is offline  
Old 11-10-2004, 10:16 AM   #4 (permalink)
 
Ultra Techie

Join Date: Jun 2004

Posts: 973

intercodes

Send a message via Yahoo to intercodes
Default

selkie,

Quote:
Any idea if that's the case?

Hmm...No idea about those Microsoft languages...

Sign
Codes

intercodes is offline  
Old 11-10-2004, 10:06 PM   #5 (permalink)
 
Ultra Techie

Join Date: Sep 2003

Location: Bamberg, Germany

Posts: 549

Iron_Cross

Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

managed C++ is way different than traditional c++...so you may have to consult the .NET documentation...I'm sure there is a much more viable function for your purpose.

You could always use events, I'd use the KeyDown or KeyPress events. That would allow you to do the processing after the first key was pressed, and no more. Read up in the .NET documentation about those events.
__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On