- #1
whysoserious2
- 10
- 1
- Homework Statement
- Write a program to read a sequence of non-empty keystrokes and output the translated message and the number of characters typed. Note that the "space" should be counted as a character in the output.
- Relevant Equations
- Assume that the input message will not start with 0 and the length of the input message always consists of 17 digits.
Been trying to work this out for a while now but struggling to come up with a program that reads all of the samples. Based on the keypad, I should be receiving an "a" when I press "2" once. However, when I put in the code "222", I still get "a" instead of "c". I've attached the codes I put in and would appreciate any help I can get!
C++:
#include <iostream>
#include <string>
using namespace std;
int main( )
{
int i;
string message;
cout << "Please enter the message: ";
cin >> message;
cout << endl;
for (i = 0; i < message.size(); i++)
{
if (message[i] == '#') cout << '#';
if (message[i] == '1') cout << ' ';
if (message[i] == '222') cout << 'C';
else if (message[i] == '22') cout << 'B';
else if (message[i] == '2') cout << 'A';
if (message[i] == '444') cout << 'I';
else if (message[i] == '44') cout << 'H';
else if (message[i] == '4') cout << 'G';
}
cout << endl;
return 0;
}
Attachments
Last edited by a moderator: