2

I have searched over the internet for this specific method but there was not anything what I was looking for. I wrote this program which takes input in integers and prints message( as in numeric keypad of cellphones). What I want to do with this program is to take input in one line as

Enter the code to crack : 454545479833165445

and the corresponding message gets printed. Rather than

Enter the code to crack :55
Enter the code to crack : 666

and prints message when I press a specific key which is -1 in this case.

  #include <iostream>
  using namespace std;

int main()
{
int a;
string n;
do{
    cout << "Enter the code to crack";
    cin >>a;

    switch (a){

    case 0:
    {


    n=n+" ";}
    break;

    case 1:
        {


    n=n+".";}
    break;

    case 11:
        {


    n=n+",";}

    break;

    case 2:{

    n=n+"a";}
    break;

    case 22:
    n=n+"b";
    break;

    case 222:
    n=n+"c";
    break;

    case 3:
    n=n+"d";
    break;

    case 33:
    n=n+"e";
    break;

    case 333:
    n=n+"f";
    break;


     case 4:
    n=n+"g";
    break;

    case 44:
    n=n+"h";
    break;

    case 444:
    n=n+"i";
    break;

     case 5:
    n=n+"j";
    break;

    case 55:
    n=n+"k";
    break;

    case 555:
    n=n+"l";
    break;


     case 6:
    n=n+"m";
    break;

    case 66:
    n=n+"n";
    break;

    case 666:
    n=n+"o";
    break;

     case 7:
    n=n+"p";
    break;

    case 77:
    n=n+"q";
    break;

    case 777:
    n=n+"r";
    break;


     case 7777:
    n=n+"s";
    break;

    case 8:
    n=n+"t";
    break;

    case 88:
    n=n+"u";
    break;

     case 888:
    n=n+"v";
    break;

    case 9:
    n=n+"w";
    break;

    case 99:
    n=n+"x";
    break;

     case 999:
    n=n+"y";
    break;

    case 9999:
    n=n+"z";
    break;}
} while(a!=-1);
cout <<"The decoded message is :" << n;

return 0;
}
vaultah
  • 44,105
  • 12
  • 114
  • 143
Ummmm
  • 21
  • 4
  • 1
    Welcome to stackoverflow. stackoverflow is not a forum. – Oswald Jun 20 '15 at 07:56
  • 1
    I am curious, what the [indenting style](https://en.wikipedia.org/wiki/Indent_style) it is. – Hi-Angel Jun 20 '15 at 08:06
  • Thank you, it is a pleasure indeed. Any help would be appreciated. :) – Ummmm Jun 20 '15 at 08:08
  • Well, this is heart wrecking to mark this question as not useful, I am new into programming and I did search over the internet for this solution, No one was born a professional if the people here are not willing to help then atleast do not discourage by marking this as not useful or research not done. – Ummmm Jun 20 '15 at 08:21
  • What is the question being asked here? – PC Luddite Jun 20 '15 at 08:22
  • @Ummmm What about using spaces in the input line: `45 454 5 47 98 331 654 45 -1`? – πάντα ῥεῖ Jun 20 '15 at 08:22
  • @pcluddite Why they can't take the input from a single line. – πάντα ῥεῖ Jun 20 '15 at 08:24
  • 2
    @Ummmm The number `454545479833165445` is much too large to be storing in an `int` – PC Luddite Jun 20 '15 at 08:28
  • If you don't want to enter separators, how can you differentiate between "9", "99", and "999"? – Jongware Jun 20 '15 at 09:03
  • @πάνταῥεῖ That is pretty awesome if it can be done that way I would love to write it that way, or using a seperator. – Ummmm Jun 20 '15 at 11:41
  • @pcluddite Sir long long can be used to store that amount of numbers I guess. The question was to take input in just one line rather then prompting user to hit enter and then taking another input. – Ummmm Jun 20 '15 at 11:42
  • @Ummmm `cin` skips whitespace delimiters automatically so your code should just work with that from of input. Alternatively, just take a whole line using `getline()` as input, and extract the number parts (works even without `-1` in the end). See [this question and answer](http://stackoverflow.com/questions/24504582/how-to-test-whether-stringstream-operator-has-parsed-a-bad-type-and-skip-it) how to read numbers separated by whitespace from a `std::istringstream`. – πάντα ῥεῖ Jun 20 '15 at 14:19

1 Answers1

2

If you want to process one signle input, assuming that the many digits that a message could contain would certainly overflow even he lognest integer type, you have to use a string input, and iterate through its chars:

string a;
string n;

cout << "Enter the code to crack";
cin >>a;   // you'll get everything, but each digit is a char 

for (int i=0; i<a.size(); i++) {
    //... process each separate digit a[i] here 
}
cout <<"The decoded message is :" << n;

Each digit a[i] is then between '0' and '9'. You'll have to take care of two difficulties:

  • the string might contain illegal caracters (for example alphanumeric), so you have to handle these cases, issuing an error message
  • each string arives separately. So no 44 anymore, but a '4' and another '4'. So you have to take into account in you ralgorithm the previous digit, to see if you have fo shift your last output letter by one, or if you have a new output letter.
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • In your second bullet: "string" -> "character". The shifting is necessary for hexadecimal; with pure decimals, it would need *multiplying* (by 10, and a rule that says "numbers are 2 digits only), but the OP does not need this at all. – Jongware Jun 20 '15 at 08:50
  • .. wait, the OP *does* need decimals - and so, indeed, more code to differentiate between "9", "99", and "999". – Jongware Jun 20 '15 at 09:01
  • 1
    @Jongware I don't want to [spoil](http://ideone.com/qDFCeB) the exercise the OP has to do. The principle on the telephone, it that digits arrive one by one (at least on my telephone I don't have a key 99). So OP has to keep track of the context and repetitions. There a re many ways to do it (spliting into substrings, keeping a status, etc...). – Christophe Jun 20 '15 at 09:25
  • Thank you for the effort you have put in to solve this question but please do not mind me as I am totally fresh in coding and does not know much. Where am I supposed to use that code I mean do I need to assign the *string n* value? the letters related to numbers or what? This is new for me.@Christophe – Ummmm Jun 20 '15 at 11:56
  • @Ummmm if you read a string, all the symbols keyed in are put in a sequence of characters. The character 0x30 for example represents (in ASCII coding) the symbol '0'. It is provided as-is, without conversion. This is different of reading an int, where the digits typed in are directly converted into a number. If you click on the link in my comment above, you'll find the full example to let you understand how this could work in detail. – Christophe Jun 20 '15 at 13:52
  • 1
    @Ummmm If you're sure a character is an integer, you could always subtract `'0'`, eg `a - '0'`, assuming that `a` is a character that is a number, this will convert it to an `int` with the corresponding value. If the answer is greater than 9 or less than 0, you know that the character wasn't an integer. – PC Luddite Jun 20 '15 at 18:04
  • @Christophe, Sir, I tried a different approach using long long and double int but even then the code doesn't work as desired. Perhaps, A time delay or a seperator will let the program perform efficiently. For instance if I want to type "aba" then the combination will be "2222" And the output is "CA". Here is a code snippet for better understanding. – Ummmm Jun 21 '15 at 09:55
  • long long num=0; long long x=0; long long c=1; cout<<"Enter The Required Number .......= "; cin>>num; x = num; while(num>0) { switch(num) { case 9999: { cout<<"Z"; //Print telephone Digit against given number... num = x; num = num%c; //Show Modulus Of Given number with c c=1; } break; default: { num = num/10; //Dividing input by 10 c = c*10; } } } getch(); } – Ummmm Jun 21 '15 at 09:57
  • If you are working with `long long` (which could work for messages of less than 18 keys) the problem would be to divide the big number into meaningfull chunks (instead of regrouping digits in bigger chunks). You'll avoid having to deal with characters , at an extra cost of complexitiy with divisions and modulos. By the way, : num=num/10 means that you're reading starting from the back. – Christophe Jun 21 '15 at 12:02
  • @Christophe Alright, that logic is not working for the program as it prints vague and undesired messages as soon as the digit limit exceeds. Is there anyway that I can make the program aware of differentiating between 2 and 22 in "222"? And thank you for being patient and increasing my knowledge. I look forward to make this program work efficiently by tonight. – Ummmm Jun 21 '15 at 14:00
  • 1
    @Ummmm all 3 are valid combinaions. So encountering 222 could either be 222 or 2 and 22 or 22 and 2. With string input, you could imagine entering a "-" to show that there is a pause: 2-22 or 22-2 or 222 would then be clearly differenciated. – Christophe Jun 21 '15 at 20:15