GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Aug-2003, 10:18
SANman SANman is offline
New Member
 
Join Date: Aug 2003
Posts: 1
SANman is an unknown quantity at this point
Question

help with getting single char


I'm new to C++ and I cant figure out how to get a single char from the user without them haveing to hit enter

example:
I hit 'A' and 'A' is stored int a varable but I dont hit enter

Any help would be great
  #2  
Old 08-Aug-2003, 04:50
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Usually doing a cin, the input is buffered in memory until the return key is hit.

If you turn off input buffering temporarily, using the www.cplusplus.com function, then reactivate it after for parformance sake, that might do the trick.

GF
  #3  
Old 11-Aug-2003, 11:36
SugarDamage SugarDamage is offline
New Member
 
Join Date: Aug 2003
Posts: 3
SugarDamage is an unknown quantity at this point
I was wondering about this also... could you elaborate on how to use "c setbuf()"?

thanks
  #4  
Old 11-Aug-2003, 11:41
SugarDamage SugarDamage is offline
New Member
 
Join Date: Aug 2003
Posts: 3
SugarDamage is an unknown quantity at this point
I was wondering about this also... could you elaborate on how to use "c setbuf()"?

thanks
  #5  
Old 12-Aug-2003, 03:20
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Sure, you could try something like this:

CPP / C++ / C Code:
#include <stdio.h>

int main(){
   char buffer[1];   //set a character buffer of 1 char.
   char yesorno;

    setbuf(stdin, buffer);  //assign the buffer to stdin.
     
    yesorno = getc(stdin);  //should take one char, without a space.

    //Do whatever you want!
}

I've no idea if this will work or not!

GF
  #6  
Old 18-Jul-2006, 06:16
Tomb332's Avatar
Tomb332 Tomb332 is offline
Junior Member
 
Join Date: Jul 2006
Location: Everywhere and yet nowhere at all
Posts: 47
Tomb332 is on a distinguished road
Post

Re: help with getting single char


Why would you need to set a buffer you can just write
CPP / C++ / C Code:
#include <iostream>;
char caracter[2];
cin.get(caracter, 1);
  #7  
Old 18-Jul-2006, 12:47
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: help with getting single char


Because cin cannot read a keystroke and continue as SANman asked about 3 years ago. Anyway caracter is a buffer so you set one up too.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #8  
Old 18-Jul-2006, 12:51
Tomb332's Avatar
Tomb332 Tomb332 is offline
Junior Member
 
Join Date: Jul 2006
Location: Everywhere and yet nowhere at all
Posts: 47
Tomb332 is on a distinguished road

Re: help with getting single char


Very Very sorry for wasting forum space. I probably diden't read the first entry properly.
  #9  
Old 19-Jul-2006, 05:52
balusss balusss is offline
Junior Member
 
Join Date: Dec 2005
Posts: 64
balusss is on a distinguished road
Smile

Re: help with getting single char


Quote:
Originally Posted by SANman
I'm new to C++ ......

i don't know whether he has become ''old'' or not by now... but i think this would do... pls see the folloing....

CPP / C++ / C Code:

#include <ncurses.h>

int main()
{   int ch;
    initscr();          /* Start curses mode        */
    raw();              /* Line buffering disabled  */
    keypad(stdscr, TRUE);       /* We read keystrokes ......      */
    noecho();           /* Don't echo() while we do getch */
    mvprintw(10,10,"Type any character to see it immediately: press '$' to end:\n");
    ch = getch();           /* If raw() hadn't been called
                            * we have to press enter before it gets to the   program      */
    mvprintw(11,10,"The pressed key(s) are:");                        
                            // by now you should have getten... this helps in positioning output
                            // on the screen wherever you want.... little calculation of rows/cols

        do {
            printw("%c", ch);
        } while ((ch=getch())!='$');
    refresh();          /* Print it on to the real screen ... the ncurses-window is just
                                imaginary and so we need to write to the real screen */
    getch();            /* Wait for user input */
    endwin();           /* End curses mode        */
    return 0;
}


if you find the code any confusing, i think, i should join you in the search to get the same effect without using 'ncurses.h', because this is the simplest program that can be written with this library. but for now, i hope i have answered the '3-year old' question... in my best possible way ;-))
__________________
balu------>>>>>U
perseverance pays slowly.
  #10  
Old 19-Jul-2006, 06:11
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: help with getting single char


Quote:
Originally Posted by balusss
i don't know whether he has become ''old'' or not by now... but i think this would do... pls see the folloing....
Considering there is not one standard function in the entire code you posted, I don't think it's an approved solution.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Irc Bot Boogy C++ Forum 16 05-Aug-2006 14:04
storing a token pointer as a string CoreLEx C Programming Language 1 07-Oct-2003 12:33
convert long to pointer to char realpopeye C++ Forum 2 26-Sep-2003 11:22
char to operator calculus87 C++ Forum 3 04-Sep-2003 11:05

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:51.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.