Trouble in programming...

Hi,
I'm a beginner of studying C++, and now I'm facing a problem.
I have an off-class practice which let me write a program to show the way of input of a mobile phone. The task is below:

The diagram below shows the layout of a mobile phone keypad. Each key presents 3-4 lowercase letters and the digit associates with the key itself. To display a letter or a digit, a sequence of keystrokes is required. The table next to the keypad gives a few examples on how sms is produced by pressing the keys. Note that depending on the number of times a key is being pressed, there are more than one ways to display a letter or a digit. As for other keys in this keypad, key 0 produces a space in the sms, key 1 produce the digit '1', key * marks the end of the keystrokes, and the key # does not have any effect in the generation of sms. Keystrokes from user sms displayed (italics are comments, not the actual display)

2 a
22 b
222 c
2222 2
222222 b
888888888888 8
0 a space is displayed
00 0
000 a space is displayed
# Nothing is displayed
1 1
11 11
* This marks the end of sms

Using the rules above, to write "hi", the user will press 44, wait for the 'h' to appear and then press 444 for the letter 'i'. We are going to represent this sequence of keystrokes as 44-444, where - means the user pause for a while before pressing the next key. As an example, the keystrokes 44-44408443377733 will produce "hi there" on the screen.

The program

Develop a program which reads in a sequence of keystrokes and then display the sms with the character count to the screen. The input is a sequence of keystrokes (any combination of 1234567890#) terminated by *. There is no limit in the length of input. The output consists of 2 lines: the first line is the content of the sms according to the user input; the second line is a number representing the total characters generated.

Many thanks!
Last edited on
We won't write the hole progtam for you, so what exactly is you problem?
I just do't know how to detect the input char and at the same time count the time. And how can I output the numbers in the same line. Thank you.
In windows you can set the cursor in the console with the function SetConsoleCursorPosition (http://msdn.microsoft.com/en-us/library/ms682073.aspx). There you can also find an example.

To detect your input you can use something like this:
1
2
3
4
5
6
7
8
9
10
11
12
while (1){
 if(kbhit()){
  key = getch();
  if(!key){// extended keycode
   key = getch();
  }
  printf("%i %x \"%c\"\n", key, key, key);
  if(key == 13){
   break;
  }
 }
}


You could also use C++'s cin.

You do not realy have to count the time while you enter you string, but you can save the time every time after a key has been pressed and compare the current time to the saved time. if the difference is greater than x you write a new character, else you set the cursor one position back and change the actual character.
Last edited on
Topic archived. No new replies allowed.