Re: cin >> c

From: K Campbell (kieran_at_cyrocom.co.uk)
Date: 08/24/04


Date: 24 Aug 2004 12:08:59 -0700

Try doing this:
char c;
cin>>c;
cin.ignore();
cin.getline(name, int);

or, if you want to just capture one character, without the user having
to press enter:
(you must #include <conio.h>)
char c = getch();
cin.getline(name, int);

Hope i've been of some help.
Kieran

"Kevin W." <contact@in.sig> wrote in message news:<opsc6ef3ufjxvii7@localhost.localdomain>...
> I'm writing a simple Unix console app. I need to take an input of a
> single character from the user and discard the rest.
>
> If I have:
> char c;
> cin >> c;
> And the user types the letter 'a', then presses Enter, 'a' gets put into c
> as I want. BUT, Enter remains as the next character in the stream, so if
> I then do a getline(cin, word), I get an empty string in word. The other
> requirement I need is that if the user just presses Enter without entering
> a letter, c should be assigned to '\n'.
>
> Ideally, it should process the character (whatever it is) without the user
> having to press Enter.



Relevant Pages

  • Re: K&R exercise 1-18
    ... _without_ the string terminator and has MAXIMUM+1 array elements. ... char lineis in this context equivalent to char *line ... int main ... You lose one character by overwriting it with '\n'. ...
    (comp.lang.c)
  • Re: Write to file
    ... You can read the file one character at a time. ... compared to the current char. ... Once the (lgh - 1)th char ... void initnext(int *next, const char *id, int lgh) ...
    (comp.lang.c)
  • Re: Write to file
    ... int main(int argc, char *argv) ... I intended you you use c here not read another character! ... You reuse the buffer. ...
    (comp.lang.c)
  • Re: Small Correction to K&R Exercise 1-22 Solution on CLC-Wiki
    ... My fix is now reflected at the above address. ... I don't think that overwriting the n-th character with a ... single char location by letting a set sign bit indicate that a line ... int main ...
    (comp.lang.c)
  • Re: doubly-linked list & sorting
    ... char Emess; ... diff = -1; ...
    (comp.lang.c)