Answer to Question #258335 in C++ for Charmaine

Question #258335

Write a program that check and classify buttons pressed on the keyboard.


After pressing any button, the program


should display one of labels: - lowercase letter


- capital letter


- digit


- ENTER


- ESC


- Left Arrow


- Right arrow


- F1 function key


- Another (unrecognised) key


HINT: to retrieve the keyboard key code, use function getch() from the library <conio.h>.


1
Expert's answer
2021-10-29T01:50:02-0400
#include <conio.h>
#include <iostream>
#define KEY_LEFT  75
#define KEY_RIGHT 77
#define DIGIT     48-57


using namespace std;


int main()
{
    int c, ex;




    while(1)
    {
        c = getch();




        if (c>=48 && c<=57)
        {
            cout << endl << "Digit :" << (char) c << endl;
        }
        else if (c>=65 && c<=90)
        {
          cout << endl << "Capital letter :" << (char) c << endl;
        }
         else if (c>=97 && c<=122)
        {
          cout << endl << "lowercase letter :" << (char) c << endl;
        }
            else if (c>=27 && c<=27)
        {
          cout << endl << "ESC :" << (char) c << endl;
        }
        else
        {
            switch(ex = getch())
            {
                case KEY_LEFT   /* M */:
                    cout << endl << "Left Arrow" << endl;  // key left
                    break;
                case KEY_RIGHT: /* P */
                    cout << endl << "Right Arrow" << endl;  // key right
                    break;
                default:
                    cout << endl << (char) ex << endl;  // not arrow
                    break;
            }
        }
    }




    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS