Mobile message composer

You have to write a program that works in the same fashion as a message composer in the
mobiles.
Write a program that compose message in the same fashion as using above 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 use the following library
#include <time.h>
To get the current time use following function
time(0)
This function will return numeric value of time. You can compute the time difference by getting
time at both key press events and test for a fixed time delay to check whether the key press are
consecutive or not.
If you want to clear screen use following statement
System(“CLS”);
Similarly find the associated letters with number and complete your
assignment.
Program Requirement. You cannot use
string and character array. Only primitive data types are allowed. You are allowed to use header
files that is mentioned in the assignment or may use other libraries.

Last edited on
Which OS/Compiler are you using?
Because the first major problem is how you're going to read keys without having to wait for enter to be pressed.

> You are allowed to use header files that is mentioned in the assignment
Oh pray, do tell.

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <conio.h> 

int main()
{
	if (_getch() == 50)
	{
		std::cout << "A";
	}
}


You'll need _getch() from the header <conio.h> . When accepting integer inputs, understand that _getch() only returns characters. So _getch() will treat an integer, like "2", as a character. Meaning you CAN'T do this:

1
2
3
4
if (_getch() == 2) //Wont Be True If User Presses 2
{
     //Code
}


Instead, you'll need to find the actual integer value of the character used to represent the digit inputs - Use the ASCII table for that - or do this:

1
2
3
4
if (_getch() == '2')
{
     //Code
}


As you can see, 50 represents the character '2', so you'd need either one of those in your if statement.
Last edited on
Salem c
I m using dev c++
You have to write a program that works

Kind of an order?

First Second draft:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <time.h>
using namespace std;

int main()
{
    char usrinpt(42), prusript(42), a(42);
    unsigned pntr(0), display[99], timesec[1], delta, maxdelta(3);
    unsigned startwith[] {32, 44, 65, 68, 71, 74, 77, 80, 84, 87};

    cout << "Enter single digits 1..9, one at a time to compose letters,\n";
    cout << "'x' to discard the last one; 'c' to restart; 'q' to quit.\n\n";
    
    timesec[0] = time(0) - maxdelta - 1;        // runin cycle
    
    for (;;)    // do forever
    {
        cin >> usrinpt;
        timesec[1] = time(0);
        delta = timesec[1] - timesec[0];

        switch (usrinpt)
        {
            case 'x':
                if (pntr > 0) --pntr;
            break;
            case 'c':
                pntr = 0;
            break;
            case 'q':
                return(0);
            default:
            switch ((usrinpt == prusript? 0 : 1)+(delta <= maxdelta? 0 : 2))
            {
                case 0:;    // input repeated within delay
                {
                    if (usrinpt > 49 && usrinpt < 58)           // 2..9 only, 0 and 1 later
                        display[pntr] += 1;                     // replace last entry with next letter
                    else if (usrinpt == 48)                     // 0 is for blank only, disregarding delay
                        display[++pntr] = 32;                   // increment pointer _before_ asignment
                    else if (usrinpt == 49)                     // 1 is for some punctuation
                        switch (display[pntr])
                        {
                            case 44:
                                display[pntr] = 46;
                            break;
                            case 46:
                                display[pntr] = 58;
                            break;
                            case 58:
                                display[pntr] = 59;
                            break;
                            default:
                            display[pntr] = 44;
                        }   //  punctuation
                    else
                        cout << "Programmers fault, input not handled: " << usrinpt << "\n";
                    break;
                }   // same entry within delay
                default:    // different input than before and/or (vel) delay expired
                pntr += 1;                                  // increment pointer
                if (usrinpt > 49 && usrinpt < 58)           // 2..9 only, 0 and 1 later
                    display[pntr] = startwith[usrinpt - 48];   // append new char
                else if (usrinpt == 48)                     // 0 is for blank only
                    display[pntr] = 32;
                else if (usrinpt == 49)                     // 1 is for punctuation
                    display[pntr] = 44;
                else
                    cout << "Programmers fault, input not handled: " << usrinpt << "\n";
            }   // reduced 4 cases
/*          default:    // should never come here */
        }   //  user input including commands c, q, and x
        timesec[0] = timesec[1];
        prusript = usrinpt;

        cout << "Display: ";
        for (uint i = 0; i <= pntr; i++)
        {
            a = display[i];
            cout << a;
        }
        cout << "\n";
    }   //  do forever
}


1st Edit: output example added, removed typo in comment, removed a comment, corrected the error message, added a note.
2nd Edit: So far nobody moaned about the bug? Replaced first draft by a second one, disentangled the four cases (new entry?) "vel" (delay expired?).

Note: the delay is set by var maxdelta, adjust it to your gusto (at will).

Enter single digits 1..9, one at a time to compose letters,
'x' to discard the last one; 'c' to restart; 'q' to quit.

2
Display: A
3
Display: AD
3
Display: AE
q
 
Exit code: 0 (normal program termination)
Last edited on
Topic archived. No new replies allowed.