Jump to content
  • Advertisement
Sign in to follow this  
romainechester

reading a character(no need to press enter)

This topic is 4516 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
What about getch() in conio.h? If you use it, you need to strategically flush streams and that sort of thing on occasion, because it doesn't always play nice with stdio.

Share this post


Link to post
Share on other sites
I'm sorry, but I've never used that compiler. You'd have to check out the headers and see if you have conio.h. I just checked VS.NET 2003 and it has it.

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
Your notion of a C++ string is broken:

#include <string>

...

std::string word;
char letter;

...

word += letter;

Share this post


Link to post
Share on other sites
Quote:
Your notion of a C++ string is broken

I'm sorry, I didn't get that..

Hmm... Is there any other way? I'm just using C and I don't want to use things from C++

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!