Not Printing

I'm trying to create a simple encryption program. You type in what you want encrypted. Then it evaluates the letters and prints out the encrypted ones.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main()
{
    char word[800];
    
    cin >> word;
    
    if (word[0] == 'a')
    {
                cout << "%";
    }
    
    else if (word[1] == 'b')
    {
         cout << "7";
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

I run this. Then I type "ab". It should print "%7", but it only prints "%". If you could help that would be great.
Right here:
1
2
3
4
5
6
7
8
9
if (word[0] == 'a')
    {
                cout << "%";
    }
    
    else if (word[1] == 'b')
    {
         cout << "7";
    }

The code inside the second if statement will never be run if the first one is true, even if word[1] is 'b'.
Just get rid of the "else" so the second if statement doesn't depend on the first one being false.
Last edited on
use strings rather than a char array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <limits>

int main()
{
    std::string word;
    getline(std::cin, word);
    
    if (word.at(0) == "a")
    {
        std::cout << "%";
    }
    
    if (word.at(1) == "b")
    {
	std::cout << "7";
    }
    
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return 0;
}
Last edited on
Thanks. I totally forgot about the "else" part.
Any idea on how to shorten the code. So far I've been having to do:

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
if (word[0] == 'a')
    {
         cout << "%";
    }
    
    if (word[0] == 'b')
    {
         cout << "7";
    }
    
    if (word[0] == 'c')
    {
         cout << "+";
    }
    
    if (word[0] == 'd')
    {
         cout << "1";
    }
    
    if (word[0] == 'e')
    {
         cout << "&";
    }
    
    if (word[0] == 'f')
    {
         cout << "/";
    }
    
    if (word[0] == 'g')
    {
         cout << ":";
    }
    
    if (word[0] == 'h')
    {
         cout << "~";
    } 

//Down the code some

     if (word[1] == 'a')
    {
         cout << "%";
    }
    
    if (word[1] == 'b')
    {
         cout << "7";
    }
    
    if (word[1] == 'c')
    {
         cout << "+";
    }
    
    if (word[1] == 'd')
    {
         cout << "1";
    }
    
    if (word[1] == 'e')
    {
         cout << "&";
    }
    
    if (word[1] == 'f')
    {
         cout << "/";
    }
    
    if (word[1] == 'g')
    {
         cout << ":";
    }
    
    if (word[1] == 'h')
    {
         cout << "~";
    }
you could store a map with the letters as the key value and symbols as the mapped value, then just loop through the word and replace each character http://www.cplusplus.com/reference/stl/map/

Last edited on
That kind of looks complex, and I am a begginer. I understand what it is. I just don't understand the coding. Could you explain it, or give me another idea.
A switch statement word do nicely:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for (size_t i = 0; i < word.length (); ++i)
  {
    switch (word[i])
      {
      case 'a':
        cout << "%";
        break;
      case 'b':
        cout << "7";
        break;
      //same for the rest

      default:
        // Unrecognized value.  Handle it however you wish.
      }
  }

Read about the switch statement here: http://www.cplusplus.com/doc/tutorial/control/#switch


Alternatively, you can use strings that map decrypted values to encrypted values:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const char *decrypted = "abcdefgh";
const char *encrypted = "%7+1&/:~";
string word = "bag"; //for example

for (size_t i = 0; i < word.length (); ++i)
  {
    size_t n = decrypted.find (word[i]);
    if (n == string::npos)
      {
        // The character in `word' was not found in `decrypted'.  Handle the problem however you wish.
      }
    else
      {
        cout << encrypted[n];
      }
  }


Notice that I said "map", just as quirkyusername did earlier. The STL provides std::map via the <map> header:
1
2
3
4
5
6
7
8
9
10
map<char, char> encryption_map;
encryption_map['a'] = '%';
encryption_map['b'] = '7';
// Same for the rest of the characters.
// It is probably best to delegate initialization to an initialize_map() function to keep this out of your main program.

for (size_t i = 0; i < word.length (); ++i)
  {
    cout << encryption_map[word[i]];
  }


In all cases, a loop is required.
Last edited on
A map is overkill in this case, I recommend a simple array:
1
2
3
char map[9]="%7+1&/:~";
cout<<map[word[0]-'a'];
cout<<map[word[1]-'a'];

Obviously you'll need to verify your input.
Topic archived. No new replies allowed.