Thread: Problems with getch()

  1. #1
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140

    Problems with getch()

    I'm having one problem with the getch() function. Let's say I do something like this..
    Code:
    short a = getch();    //need the value!
    char* name;
    cout << "Enter Name: ";
    cin >> name;
    The character that I press when getch() comes up will be put onto the screen when I go to input the name array. For example, let's say when getch() comes up, I press "a". Now the the next line would read this: Enter Name: a

    is there a way to fix this? if you want to see exactly what i mean, download the file and run the .exe

    if you want to see the source code, i'm using dev 4.9.4.1 But just open the .exe
    Last edited by GrNxxDaY; 08-09-2002 at 10:10 PM.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    I believe you want to know a way to pause the program. Here is one solution.

    -----
    // ignore 256 characters other than Enter
    cin.ignore(256, '\n');

    // ingore

    cin.ignore();
    -----

    Test out cin.ignore.

    Kuphryn

  3. #3
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    Oops.. that isn't for a pause in the program... I test a for what key the user pressed.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Use cin.ignore() to pause the program.

    Kuphryn

  5. #5
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    I need the ASCII value of the key pressed by the user though- its just not for a pause.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  6. #6
    kuphryn
    Guest
    Try this solution.

    -----
    std::getline(cin, 1);

    // or this

    TCHAR cOne;
    std::cin.get(cOne);
    -----

    Kuphryn

  7. #7
    *ClownPimp*
    Guest
    he wasnt asking how to pause the proggy...

    GrN, Ive had that problem for years and i havent yet figured out/seen a solution. My suggestion is not to mix calls between getch() and any other standard input functions because they all have that problem.

  8. #8
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    here..

    Originally posted by GrNxxDaY
    I need the ASCII value of the key pressed by the user though- its just not for a pause.
    Here, this program finds ASCII key codes for most keys...
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    main()
    {
    	char c;
    	
    	printf("KEY\tCODE\n");
    
    	while ((c = getch()) != 27)
    	{
    		printf("%3c\t%4d\n", c, (int)c);
    		fflush(stdin);
    	}
    }
    Good Luck
    Last edited by moonwalker; 08-10-2002 at 06:54 AM.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >fflush(stdin);
    This is wrong, search the boards for the many times I've explained why.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    hmm

    Originally posted by Prelude
    >fflush(stdin);
    This is wrong, search the boards for the many times I've explained why.

    -Prelude
    It works fine with my compiler. It's not wrong.

    Header File

    stdio.h

    Category

    Input/output Routines

    Syntax

    #include <stdio.h>
    int fflush(FILE *stream);

    Description

    Flushes a stream.

    If the given stream has buffered output fflush writes the output for stream to the associated file.

    The stream remains open after fflush has executed. fflush has no effect on an unbuffered stream.

    Return Value

    fflush returns 0 on success. It returns EOF if any errors were detected.
    Last edited by moonwalker; 08-10-2002 at 08:58 PM.

  11. #11
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    thanks clown
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: hmm

    >It works fine with my compiler. It's not wrong.
    Not the fflush(stdin) argument again?
    http://www.eskimo.com/~scs/C-faq/q12.26.html
    http://www.cprogramming.com/cboard/s...threadid=23237
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >It works fine with my compiler. It's not wrong.
    It may work for you, but that doesn't make it any less wrong. Unless you define the C++ language of course. Please see previous explanations of why, and instead of getting defensive maybe you should research what I say and try to prove me wrong. That would be more effective than the equivalent of saying "Is not!".

    -Prelude
    My best code is written with the delete key.

  14. #14
    Moonwalker
    Guest
    oh please... go home.. it's not wrong and borland is not idiotic
    to include it if it is....
    it is not ANSI standard, but it doesn't make it wrong...

  15. #15
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    If the given stream has buffered output fflush writes the output for stream to the associated file.
    You flush an input stream, behaviour of your program is therefore completely undefined. It may work on your compiler and OS, it might reformat my disk on my compiler and OS. Something isn't right just because it works on your specific computer in your specific situation. If it doesn't work on all compilers that follow the standard, it's wrong.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. getch() / getche()
    By Blanket in forum C++ Programming
    Replies: 5
    Last Post: 04-27-2003, 07:06 PM
  3. Replies: 5
    Last Post: 11-26-2002, 10:34 PM
  4. Using getch()
    By pr0ficient in forum Linux Programming
    Replies: 2
    Last Post: 07-26-2002, 05:59 AM
  5. getch() acting up...
    By Govtcheez in forum C Programming
    Replies: 9
    Last Post: 08-18-2001, 12:56 PM