Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getchar not working as it should

Status
Not open for further replies.

Tommyhat

Programmer
Sep 10, 2004
96
CA
When i use 'getchar();' it will take any numberof characters until return is pressed.

I just want to use it as an input key/keypressed detetecter and i KNOW that's how it's supposed to work.

I'm using Dev C++ 4 complier and ive re-installed to see if that would fix a possible header error.

Does anyone know what i could do?

---------------------------------------
2b||!2b that == the_question
 
Try using getch or _getch instead of getchar. getchar is probably getc/fgetc which is probably buffered.
 
> When i use 'getchar();' it will take any numberof characters until return is pressed.
No, the OS buffers them until return is pressed, then your program loops until it gets to the '\n' character.

You might try calling setvbuf() before any other stdio call in your program, but that will only stop buffering inside the standard library. It may be still buffered within the OS.

How about posting some code showing how it "doesn't work" as you expect it.

If you're looking for something like "getch" functionality from the old conio.h, then I'd suggest you need something like this (from here)
Code:
#include <iostream> 
#include <windows.h> 

bool keyHit(void)
{
  HANDLE  stdIn = GetStdHandle(STD_INPUT_HANDLE);

  DWORD   saveMode;
  GetConsoleMode(stdIn, &saveMode);
  SetConsoleMode(stdIn, ENABLE_PROCESSED_INPUT);

  bool  ret = false;

  if (WaitForSingleObject(stdIn, 1) == WAIT_OBJECT_0) ret = true;

  SetConsoleMode(stdIn, saveMode);

  return(ret);
}

bool getChar(TCHAR &ch)
{
  bool    ret = false;

  HANDLE  stdIn = GetStdHandle(STD_INPUT_HANDLE);

  DWORD   saveMode;
  GetConsoleMode(stdIn, &saveMode);
  SetConsoleMode(stdIn, ENABLE_PROCESSED_INPUT);

  if (WaitForSingleObject(stdIn, INFINITE) == WAIT_OBJECT_0)
  {
    DWORD num;
    ReadConsole(stdIn, &ch, 1, &num, NULL);

    if (num == 1) ret = true;
  }

  SetConsoleMode(stdIn, saveMode);

  return(ret);
}

TCHAR getChar(void)
{
  TCHAR ch = 0;
  getChar(ch);
  return(ch);
}

int main(void)
{
  std::cout << "Press a key" << std::endl;
  getChar();
  std::cout << "Done" << std::endl;
}

--
 
getch give an implicit declairation error and getchar will take in any number of characters until it gets the \n

i know getchar isnt supposed to do that ive used it to get a single character before without the carrage return, i just cant understand why it doesnt work now.

---------------------------------------
2b||!2b that == the_question
 
Post code which shows the problem.
We can't do anything but make random guesses until you do.

--
 
#include <stdio.h>
#include <conio.h>

int main()
{
puts ("Press any key to quit");
getchar();

return 0;
}

---------------------------------------
2b||!2b that == the_question
 
If you replace getchar() with getch() or getche() it should work, but only on Windows.
 
Both of those give implicit declairation errors.

I'm using windows 2000

---------------------------------------
2b||!2b that == the_question
 
Please see the sample code i posted, second line

---------------------------------------
2b||!2b that == the_question
 
What compiler are you using? That should work fine on Visual C++ 6.0.
 
I'm using Dev C++ 4

---------------------------------------
2b||!2b that == the_question
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top