Pig-Latin translation program

Hello, I'm trying to make this piglatin program for my computer science class. I have the general code done but I am having trouble creating the loop for the words to be redefined correctly. The words are being taken from a file and are the following:
"This is a, bunch of words"
The output should be:
"Isthay isway away. unchbay ofway ordsway"
But I am getting:
"Isthay Isay A,ay Unchbay Ofay Ordsway"
I am pretty sure the problem is coming from the while loop but I have
no idea on how to correct it. Someone please help.


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
 #include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
string lowerCase(string x);
string normal(string word);
int main(){
	ofstream fout;
	ifstream fin;
	string x = "";
	string y = "";
	string word = "";
	int numwords = 0;
	char first;
	fin.open("text.txt");
	if (fin.fail())
		cout << "it failed" << endl;
	//fout.open("answers.txt");
	while (fin >> word)
	{
		numwords++;
		first = word.at(0);
			if (first != 'a' || 'e' || 'i' || 'o' || 'u' || 'A' || 'E' || 'I' || 'O' || 'U')
			{
	
				word = normal(word);
				y += word  + " ";
			}
			else  
			{	
				
				y += word + "way ";
			}
			first = 'x';
	}
		cout << y;
		cout << endl;
		cout << "Total Words: " << numwords << endl;
		cout << "total letters " << y.length() << endl;

		
}//main finish

string lowerCase(string x)
{
	string CCC = "";
	for (int i = 0; i < x.length(); i++)
		CCC += tolower(x.at(i));

	return CCC;
}

string normal(string word)
{

	int b = 0;
	int flag = 0;
	string a = "";
	if (isupper(word.at(0)))
	{
		flag = 1;
		word = lowerCase(word);
	}
	b = word.find('a');
	if (word.find('e') < b)
		b = word.find('e');
	if (word.find('i') < b)
		b = word.find('i');
	if (word.find('o') < b)
		b = word.find('o');
	if (word.find('u') < b)
		b = word.find('u');
	for (int i = 0; i < (b); i++)
		a += word.at(i);

	word.erase(0, b);
	word = word + a + "ay";
	if (flag = 1)
	{
		flag = 0;
		word.at(0) = toupper(word.at(0));
	}
	//cout << word << " added to phrase" << endl;
	return word;
}
Last edited on
This may not be the only problem, but you can't do this
 
if (first != 'a' || 'e' || 'i' || 'o' || 'u' || 'A' || 'E' || 'I' || 'O' || 'U')

You need to compare first to each letter individually. You can cut the work in half by converting to a particular case (in a temporary variable if you want to maintain the original case for output):
1
2
char c = tolower(first);
if (c != 'a' && c != 'e' && c != 'i' && c != 'o' && c != 'u' )

Or you can compare it using a function from the C library:
 
if (!strchr("aeiou", tolower(first))) // if first is not a vowel 

Or you can use a C++ string function:
1
2
string vowels{"aeiou"};
if (vowels.find(tolower(first)) == vowels.npos)

Thanks tpb I figured it out a different way but here is my solution incase anyone else falls into the same issue.
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
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
string normal(string word);
string vowel(string word);
string lowerCase(string x);
string punch(string word);
int main()
{
	ofstream fout;
	ifstream fin;
	string x = "", y = "", word = "";
	int numwords = 0, numlett = 0, numchar = 0;
	char first;
	fin.open("Text.txt");
	if (fin.fail())
		cout << "it failed" << endl;
	while (fin >> word)
	{
		x += word + " ";
		numwords++;
		first = tolower(word.at(0));
		if (first == 'a' || first == 'e' || first == 'i' || first == 'o' || first == 'u')
		{
			word = vowel(word);
			y += word;
		}

		else
		{
			word = normal(word);
			y += word;
		}

		for (int i = 0; i < word.length(); i++)
		{
			if (isalpha(word.at(i)))
				numlett++;
		}
		for (int i = 0; i < word.length(); i++)
		{
			if (ispunct(word.at(i)))
				numchar++;
		}



	}
	//2. the number of letters in the text(after the conversion to pig latin)
	//3. the number of all characters in the text(including punctuation but NOT whitespace)
	
	cout << x << endl;
	cout << y << endl;
	cout << "The number of letters in the text is " << numlett << endl;
	cout << "The number of characters in the text is " << numchar + numlett << endl;



	fin.close();
}
string punch(string word)
{
	word.erase(word.size() - 1);

	return word;
}
string lowerCase(string x)
{
	string CCC = "";
	int xlength = x.length();
	for (int i = 0; i < xlength; i++)
		CCC += tolower(x.at(i));

	return CCC;
}
string vowel(string word)
{
	char last = word.at(word.length() - 1);
	int flag2 = 0;

	if (ispunct(last))
	{
		flag2++;
		word = punch(word);
	}
	word = word + "way";

	if (flag2 == 1)
	{
		word = word + last;
		flag2--;
	}
	word += " ";
	return word;
}
string normal(string word){
	int x = word.length(), flag2 = 0, flag1 = 0;
	char last;

	string tempword = "";

	if (isupper(word.at(0)))
	{
		flag1++;
		word = lowerCase(word);
	}


	last = word.at(word.length() - 1);

	if (ispunct(last))
	{
		flag2++;
		word = punch(word);
	}
	int b = 100;
	int a = word.find('a'), e = word.find('e'), i = word.find('i'), o = word.find('o'), u = word.find('u');
	if (a < b && a != 0 && a != -1)
	{
		b = a;
		//cout << "Found a " << endl;
	}
	if (e < b && e != 0 && e != -1)
	{
		b = e;
		//cout << "Found e" << endl;
	}
	if (i < b  && i != 0 && i != -1)
	{
		b = i;
		//cout << "Found i" << endl;
	}
	if (o < b && o != 0 && o != -1)
	{
		b = o;
		//cout << "Found o" << endl;
	}
	if (u < b  && u != 0 && u != -1)
	{
		b = u;
		//	cout << "Found u" << endl;
	}

	for (int x = 0; x < b; x++)
		tempword += word.at(x);
	word.erase(0, b);
	word = word + tempword;

	if (flag1 == 1)
	{
		flag1--;
		word.at(0) = toupper(word.at(0));
	}

	word = word + "ay";
	if (flag2 == 1)
	{
		word = word + last;
		flag2--;
	}
	word = word + " ";
	return word;
}
Topic archived. No new replies allowed.