Problem with using maps..

I'm solving one of the problems in UVa Online Judge. Problem statement below.


A common typing error is to place your hands on the keyboard one row to the right
of the correct position. Then “Q” is typed as “W” and “J” is typed as “K” and so on.
Your task is to decode a message typed in this manner.

Input
Input consists of several lines of text. Each line may contain digits, spaces, uppercase
letters (except “Q”, “A”, “Z”), or punctuation shown above [except back-quote (‘)].
Keys labeled with words [Tab, BackSp, Control, etc.] are not represented in the input.

Output
You are to replace each letter or punctuation symbol by the one immediately to its left
on the QWERTY keyboard shown above. Spaces in the input should be echoed in the
output.

Sample Input

O S, GOMR YPFSU/

Sample Output

I AM FINE TODAY.


See the Problem at UVa Online Judge http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=1023


I developed a solution using maps. But I'm having two issues.

1: The last character of the input is printed twice, and
2: Blank space is missing in the output,

For exaaple,

for the sample input
O S, GOMR YPFSU/

i'm getting output
IAMFINETODAY..

Could anyone please point out what is wrong? Thanks.


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
#include <iostream>
#include <map>

using namespace std;

int main()
{
        map <char,char> keymap;

        keymap['W'] = 'Q';
        keymap['E'] = 'W';
        keymap['R'] = 'E';
        keymap['T'] = 'R';
        keymap['Y'] = 'T';
        keymap['U'] = 'Y';
        keymap['I'] = 'U';
        keymap['O'] = 'I';
        keymap['P'] = 'O';
        keymap['['] = 'P';
        keymap['S'] = 'A';
        keymap['D'] = 'S';
        keymap['F'] = 'D';
        keymap['G'] = 'F';
        keymap['H'] = 'G';
        keymap['J'] = 'H';
        keymap['K'] = 'J';
        keymap['L'] = 'K';
        keymap[';'] = 'L';
        keymap['\''] = ';';
        keymap['X'] = 'Z';
        keymap['C'] = 'X';
        keymap['V'] = 'C';
        keymap['B'] = 'V';
        keymap['N'] = 'B';
        keymap['M'] = 'N';
        keymap[','] = 'M';
        keymap['.'] = ',';
        keymap['/'] = '.';
        keymap[' '] = ' ';

        char a;
        map <char, char>::iterator itr;
        while(!cin.eof())
        {

                cin >> a;
                itr = keymap.find(a);
                if( itr == keymap.end() )
                        cout << a;
                else
                        cout << itr->second;//keymap[a];
        }
        cout << endl;

        return 0;
}
Last edited on
Use std::getline(). std::cin >> explicitly ignores whitespace. Another alternative would be std::cin.get().
Topic archived. No new replies allowed.