Hello there, I need some help on how I could replace the getch() function.
Basically, my assignment was to make a simple simulated "joystick" program.
You press the letters a,b,c,d... and it will output the number pressed...
The program works and everything, but i found out im not allowed to use getch() from the conio platform because it's Borland specific... Any ideas on what can replace the getch() issue? I've tried cin.get, getline, etc... but it doesnt work the same because you have to press enter every single time. Any help is much appreciated. :-)

#include <cstdlib>
#include <iostream>
#include <conio.h>

using namespace std;

typedef void (*Func)();

void zero();
void one();
void two();
void three();

int main(int argc, char *argv[])
{
    char data;
    Func func[4];
    func[0] = &zero;
    func[1] = &one;
    func[2] = &two;
    func[3] = &three;
    
    while((data = cin.getch()) != '\033') 
    {          
      if(data>='a' && data <='d')
         func[(int)data-97]();
    } 
    system("PAUSE");
    return EXIT_SUCCESS;
}

void zero()
{
    cout<<"Zero was pressed!!!"<<endl;
}

void one()
{
    cout<<"One was pressed!!!"<<endl;
}

void two()
{
    cout<<"Two was pressed!!!"<<endl;
}

void three()
{
    cout<<"Three was pressed!!!"<<endl;
}

Generally speaking getch() is not "Borland specific". It's a function declared in <conio.h> - commonly used and partially supported but not standardized console input/output header. Sometimes its name is _getch() but as usually in that case conio.h includes macros for "deprecated" getch.

It's impossible to inplement full getch() functionality with C and C++ streams i/o. Don't waste time on this hopeless attempts. Stream and console concepts differs. No console concept in C and C++ standards.

However it's possible to accomodate C++ stream model with platform specific console i/o on streambuf class level. It's not so easy job (and, probably, not so hard).

Almost exactly what I was going to say. Your professor is giving you a hard time for using getch()? I would be hard-pressed to find a modern 32-bit Windows compiler that doesn't support it. And only a little less hard-pressed to find an old 16-bit DOS compiler that doesn't. It may have been a Borland invention, but its popularity and availability is undisputed.

Ask your professor, if you aren't allowed to use a common function to do unbuffered input, exactly what should you use?

Is he going home and compiling your assignments on a Linux box or something?

>but it doesnt work the same because you have to press enter every single time.
Be clear about your assignment first. Are you required to use raw input to achieve the correct behavior, or did you use getch because you personally think the result is better? This might be a case of you going above and beyond the program specification, but in doing so you ended up throwing away portability.

If it's the former, ask your instructor what you're allowed to use, because there's no standard solution. If it's the latter, there's a standard solution (though it may be less user-friendly) and you're probably expected to use it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.