confused about the output to this function I wrote

i am confused about the output

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
#include<iostream.h>
#include<lvp\string.h>
#include"StringUtility.h"
int main()
{
	String word("Sally sells sea shells, at the sea shore!");
	cout<<convertToPigLatin(word)<<endl;
	return(0);
}
//-------------------------------------------------------------------------
String convertToPigLatin(const String &word)
/*Converts the string parameter to pig latin
pre:
post:		*/
{
	String finalWord;
	String temp; 
	String leftOver;
	String way("way");
	String ay("ay");
	int length=word.length()-1;
	int pos=0; 
	char letter, letterb, letterc, letterd, letterx;
	for(pos=0;pos<=length;pos++)
	{
		leftOver=word.substr(pos,length-pos);
		int x=leftOver.find(' ');
		if (x!=-1){
			temp=leftOver.substr(pos,x);
			letter=temp.operator[](pos);
			int a(letter);
			letterb=temp.operator[](pos+1);
			int b(letterb);
			letterc=temp.operator[](pos+2);
			int c(letterc);
			letterd=temp.operator[](pos+3);
			int d(letterd);
			if ((a>=65) && (a<=90)){
				if ((a=65)||(a=69)||(a=73)||(a=79)||(a=85)){
					finalWord=finalWord+temp.substr(pos,x-(pos+1));
					finalWord=finalWord+way;
					finalWord=finalWord+" ";
				}
				else if ((b=97)||(b=101)||(b=105)||(b=111)||(b=117)){
					finalWord=finalWord+char(b-32);
					finalWord=finalWord+temp.substr(pos+2,x-(pos+3));
					finalWord=finalWord+char(a+32);
					finalWord=finalWord+ay;
					finalWord=finalWord+" ";
				}
				else if ((c>=97) && (c<=122)){
					finalWord=finalWord+char(c-32);
					finalWord=finalWord+temp.substr(pos+3,x-(pos+4));
					finalWord=finalWord+char(a+32);
					finalWord=finalWord+char(b);
					finalWord=finalWord+ay;
					finalWord=finalWord+" ";
				}
			}
			else if ((a>96) && (a<123)){
				if ((a=97)||(a=101)||(a=105)||(a=111)||(a=117)){
					finalWord=finalWord+temp.substr(pos,x-(pos+1));
					finalWord=finalWord+way;
					finalWord=finalWord+" ";
				}
				else if ((b=97)||(b=101)||(b=105)||(b=111)||(b=117)){
					finalWord=finalWord+char(b);
					finalWord=finalWord+temp.substr(pos+2,x-(pos+3));
					finalWord=finalWord+char(a);
					finalWord=finalWord+ay;
					finalWord=finalWord+" ";
				}
				else if ((c>=97) && (c<=122)){
					if ((d>=97) && (d<=122)){
						letterx=temp.operator[](x-1);
						if (letterx=','){
							finalWord=finalWord+char(c);
							finalWord=finalWord+temp.substr(pos+3,x-(pos+5));
							finalWord=finalWord+char(a);
							finalWord=finalWord+char(b);
							finalWord=finalWord+ay;
							finalWord=finalWord+", ";
						}
						else{
							finalWord=finalWord+char(c);
							finalWord=finalWord+temp.substr(pos+3,x-(pos+4));
							finalWord=finalWord+char(a);
							finalWord=finalWord+char(b);
							finalWord=finalWord+ay;
							finalWord=finalWord+" ";
						}
					}
					else{
						finalWord=finalWord+char(c);
						finalWord=finalWord+char(a);
						finalWord=finalWord+char(b);
						finalWord=finalWord+ay;
						finalWord=finalWord+" ";
					}
				}
			}
			pos=pos+x;
		}
		else{
			int y=leftOver.find('!');
			temp=leftOver.substr(pos,y);
			letter=temp.operator[](pos);
			int a(letter);
			letterb=temp.operator[](pos+1);
			int b(letterb);
			letterc=temp.operator[](pos+2);
			int c(letterc);
			if ((a>=65) && (a<=90)){
				if ((a=65)||(a=69)||(a=73)||(a=79)||(a=85)){
					finalWord=finalWord+temp.substr(pos,y-(pos+1));
					finalWord=finalWord+way;
					finalWord=finalWord+"!";
				}
				else if ((b=97)||(b=101)||(b=105)||(b=111)||(b=117)){
					finalWord=finalWord+char(b-32);
					finalWord=finalWord+temp.substr(pos+2,y-(pos+3));
					finalWord=finalWord+char(a+32);
					finalWord=finalWord+ay;
					finalWord=finalWord+"!";
				}
				else if ((c>=97) && (c<=122)){
					finalWord=finalWord+char(c-32);
					finalWord=finalWord+temp.substr(pos+3,y-(pos+4));
					finalWord=finalWord+char(a+32);
					finalWord=finalWord+char(b);
					finalWord=finalWord+ay;
					finalWord=finalWord+"!";
				}
			}
			else if ((a>96) && (a<123)){
				if ((a=97)||(a=101)||(a=105)||(a=111)||(a=117)){
					finalWord=finalWord+temp.substr(pos,y-(pos+1));
					finalWord=finalWord+way;
					finalWord=finalWord+"!";
				}
				else if ((b=97)||(b=101)||(b=105)||(b=111)||(b=117)){
					finalWord=finalWord+char(b);
					finalWord=finalWord+temp.substr(pos+2,y-(pos+3));
					finalWord=finalWord+char(a);
					finalWord=finalWord+ay;
					finalWord=finalWord+"!";
				}
				else if ((c>=97) && (c<=122)){
					finalWord=finalWord+char(c);
					finalWord=finalWord+temp.substr(pos+3,y-(pos+4));
					finalWord=finalWord+char(a);
					finalWord=finalWord+char(b);
					finalWord=finalWord+ay;
					finalWord=finalWord+"!";
				}
			}
			pos=pos+y;
		}
	}
	return finalWord;
}




index out of range: 6 string: sea s
Press any key to continue
Last edited on
Please edit your post and put all the code between code tags - http://www.cplusplus.com/articles/jEywvCM9/
Sorry I am new to this site.
Tell us specifics. What are you confused about. What output are you expecting, what output are you getting?
the expected output is:

Allysay ellssay easay ellsshay, atway ethay easay oreshay!
Press any key to continue


the output I am getting is:

index out of range: 6 string: sea s
Press any key to continue


Would you like me to add comments to the code to make it easier to follow?
Last edited on
Topic archived. No new replies allowed.