Variable not Printing to Cout

Hi, LeafyCircuits here.
Somewhere on these forums was a person trying to make a Caesar Encryption program, but was having trouble as to achieving that. Well, I decided to try making one myself, and now I've run into problems :)

For those who haven't studied cryptography, a Caesar Cipher is one of the most basic ciphers. Basically, to create the "key", you shift letters of the alphabet down 3 or 4 letters:
Thus, this shifted 3 letters would turn from this,
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
to this,
DEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABC

Therefor, the letter 'A' becomes 'D', and 'y' becomes 'B'


Anyways, here's the problem. I'm trying to print a string variable to the console using cout, but even though the var is filled with valid, null terminated string, it doesn't print anything. I know it's filled because I've used my debugger and looked at the var in question.
So first, comp specs:
OS: Windows 7 Home Premium 64-bit
IDE: Dev-C++ with C++11; ISO and STL
Compiler: GNU gcc

Here's the code in question:
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <vector>
#include <map>
#include <conio.h>

using namespace std;

void nogetch() {getch();}

ostream& operator << (ostream &out, const vector<char> &vec)
{
         vector<char>::iterator itr=vec.begin();
         for (;itr!=vec.end();itr++)
         {
             cout << *itr;
         }
         return out;
}

map<char, char> createCypherKey(unsigned int adv)
{
             vector<char> key1, key2, *keytemp=new vector<char>;
             char rep;
             for (char i=65;i<91;i++)
             {
                 key1.push_back(i);
             }
             for (char i=97;i<123;i++)
             {
                 key1.push_back(i);
             }
             
             *keytemp=key1;
             
             for (int i=0;i<adv;i++)
             {
                 rep=(*keytemp).at(0);
                 (*keytemp).erase((*keytemp).begin());
                 (*keytemp).push_back(rep);
             }
             
             key2=*keytemp;
             delete keytemp;
             keytemp=0;
             
             map<char, char> key;
             if (key1.size()==key2.size())
             {
                                          for (int i=0;i<key1.size();i++)    //since key1.size()==key2.size(), key2 can substitue key1
                                          {
                                              key[key1.at(i)]=key2.at(i);
                                          }
             }
             
             else
             {
                 system("CLS");
                 cout << "Cypher key creation failed!";
                 nogetch();
                 system("CLS");
                 return key;
             }
             
             return key;
}
             

string encrypt(string chvec, map<char, char> key)
{
       string result;
       vector<char> strbuf;
       char input;
       for (int i=0;i<chvec.length();i++)
       {
           strbuf.push_back(chvec[i]);
       }
       
       for (int i=0;i<strbuf.size();i++)
       {
           input=strbuf.at(i);
           strbuf.at(i)=key[input];
       }
       
       for (int i=0;i<strbuf.size();i++)
       {
           result[i]=strbuf.at(i);
       }
       
       return result;
}

main()
{
      string hold, test;
      map <char, char> keyhold=createCypherKey(3);
      cout << "Enter phrase to be encrypted:\n\n";
      cin >> hold;
      test=encrypt(hold, keyhold);
      cout << test;
      getchar();
      getchar();
}


The functions that do the actual encryption seem to work, but even though I don't think the problem is in there, obviously I'm missing something, so please don't ignore the functions entirely (but I'm pretty sure I've proofread enough times).

As you can see, I call that encrypt function (yes, Cipher is spelt wrong) and place it in the 'test' var, then try to print to the console, but nothing is printed. I looked over my overloaded << operator function, and I deduced that that wasn't the source, for when I removed the code, the same bug happened. Using the debugger, I found out that the var is being filled with what I want in there, so I'm really stumped. But then again, most of the time, I probably forgot some small detail that a common mistake. But hey, we make those from time to time.

Also, if you're wondering why I go through the trouble of placing a string inside of a vector to work with, then put that into a string again in my encrypt function, it's simply so I can return simple types for easy manageability.




I'm quite new to the forums, so constructive criticism is greatly appreciated. Thanks for reading.
This code doesn't compile as is, so I'm not sure how you're running it.

When you reach line 86 result is an empty string. You cannot access element i of result because result doesn't have an element i.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cctype>
#include <string>

string encrypt(string chvec, map<char, char> key)
{
    string encrypted = chvec ;

    for ( unsigned i=0; i<encrypted.length(); ++i )
        if ( std::isalpha( encrypted[i] ) )
            encrypted[i] = key[encrypted[i]] ;
  
    return encrypted ;
}
Topic archived. No new replies allowed.