Sign in to follow this  
romainechester

reading a character(no need to press enter)

Recommended Posts

Umm.. yeah.. actually the title says it all. I wonder how can I do per character input so that I can filter it? The code looks somewhat like this:
int isLooping = 1;
char currEntry;
char wholeWord[100];

while(isLooping) {
  // The function or code that I need goes here
  // scanf(), getchar() doesn't work for me

  // Could also be an if..else block
  switch(currEntry) {
    case 'a': // Do stuff
      break;
    case '9': // Do some other stuff
      break;
    default:
      break;
  }
  strcat(wholeWord, currEntry);
}
Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
Quote:
Original post by romainechester
hmm.. yes I know but do all compilers have that? how bout mingw?


getch() isn't a standard function, but it's been around long enough that every compiler has a version of it. It is considered non portable, however, and it is accessed differently on every platform. However, C++ only specifies line based input functions - it does not specify individual key based input.

Ming is based on GNU GCC, which has getch() defined in curses.h.

Share this post


Link to post
Share on other sites
I recently made a posting on this exact thing. I detailed how it worked differently between compilers, sometimes having entirely different functionality (though a similar idea.) I noticed that on an old version of devc++ getch would require you to press enter in some cases but getche never would. I don't like relying on conio, but it's really the only solution aside from handing your input and display for console programs over to something like SDL.

Note: Devc++ doesn't even pretend to like conio, they state directly that the limited functionality they -do- have is for convenience, but the whole conio.h and conio.cpp files were not implemented last I checked. Just a portion of it. Most compilers should have it though, but that's a problem if you plan on distributing your source. It's one thing to have platform dependant code, another entirely to have compiler dependant code. I generally avoid the second unless I'm also making platform dependant code in which case you can justify it a bit more (ie: using Visual Studio for some of it's windows functionality would mean that it's both platform and compiler dependant which is alright considering you have a specific target and the programmers who want your code will probably have the same compiler for that platform. However, let's say I'm developing a cross platform toolset and releasing it open source. It would not really be a good idea to implement librarys that are sometimes included and sometimes excluded and sometimes implemented totally differently depending on the compiler without providing some means to access those dependant libs.)

Share this post


Link to post
Share on other sites
If you are on a windows OS you can go with the GetAsyncKeyState function


#include <iostream>
#include <windows.h>

int main()
{
while(1)
{
if(GetAsyncKeyState(VK_UP)) {
std::cout << "up is pressed" << std::endl;
}
else std::cout << "up is not pressed" << std::endl;

if(GetAsyncKeyState(VK_RETURN))
break;
}

return 0;
}




It does not work exactly like the getch funtion though. You would have to keep track of what keys are already pressed/released.

Share this post


Link to post
Share on other sites
Hmm... I have another question. I'm more used to java than in C so I was wondering how to append a character to a String?


typedef char String[100];

// lets assume that there's already a value
char someChar;
String word; // No value yet..

// This doesn't work.. There's a warning and when I run it, the program crashes
strcat(word, someChar);

Share this post


Link to post
Share on other sites
A c-style string must be null terminated.
What happens is that your string is initially filled with garbage (random data)
The strcat will search the string for the first occurance of the null terminator ('\0') and append the char from there. In this case it will keep woking its way into memory eterity to find one, and when/if it does it will attempt to write the character to that point in memory. Since this memory is not allocated to your process the program will crash at that point.

char someChar;
String word;

// Add a "end of string" mark at the beginning to empty the string
word[0] = '\0';

// should now be added at the beginning. The strcat will append the new null
// terminator for you at the end.
strcat(word, someChar);

Be aware that the functions strncat/strncpy do not append the null terminator for you

edit:
By the way, the strcat function expects tow strings, not a string and a char.
To add a char at the end, try

int len = strlen(word);
word[len] = someChar;
word[len + 1] = '\0';



[Edited by - pulpfist on March 12, 2006 1:56:01 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this