vendredi 22 mars 2019

Mobile keypad Program in C++

I have Wrote a program that compose message in the same fashion as keypad. The program must receive integer input. When use enter 2 the program outputs letter ‘A’ and if 2 is pressed twice, the letter ‘A’ will disappear from screen and character ‘B’ will appear and if 2 is pressed thrice the letter ‘B’ will disappear and Letter C will appear. To enter ‘AA’ you have to enter number ‘2’ twice with delay. Delay must be as smaller as it is usually in the mobile. To check a delay between two key press i have use the time.h library

To get the current time use following function time(0). I have to only use primitive data types. Looking forward for positive feedback.

#include <iostream>
#include <string.h>
#include <conio.h>
#include <time.h>
using namespace std;

main()
{
    char value;
    int exit = 1;
    int timeChk;
    time_t start, end;
    cout << "To Exit press c" << endl;
    while (exit == 1)
    {
        start = time(0);
        value = getch();
        end = time(0);
        timeChk = float(difftime(end, start));
        if (int(value) == 27)
        {
            exit = 0;
        }
        if (timeChk < 1)
        {

            if (strcmp(value, '222'))
                cout << "c";
            else if (strcmp(value, '22'))
                cout << "b";
            else if (strcmp(value, '2'))
                cout << "a";
            else if (strcmp(value, '333'))
                cout << "f";
            else if (strcmp(value, '33'))
                cout << "e";
            else if (strcmp(value, '3'))
                cout << "d";
            else if (strcmp(value, '444'))
                cout << "i";
            else if (strcmp(value, '44'))
                cout << "h";
            else if (strcmp(value, '4'))
                cout << "g";
            else if (strcmp(value, '555'))
                cout << "l";
            else if (strcmp(value, '55'))
                cout << "k";
            else if (strcmp(value, '5'))
                cout << "j";
            else if (strcmp(value, '666'))
                cout << "o";
            else if (strcmp(value, '66'))
                cout << "n";
            else if (strcmp(value, '6'))
                cout << "m";
            else if (strcmp(value, '7777'))
                cout << "s";
            else if (strcmp(value, '777'))
                cout << "r";
            else if (strcmp(value, '77'))
                cout << "q";
            else if (strcmp(value, '7'))
                cout << "p";
            else if (strcmp(value, '888'))
                cout << "v";
            else if (strcmp(value, '88'))
                cout << "u";
            else if (strcmp(value, '8'))
                cout << "t";
            else if (strcmp(value, '9999'))
                cout << "z";
            else if (strcmp(value, '999'))
                cout << "y";
            else if (strcmp(value, '99'))
                cout << "x";
            else if (strcmp(value, '9'))
                cout << "w";
        }
        else
        {
            switch (value)
            {
            case '0':
                cout << endl;
                break;
            case '1':
                cout << ".";
                break;
            case '2':
                cout << "a";
                break;
            case '3':
                cout << "d";
                break;
            case '4':
                cout << "g";
                break;
            case '5':
                cout << "j";
                break;
            case '6':
                cout << "m";
                break;
            case '7':
                cout << "p";
                break;
            case '8':
                cout << "t";
                break;
            case '9':
                cout << "w";
                break;
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire