My program is acting strange all of the sudden

Ok so i was making some minor improvements to my text encryptor program and for some reason when i start the program it doesnt ask for the password it just goes straight to the program, and its supposed to create a file on the desktop with a 1 for true in it and it puts 118 in there instead, i dont understand why that is. Run the code yourself and see what i mean. Open the .psk file with notepad.

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

void encrypt()
{
    ifstream fIn;
    ifstream passIn;
    string file_name2;
    string store_text2;
    wchar_t lower[27] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\0'};
    wchar_t upper[27] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
    //wchar_t symbol[31] = {'!','@','#','$','%','^','&','*','(',')','_','-','=','+',',',';',':','"','<','>','.','/','?','|','~','`','[',']','{','}','\0'};
    //wchar_t symbolReplace[31] = {'Ϡ','ϡ','Ϣ','ϣ','Ϥ','ϥ','Ѡ','Ѣ','ѣ','Ѥ','ѥ','Ѧ','Ѩ','Ѫ','Ѭ','Ѯ','Ѳ','Ѷ','Ѹ','Ѻ','Ѽ','ѽ','Ҁ','Ҟ','ᾗ','Ҿ','ϰ','ͽ','ˤ','ʇ','\0'};
    wchar_t replace[28] = {'♎','×','♋','¼','Ѝ','☩','þ','ß','¤','Œ','Ø','★','유','½','å','¹','°','Ä','#','¡','♯','$','','♚','"','™','✝','\0'};
    wchar_t upperReplace[28] = {'³','²','µ','÷','û','ê','ì','Ñ','Д','Ó','Ö','É','п','⅞','Á','¦','Ü','Û','Ù','Õ','я','Ю','Ã','Ш','₧','Æ','✝','\0'};
    wchar_t space = ' ';
    size_t pos = string::npos;

    cin.ignore(100, '\n');

    cout << "What is the exact name of the file for encryption" << endl;
    getline(cin, file_name2);

    fIn.open(file_name2.c_str());

    stringstream ss(stringstream::in | stringstream::app | stringstream::out);

    while(getline(fIn, store_text2))
    {
        ss << store_text2 << endl;
    }

    store_text2 = ss.str();


    //Find lower case alphabet
    for(int i = 0; i < 27; i++)
    {
        while((pos = store_text2.find(lower[i])) != string::npos)
        {
            store_text2[pos] = replace[i];
        }

        while((pos = store_text2.find(upper[i])) != string::npos)
        {
            store_text2[pos] = upperReplace[i];
        }
/*
        while((pos = store_text2.find(symbol[i])) != string::npos)
        {
            store_text2[pos] = symbolReplace[i];
        }
*/
        while((pos = store_text2.find(space)) != string::npos)
        {
            store_text2[pos] = replace[27];
        }
    }

    ofstream fOut;

    fOut.open(file_name2.c_str());

    fOut << store_text2;

    cout << "File Encrypted\n" << endl;

    cout << store_text2;

    fOut.close();
}


void decrypt()
{
    ifstream fIn;
    string file_name;
    char space = ' ';
    string store_text;
    wchar_t lower[27] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\0'};
    wchar_t upper[27] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
    //wchar_t symbol[31] = {'!','@','#','$','%','^','&','*','(',')','_','-','=','+',',',';',':','"','<','>','.','/','?','|','~','`','[',']','{','}','\0'};
    //wchar_t symbolReplace[31] = {'Ϡ','ϡ','Ϣ','ϣ','Ϥ','ϥ','Ѡ','Ѣ','ѣ','Ѥ','ѥ','Ѧ','Ѩ','Ѫ','Ѭ','Ѯ','Ѳ','Ѷ','Ѹ','Ѻ','Ѽ','ѽ','Ҁ','Ҟ','ᾗ','Ҿ','ϰ','ͽ','ˤ','ʇ','\0'};
    wchar_t replace[28] = {'♎','×','♋','¼','Ѝ','☩','þ','ß','¤','Œ','Ø','★','유','½','å','¹','°','Ä','#','¡','♯','$','','♚','"','™','✝','\0'};
    wchar_t upperReplace[28] = {'³','²','µ','÷','û','ê','ì','Ñ','Д','Ó','Ö','É','п','⅞','Á','¦','Ü','Û','Ù','Õ','я','Ю','Ã','Ш','₧','Æ','✝','\0'};
    size_t pos = string::npos;

    cin.ignore(100, '\n');

    cout << "What is the exact name of the file for decryption" << endl;
    getline(cin, file_name);

    fIn.open(file_name.c_str());

    stringstream ss(stringstream::in | stringstream::app | stringstream::out);

    while(getline(fIn, store_text))
    {
        ss << store_text << endl;
    }

    store_text = ss.str();


    for(int i = 0; i < 27; i++)
    {
        while((pos = store_text.find(replace[i])) != string::npos)
        {
            store_text[pos] = lower[i];
        }

        while((pos = store_text.find(upperReplace[i])) != string::npos)
        {
            store_text[pos] = upper[i];
        }
/*
        while((pos = store_text.find(symbolReplace[i])) != string::npos)
        {
            store_text[pos] = symbol[i];
        }
*/
        while((pos = store_text.find(replace[27])) != string::npos)
        {
            store_text[pos] = space;
        }
    }

    ofstream fOut2;

    fOut2.open(file_name.c_str());

    fOut2 << store_text;

    cout << "File Decrypted\n" << endl;

    cout << store_text;

    fOut2.close();
}


int password()
{
    string newPassword;
    string existingPassword;
    string store;
    bool passwordCreated;
    ifstream passIn;
    ofstream passOut;

    passIn.open("PassKey.psk");

    passIn >> passwordCreated;

    while(getline(passIn, store))
    {
        newPassword = store;
    }

    if(passwordCreated == 1)
    {
        passIn >> passwordCreated;

        cout << "Please enter your password" << endl;

        while(newPassword != existingPassword)
        {
            getline(cin, existingPassword);

            if(existingPassword.length() == 0)
            {
                cout << "Password cannot be 0 characters" << endl;
            }
            if((newPassword != existingPassword) && (existingPassword.length() > 0))
            {
                cout << "Incorrect password" << endl;
            }
            else if(newPassword == existingPassword)
            {
                cout << "Correct password, Proceed" << endl;
                return 1;
            }
        }
    }

    passOut.open("PassKey.psk");
    passOut << passwordCreated << endl;
    passOut << newPassword << endl;

    //If password is not set up yet, create one

    if(passwordCreated == 0)
    {
        passOut.close();
        passOut.open("PassKey.psk");
        cout << "Please create a password" << endl;

        do
        {
            getline(cin, newPassword);

            if(newPassword.length() <= 4)
            {
                cout << "Your password must be more than 4 characters long!!\n" << endl;
            }
        }
        while(newPassword.length() <= 4);

        if(newPassword.length() > 4)
        {
            passOut << true << endl;
            passOut << newPassword << endl;
            cout << "Your password is: " << newPassword << endl;
        }
        passOut.close();
    }
}


int main()
{
    if(password() == 0) return 0;

    int choice;

    while(choice != 3)
    {
        cout << "\n";
        cout << "Encryptor Version 1.2.7 Alpha\n" << endl;

        cout << "What do you want to do?" << endl;
        cout << "1) Encrypt file" << endl;
        cout << "2) Decrypt file" << endl;
        cout << "3) Exit Program" << endl;
        cin >> choice;

        if(choice == 1)
        {
            encrypt();
        }
        else if(choice == 2)
        {
            decrypt();
        }
        if(choice == 3)
        {
            return 0;
        }
    }

    return 0;
}
new==old mean that objects has the same pointer. Try by string.equals().
ok what line please
Line 182. This if() may be never TRUE. Others if() bay be always TRUE
Last edited on
new==old mean that objects has the same pointer. Try by string.equals().

No, it does not. std::string has no equals method.

Line 182. This if() may be never TRUE. Others if() bay be always TRUE

Assuming you meant line 183, this is, again, bad information.
So does anyone know whats wrong? I was blindsided by this, I have been looking over my code and still cant figure it out.
You really should pay attention to the multitude of warnings the compiler issues when it compiles your code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    // ...
    bool passwordCreated;  // uninitialized variable.
    // ...

    passIn.open("PassKey.psk");

    passIn >> passwordCreated;
    // did the previous succeed or is passwordCreated still some random value?
    // hard to say, since you don't check.

    // ...
    passOut.open("PassKey.psk");
    passOut << passwordCreated << endl;
    passOut << newPassword << endl;

    // If the previous extraction wasn't successful, you just wrote some random sampling
    // of bits to passOut. 

Ok that fixed the boolean value part but what about the others? i am a little slow so I dont quite get it.
ok i got it working back to normal agian, but how can i fix all the warnings? what do i need to do? I tried fixing them before and i succeeded with like 80 of the warnings but i cant seem to get rid of the rest.
Last edited on
bump
bump
Give some example of the warnings please.
Oh, i thought I already posted some, sorry about that.

I get 101 warnings and this one

warning: multi-character character constant

is 100 of them and this one is the other one

warning: control reaches end of non-void function


updated code

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

/*
=====|
Notes|
=====|

I am having a problem where if you enter a letter in the
decrypt function it will end the loop and go to the main
function
*/

using namespace std;

void encrypt()
{
    ifstream fIn;
    ifstream passIn;
    string file_name2;
    string store_text2;
    unsigned wchar_t lower[27] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\0'};
    unsigned wchar_t upper[27] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
    //wchar_t symbol[31] = {'!','@','#','$','%','^','&','*','(',')','_','-','=','+',',',';',':','"','<','>','.','/','?','|','~','`','[',']','{','}','\0'};
    //wchar_t symbolReplace[31] = {'Ϡ','ϡ','Ϣ','ϣ','Ϥ','ϥ','Ѡ','Ѣ','ѣ','Ѥ','ѥ','Ѧ','Ѩ','Ѫ','Ѭ','Ѯ','Ѳ','Ѷ','Ѹ','Ѻ','Ѽ','ѽ','Ҁ','Ҟ','ᾗ','Ҿ','ϰ','ͽ','ˤ','ʇ','\0'};
    unsigned wchar_t replace[28] = {'♎','×','♋','¼','Ѝ','☩','þ','ß','¤','Œ','Ø','★','유','½','å','¹','°','Ä','#','¡','♯','$','','♚','"','™','✝','\0'};
    unsigned wchar_t upperReplace[28] = {'³','²','µ','÷','û','ê','ì','Ñ','Д','Ó','Ö','É','п','⅞','Á','¦','Ü','Û','Ù','Õ','я','Ю','Ã','Ш','₧','Æ','✝','\0'};
    unsigned wchar_t space = ' ';
    size_t pos = string::npos;

    stringstream ss(stringstream::in | stringstream::app | stringstream::out);

    cin.ignore(100, '\n');

    cout << "What is the exact name of the file for encryption" << endl;

    while(file_name2.empty())
    {
        getline(cin, file_name2);

        if(!file_name2.empty())
        {
            fIn.open(file_name2.c_str());
        }

        while(getline(fIn, store_text2))
        {
            ss << store_text2 << endl;
        }

        store_text2 = ss.str();

        if(ss.gcount() == 0)
        {
            cout << "File is empty" << endl;
        }
    }


    //Find lower case alphabet
    for(int i = 0; i < 27; i++)
    {
        while((pos = store_text2.find(lower[i])) != string::npos)
        {
            store_text2[pos] = replace[i];
        }

        while((pos = store_text2.find(upper[i])) != string::npos)
        {
            store_text2[pos] = upperReplace[i];
        }
/*
        while((pos = store_text2.find(symbol[i])) != string::npos)
        {
            store_text2[pos] = symbolReplace[i];
        }
*/
        while((pos = store_text2.find(space)) != string::npos)
        {
            store_text2[pos] = replace[27];
        }
    }

    ofstream fOut;

    fOut.open(file_name2.c_str());

    fOut << store_text2;

    if(ss.gcount() >= 1)
    {
        cout << "File Encrypted" << endl;
    }

    cout << store_text2;

    fOut.close();
}


void decrypt()
{
    ifstream fIn;
    string file_name;
    char space = ' ';
    string store_text;
    unsigned wchar_t lower[27] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\0'};
    unsigned wchar_t upper[27] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
    //wchar_t symbol[31] = {'!','@','#','$','%','^','&','*','(',')','_','-','=','+',',',';',':','"','<','>','.','/','?','|','~','`','[',']','{','}','\0'};
    //wchar_t symbolReplace[31] = {'Ϡ','ϡ','Ϣ','ϣ','Ϥ','ϥ','Ѡ','Ѣ','ѣ','Ѥ','ѥ','Ѧ','Ѩ','Ѫ','Ѭ','Ѯ','Ѳ','Ѷ','Ѹ','Ѻ','Ѽ','ѽ','Ҁ','Ҟ','ᾗ','Ҿ','ϰ','ͽ','ˤ','ʇ','\0'};
    unsigned wchar_t replace[28] = {'♎','×','♋','¼','Ѝ','☩','þ','ß','¤','Œ','Ø','★','유','½','å','¹','°','Ä','#','¡','♯','$','','♚','"','™','✝','\0'};
    unsigned wchar_t upperReplace[28] = {'³','²','µ','÷','û','ê','ì','Ñ','Д','Ó','Ö','É','п','⅞','Á','¦','Ü','Û','Ù','Õ','я','Ю','Ã','Ш','₧','Æ','✝','\0'};
    size_t pos = string::npos;

    stringstream ss(stringstream::in | stringstream::app | stringstream::out);

    cin.ignore(100, '\n');

    cout << "What is the exact name of the file for decryption" << endl;

    while(file_name.empty())
    {
        getline(cin, file_name);

        if(!file_name.empty())
        {
            fIn.open(file_name.c_str());
        }

        while(getline(fIn, store_text))
        {
            ss << store_text << endl;
        }

        store_text = ss.str();

        if(ss.gcount() == 0)
        {
            cout << "File is empty" << endl;
        }
    }


    for(int i = 0; i < 27; i++)
    {
        while((pos = store_text.find(replace[i])) != string::npos)
        {
            store_text[pos] = lower[i];
        }

        while((pos = store_text.find(upperReplace[i])) != string::npos)
        {
            store_text[pos] = upper[i];
        }
/*
        while((pos = store_text.find(symbolReplace[i])) != string::npos)
        {
            store_text[pos] = symbol[i];
        }
*/
        while((pos = store_text.find(replace[27])) != string::npos)
        {
            store_text[pos] = space;
        }
    }

    ofstream fOut2;

    fOut2.open(file_name.c_str());

    fOut2 << store_text;

    if(ss.gcount() >= 1)
    {
        cout << "File Decrypted" << endl;
    }

    cout << store_text;

    fOut2.close();
}


int password()
{
    string newPassword;
    string existingPassword;
    string store;
    bool passwordCreated = false;
    ifstream passIn;
    ofstream passOut;

    passIn.open("PassKey.psk");

    passIn >> passwordCreated;

    while(getline(passIn, store))
    {
        newPassword = store;
    }

    if(passwordCreated == 1)
    {
        passIn >> passwordCreated;

        cout << "Please enter your password" << endl;

        while(newPassword != existingPassword)
        {
            getline(cin, existingPassword);

            if(existingPassword.length() == 0)
            {
                cout << "Password cannot be 0 characters" << endl;
            }
            if((newPassword != existingPassword) && (existingPassword.length() > 0))
            {
                cout << "Incorrect password" << endl;
            }
            else if(newPassword == existingPassword)
            {
                cout << "Correct password, Proceed" << endl;
                return 1;
            }
        }
    }

    passOut.open("PassKey.psk");
    passOut << passwordCreated << endl;
    passOut << newPassword << endl;

    //If password is not set up yet, create one

    if(passwordCreated == 0)
    {
        passOut.close();
        passOut.open("PassKey.psk");
        cout << "Please create a password" << endl;

        do
        {
            getline(cin, newPassword);

            if(newPassword.length() <= 4)
            {
                cout << "Your password must be more than 4 characters long!!\n" << endl;
            }
        }
        while(newPassword.length() <= 4);

        if(newPassword.length() > 4)
        {
            passOut << true << endl;
            passOut << newPassword << endl;
            cout << "Your password is: " << newPassword << endl;
        }
        passOut.close();
    }
}


int main()
{
    if(password() == 0) return 0;

    int choice;

    while(choice != 3)
    {
        cout << "\n";
        cout << "Encryptor Version 1.2.8 Alpha\n" << endl;

        cout << "What do you want to do?" << endl;
        cout << "1) Encrypt file" << endl;
        cout << "2) Decrypt file" << endl;
        cout << "3) Exit Program" << endl;
        cin >> choice;

        if(choice == 1)
        {
            encrypt();
        }
        else if(choice == 2)
        {
            decrypt();
        }
        else if(choice == 3)
        {
            return 0;
        }
    }

    return 0;
}
Looks like you will have to put an L in front of all those wide chars:
unsigned wchar_t replace[28] = { L'♎',L'×',L'♋',L'¼',L'Ѝ',L'☩'

That might clear 100 warmnings or so.
(but I'm no expert in using wide chard).

then there is one outstandaing problem - the int password() function needs to return a value - and at the moment it is posible to exit the function without an explicit return statement.
ok i'll try that.
ok great that got rid of almost everything now i have these 3

C:\Users\Chay\Desktop\Encryptor\main.cpp|28|warning: character constant too long for its type|
C:\Users\Chay\Desktop\Encryptor\main.cpp|113|warning: character constant too long for its type|
C:\Users\Chay\Desktop\Encryptor\main.cpp||In function 'int password()':|
C:\Users\Chay\Desktop\Encryptor\main.cpp|261|warning: control reaches end of non-void function|
||=== Build finished: 0 errors, 3 warnings ===|
That's because of L'Ѝ' which is two characters: U+00D0 and the unprintable U+008D (I don't know how you typed that), it should be just L'Ð' (single character U+00D0).
When in doubt, use Unicode escapes: L'\u00d0'
Last edited on
Topic archived. No new replies allowed.