Sunday, November 23, 2014

Conio.h ugh.

Disappointed how c/c++ programmers do not take the time to make their code portable. Take for instance the legacy code:

    #include<iostream>
    #include<conio.h>
    using namespace std;
    int main()
    {
        char key_press;
        int ascii_value;
        cout<<"\n\t\t\tPress Any Key To Check  Its ASCI Value\n\n\t\t\tPress ESC to EXIT\n\n\n";
        while(1)
        {
        key_press=getch();
        ascii_value=key_press;
        if(ascii_value==27) // For ESC
         break;
       cout<<"\t\t\tKEY Pressed-> \" "<<key_press<<" \" Ascii Value =  "<<ascii_value<<"\n\n";
        }
        return 0;
    }



Would not compile on our linux machine.  There is not a conio.h for linux, but you can find compatible code on the net to act as a replacement. After downloading the code and moving to the right place, things began to work. Here is the updated code to work on linux.

#include<curses.h>
#include<conio.h>
#include<iostream>
    using namespace std;
    int main()
    {
    char key_press;
    int ascii_value;
    cout<<"\n\rPress Any Key To Check  Its ASCI Value\n\rPress ESC to EXIT\n\r";
    while(1)
    {
    key_press=getch();
    ascii_value=key_press;
    if(ascii_value==27) // For ESC
     break;
   cout<<"\tKEY Pressed-> \" "<<key_press<<" \" Ascii Value =  "<<ascii_value<<"\r\n";
    }
    return 0;
    }

Then to compile it you would use:

$  g++  getthecharcode.c -lncurses -o myprog

Hope this helps those who have problems moving code from legacy platforms to linux.




No comments:

Post a Comment