Error: main returns 3.

So I started making a program to convert normal words into words that can be typed into a calculator and read upside-down. (e.g. hello, hole, shell) I only got this far before I ran it and got an unusual error message.

Here's the 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
#include <iostream>
#include <string>

using namespace std;

void charFinder();

int main(){
    cout << "                    The" << endl;
    cout << "UPSIDEDOWN CALCULATOR WORD FINDING MACHINE" << endl;
    cout << "           or the UCWFM for short" << "\n\n";

    charFinder();

    return 0;
}
void charFinder(){
    string word;
    cout << "Enter the word you would like to UCFWMify: " << endl;
    cin >> word;

    int b;
    b=word.find("b");
    word.replace(b, 1, "8");

    int e;
    e=word.find("e");
    word.replace(e, 1, "3");

    int h;
    h=word.find("h");
    word.replace(h, 1, "4");

    int i;
    i=word.find("i");
    word.replace(i, 1, "1");

    int l;
    l=word.find("l");
    word.replace(l, 1, "7");

    int o;
    o=word.find("o");
    word.replace(o, 1, "0");

    int s;
    s=word.find("s");
    word.replace(s, 1, "5");

    cout << word;
}

When it runs, it gives me this:
-------------------------------------------------------------------------
The
UPSIDEDOWN CALCULATOR WORD FINDING MACHINE
or UCWFM for short

Enter the word that you would like to UCWFMify:
hello

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Process returned 3 (0x3) execution time : 6.356 s
Press ant key to continue.
-------------------------------------------------------------------------

Up until I added the code to replace o with 0 it worked fine.
After I type hello (or anything else) it crashes.

Please help
Bufflez
Last edited on
find will return string::npos (-1) if no instances are found. You are then trying to replace position -1 with a numeral. Since -1 is out of bounds, that is corrupting your memory, which is causing the crash.

You need to make sure the character was actually found before you try to replace it.

1
2
3
4
int s;
s=word.find("s");
if(s >= 0)  // only replace it if you found it
  word.replace(s, 1, "5");
http://www.cplusplus.com/reference/string/string/replace/

There's no 'b' in hello, so string::find will return string::npos. The pos for string::replace has to be within the string though (npos is never).
Last edited on
I think I get it. I was wondering how to check whether or not the find function had found anything, but I didn't know. Can you tell me why it returns -1?

And thanks hanst99, I'll put if statements before each segment of code to check whether that letter is actually in the word.
Since -1 is out of bounds, that is corrupting your memory, which is causing the crash.


According to the docs on this site, replace should throw an out_of_range exception.
I think I get it. I was wondering how to check whether or not the find function had found anything, but I didn't know. Can you tell me why it returns -1?


npos (-1) is the special value find uses to indicate that it didn't find anything. If you want to check to see if it was found, you can check the value to see if it is npos or not. If it's npos, you know it didn't find anything, and you can skip the replace call.
So I did everything that you guys suggested, but now I have a new error. (well, its actually the same error, but its for a different reason now.)
I also added a new function to flip the word around so that you can put it into a calculator, I think that's where the problem lies.

here's the revised 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
#include <iostream>
#include <string>

using namespace std;

void charFinder();
void flipper(string theWord);

int main(){
    cout << "                    The" << endl;
    cout << "UPSIDEDOWN CALCULATOR WORD FINDING MACHINE" << endl;
    cout << "           or the UCWFM for short" << "\n\n";

    charFinder();

    return 0;
}
void charFinder(){
    string word;
    cout << "Enter the word you would like to UCFWMify: " << endl;
    cin >> word;

    int b;
    do{b=word.find("b");
    if(b>=0)
    word.replace(b, 1, "8");
    }while(b>=0);

    int e;
    do{e=word.find("e");
    if(e>=0)
    word.replace(e, 1, "3");
    }while(e>-0);

    int h;
    do{h=word.find("h");
    if(h>=0)
    word.replace(h, 1, "4");
    }while(h>=0);

    int i;
    do{i=word.find("i");
    if(i>=0)
    word.replace(i, 1, "1");
    }while(i>=0);

    int l;
    do{l=word.find("l");
    if(l>=0)
    word.replace(l, 1, "7");
    }while(l>=0);

    int o;
    do{o=word.find("o");
    if(o>=0)
    word.replace(o, 1, "0");
    }while(o>=0);

    int s;
    do{s=word.find("s");
    if(s>=0)
    word.replace(s, 1, "5");
    }while(s>=0);

    flipper(word);
}

void flipper(string theWord){
    for(int x=theWord.length(); x>=0; x--){
        cout << theWord.at(x);
    }
}


I get the exact same error message that I got before when I ran it.
Last edited on
I haven't complile your code yet (I will do it later when I get home), but as far as I see I think the problem may be on line 33:

}while(e>-0);

I guess you mean e >= 0
Last edited on
oh haha I missed that completely. thnx for looking over my code so thouroughly!
**********
I just tried it, and it works fine, thnx!
I finally finished and debugged the program(or applet, I guess)
Here's the source code, in case you want to copy it and run it cuz its a pretty sick program. (its really condensed and hard to edit, I did that on purpose to save character space)
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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
void charFinder();
void flipper(string theWord);
void exclamation();
int main(){
cout << "                    The" << endl;
cout << "UPSIDEDOWN CALCULATOR WORD FINDING MACHINE" << endl;
cout << "           or the UCWFM for short\n\n";
cout << "Valid letters are: B, E, H, L, I, O and S.\n\n";
charFinder();}
void charFinder(){
string word;
cout << "Enter the word(s) you would like to UCWFMify: " << endl;
getline(cin, word);
if(word.at(word.length()-1)=='o')
word.insert(word.length()-1, ".");
bool a;
int finda=-1;
finda=word.find("a");
if(finda>=0){
a=true;
}else{
a=false;}
int b;
do{b=word.find("b");
if(b>=0)
word.replace(b, 1, "8");
}while(b>=0);
bool c;
int findc=-1;
findc=word.find("c");
if(findc>=0){
c=true;
}else{
c=false;}
bool d;
int findd=-1;
findd=word.find("d");
if(findd>=0){
d=true;
}else{
d=false;}
int e;
do{e=word.find("e");
if(e>=0)
word.replace(e, 1, "3");
}while(e>=0);
bool f;
int findf=-1;
findf=word.find("f");
if(findf>=0){
f=true;
}else{
f=false;}
bool g;
int findg=-1;
findg=word.find("g");
if(findg>=0){
g=true;
}else{
g=false;}
int h;
do{h=word.find("h");
if(h>=0)
word.replace(h, 1, "4");
}while(h>=0);
int i;
do{i=word.find("i");
if(i>=0)
word.replace(i, 1, "1");
}while(i>=0);
bool j;
int findj=-1;
findj=word.find("j");
if(findj>=0){
j=true;
}else{
j=false;}
bool k;
int findk=-1;
findk=word.find("k");
if(findk>=0){
k=true;
}else{
k=false;}
int l;
do{l=word.find("l");
if(l>=0)
word.replace(l, 1, "7");
}while(l>=0);
bool m;
int findm=-1;
findm=word.find("m");
if(findm>=0){
m=true;
}else{
m=false;}
bool n;
int findn=-1;
findn=word.find("n");
if(findn>=0){
n=true;
}else{
n=false;}
int o;
do{o=word.find("o");
if(o>=0)
word.replace(o, 1, "0");
}while(o>=0);
bool p;
int findp=-1;
findp=word.find("p");
if(findp>=0){
p=true;
}else{
p=false;}
bool q;
int findq=-1;
findq=word.find("q");
if(findq>=0){
q=true;
}else{
q=false;}
bool r;
int findr=-1;
findr=word.find("r");
if(findr>=0){
r=true;
}else{
r=false;}
int s;
do{s=word.find("s");
if(s>=0)
word.replace(s, 1, "5");
}while(s>=0);
bool t;
int findt=-1;
findt=word.find("t");
if(findt>=0){
t=true;
}else{
t=false;}
bool u;
int findu=-1;
findu=word.find("u");
if(findu>=0){
u=true;
}else{
u=false;}
bool v;
int findv=-1;
findv=word.find("v");
if(findv>=0){
v=true;
}else{
v=false;}
bool w;
int findw=-1;
findw=word.find("w");
if(findw>=0){
w=true;
}else{
w=false;}
bool x;
int findx=-1;
findx=word.find("x");
if(findx>=0){
x=true;
}else{
x=false;}
bool y;
int findy=-1;
findy=word.find("y");
if(findy>=0){
y=true;
}else{
y=false;}
bool z;
int findz=-1;
findz=word.find("z");
if(findz>=0){
z=true;
}else{
z=false;}
if(a==true)
cout << "A is not a valid letter" << endl;
if(c==true)
cout << "C is not a valid letter" << endl;
if(d==true)
cout << "D is not a valid letter" << endl;
if(f==true)
cout << "F is not a valid letter" << endl;
if(g==true)
cout << "G is not a valid letter" << endl;
if(j==true)
cout << "J is not a valid letter" << endl;
if(k==true)
cout << "K is not a valid letter" << endl;
if(m==true)
cout << "M is not a valid letter" << endl;
if(n==true)
cout << "N is not a valid letter" << endl;
if(p==true)
cout << "P is not a valid letter" << endl;
if(q==true)
cout << "Q is not a valid letter" << endl;
if(r==true)
cout << "R is not a valid letter" << endl;
if(t==true)
cout << "T is not a valid letter" << endl;
if(u==true)
cout << "U is not a valid letter" << endl;
if(v==true)
cout << "V is not a valid letter" << endl;
if(w==true)
cout << "W is not a valid letter" << endl;
if(x==true)
cout << "X is not a valid letter" << endl;
if(y==true)
cout << "Y is not a valid letter" << endl;
if(z==true)
cout << "Z is not a valid letter" << endl;
flipper(word);}
void flipper(string theWord){
cout << endl << "Upside-down in a calculator: ";
for(int x=theWord.length()-1;x>=0;x--){
cout << theWord.at(x);}
exclamation();
cout << "\n\n\n\n";}
void exclamation(){
srand(time(0));
int randNum=0;
randNum=(rand()%11);
switch(randNum){
case 0:cout << "\n\n                    WOAH!!";
break;
case 1:cout << "\n\n                   AMAZING!!";
break;
case 2:cout << "\n\n                  INCREDIBLE!!";
break;
case 3:cout << "\n\n                  UNBELIEVABLE!!";
break;
case 4:cout << "\n\n                 RIGHTEOUS MAN!!";
break;
case 5:cout << "\n\n                     SVEET!!";
break;
case 6:cout << "\n\n                   STUPENDOUS!!";
break;
case 7:cout << "\n\n                   TRMENDOUS!!";
break;
case 8:cout << "\n\n                    WICKED!!";
break;
case 9:cout << "\n\n                  WICKED AWESOME!!";
break;
case 10:cout << "\n\n                    AWESOME!!";
break;
case 11:cout << "\n\n                 INCONCIEVABLE!!";}}


Thnx for all the help!!
Topic archived. No new replies allowed.