converts name to pig latin

Pages: 12
I need some help, not sure what to do? Advice? Im not asking anyone to do this, I just wanted help on how to start it, that is all!
Thanks


Write a program which converts a person’s name to Pig Latin. Both the first and last name should be read from the user and stored in separate string variables. You will also need another pair of strings to hold the name in Pig Latin. The conversion must take place in a separate function, which will be called twice (once for the first name and once for the last name). To convert a name to Pig Latin, use the following algorithm:

Convert the first letter to lower case.
If the first letter in the word is a vowel, add "way" to the end.
Otherwise, find the first letter (left to right) in the word that is a vowel.
Move every consonant before (to the left of) of the vowel to the end of the name.
Add "ay" to the end.
Then, convert the new first letter of the name back to an uppercase letter.

For example, “Isaac Newton” is “Isaacway Ewtonnay” in Pig Latin. The conversion function should take one argument, the original word, and return the word converted to Pig Latin. Each of these is a string, so you will need to spend some time reading up on how to work with and manipulate strings. Here is a good starting point:

http://cplusplus.com/reference/string/string/

In particular, look into the string concatenation operator and the substr() function. You will also need to write a function to determine if a character is a vowel or not. Finally, you should look at the cctype library for the toupper() and tolower() functions which converts characters to uppercase and lowercase, respectively.

Your program should prompt the user to repeat the process as often as they want. That is, after the program displays the name in Pig Latin it should ask the user if they want to enter another name and repeat until the user is finished.
The instructions seem quite clear. Your program reads words, calls function to convert them, and then shows the result.
This is all I got, im very confused...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
	char answer;
	cout << "Enter a name (first then last): " << endl;
	do
	{
		cout << "Would you like to convert another name (y/n)? ";
		cin >> answer;
	} while (answer == 'y');
	
	return 0;
}
That is a perfectly good start. Now you insert between lines 9 and 10 the actual program.

Your instructions have:
- Both the first and last name should be read from the user and stored in separate string variables.
- You will also need another pair of strings to hold the name in Pig Latin.
- The conversion must take place in a separate function, which will be called twice.
- The program displays the name in Pig Latin.

- The conversion function should take one argument, the original word, and return the word converted to Pig Latin.
Is this closer, or way off??? I need some help with this.
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

bool avowel(char);
string rotate(string);
string plstring(string);

int main()
{
	string name;
	char answer;
	cout << "Enter a name (first then last): ";
	cin >> name;
	cout << name << plstring(name) << endl;

	do
	{
		cout << "Would you like to convert another name (y/n)? ";
		cin >> answer;
	} while (answer == 'y');
	
	return 0;
}

bool avowel(char ch)
{
	switch (ch)
	{
	case 'A':
	case 'E':
	case 'I':
	case 'O':
	case 'U':
	case 'Y':
	case 'a':
	case 'e':
	case 'i':
	case 'o':
	case 'u':
	case 'y':
		return true;
	default:
		return false;
	}
}

string rotate(string name)
{
	string::size_type lcn = name.length;
	string strl;
	strl = name.substr(1,len - 1) + name[0];
	return strl;
}

string plstring(string name)
{
	string::size_type len;
	bool vowel;
	string::size_type counter;
	if (avowel(name[0]))
		name = name + "-way";
	else
	{
		name = name + "-";
		name = rotate(name);
		len - name.length();
		vowel = false;
		for (counter = 1; counter < len - 1; counter++)
		if (avowel(name[0]))
		{
			vowel = true;
			break;
		}
		else
			name = rotate(name);
		if (!vowel)
			name = name.substr(1, len) + "-way";
		else
			name = name + "ay";
	}
	return name;
}
That is both closer and off.

Lines 14-16 should be inside the do-while loop.

How many (string) variables you were told to use?

Your plstring() is too complex. Read again the conversion rules.
still confused....
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

bool avowel(char);
string rotate(string);
string plstring(string);

int main()
{
	string name;
	char answer;

	do
	{
		cout << "Enter a name (first then last): ";
		cin >> name;
		cout << name << plstring(name) << endl;
		cout << "Would you like to convert another name (y/n)? ";
		cin >> answer;
	} while (answer == 'y');
	
	return 0;
}

bool avowel(char ch)
{
	switch (ch)
	{
	case 'A':
	case 'E':
	case 'I':
	case 'O':
	case 'U':
	case 'Y':
	case 'a':
	case 'e':
	case 'i':
	case 'o':
	case 'u':
	case 'y':
		return true;
	default:
		return false;
	}
}

string rotate(string name)
{
	string::size_type lcn = name.length;
	string strl;
	strl = name.substr(1,len - 1) + name[0];
	return strl;
}

string plstring(string name)
{
	string::size_type len;
	bool vowel;
	string::size_type counter;
	if (avowel(name[0]))
		name = name + "-way";
	else
	{
		name = name + "-";
		name = rotate(name);
		len - name.length();
		vowel = false;
		for (counter = 1; counter < len - 1; counter++)
		if (avowel(name[0]))
		{
			vowel = true;
			break;
		}
		else
			name = rotate(name);
		if (!vowel)
			name = name.substr(1, len) + "-way";
		else
			name = name + "ay";
	}
	return name;
}
I think I have an infinite loop issue somewhere???
After entering a first and last name a 3rd time, it hangs and wont give the answer or anything...

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
#include <iostream>
#include <cctype>
#include <string>
using namespace std;

void FirstVowel(string& middle)
{
	char Cons = middle[0];
	do
	{
		middle.push_back(Cons);
		middle.erase(0, 1);
		Cons = middle[0];
	} while (Cons != 'a' && Cons != 'e' && Cons != 'i' && Cons != 'o' && Cons != 'u');
}

string Vowel_Cont(string Median)
{
	string str = "way", str2 = "ay";
	if (Median[0] == 'a' || Median[0] == 'e' || Median[0] == 'i' || Median[0] == 'o' || Median[0] == 'u')
	{
		Median.append(str);
	}
	else
	{
		FirstVowel(Median);
		Median.append(str2);
	}

	return Median;
}

string conversion(string bef)
{
	string aft;
	int x = 0;
	int G = bef.length();
	while (x < G)
	{
		bef[x] = tolower(bef[x]);  //this part converts to lowercase
		x++;
	}
	bef = Vowel_Cont(bef);
	bef[0] = toupper(bef[0]);   //this part converts to uppercase
	aft = bef;
	return aft;
}

int main()
{
	string First, Last, conF, conL;
	char do_again;

	do
	{
		cout << "Enter a name (first then last): ";
		cin >> First >> Last;
		conF = conversion(First);
		conL = conversion(Last);
		cout << conF << " " << conL << endl;

		cout << "Would you like to convert another name (y/n)? ";
		cin >> do_again;
	} while (do_again == 'Y' || do_again == 'y');
	
	return 0;
}
What names are you using to test this?
anyway names with just a first seperated by last name
I was able to run 5 names through without crashing - just wondering if you're hitting an exception with some particular name.
when I enter gg hh, it hangs
Also it there anyway I can get make the cout << "Enter a name (first then last): "; just show the very first time and not repeat?? I was told to do this.
Thanks
If you only want the phrase to print once, pull it out of the loop.

If you have a name with no vowels, looks like you get an infinite loop at line 14.
how should i fix the infinite loop at this line?
You'd want to stop looping when you've checked all the original letters in the name. Right now, you're looping until you find a letter that's not a vowel.

You need to decide how you're going to handle a name with no vowels. I'm not sure what the pig latin rule would be? Treat it like a word starting with a vowel and just add 'way' to the end? (Also - what if the only vowel is a y?)

Yea, I dont know what to do....
Okay, so my only issue is my program hangs (gos into infinite loop) when the name gg hh is entered. This is becuase there are are NO consonants in the name. Could you tell me what I need after my do while loop in void FirstVowel(string& middle)???

Thanks
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
#include <iostream>
#include <cctype>
#include <string>
using namespace std;

void FirstVowel(string& middle)
{
	char Cons;
	do
	{
		Cons = middle[0];
		middle.push_back(Cons);
		middle.erase(0, 1);
		Cons = middle[0];
	} while (Cons != 'a' && Cons != 'e' && Cons != 'i' && Cons != 'o' && Cons != 'u');
}

string Vowel_Cont(string Med)
{
	string str = "way", str2 = "ay";
	if (Med[0] == 'a' || Med[0] == 'e' || Med[0] == 'i' || Med[0] == 'o' || Med[0] == 'u')
	{
		Med.append(str);
	}
	else
	{
		FirstVowel(Med);
		Med.append(str2);
	}

	return Med;
}

string conversion(string bef)
{
	string aft;
	int x = 0;
	int G = bef.length();
	while (x < G)
	{
		bef[x] = tolower(bef[x]);  //this part converts to lowercase
		x++;
	}
	bef = Vowel_Cont(bef);
	bef[0] = toupper(bef[0]);   //this part converts to uppercase
	aft = bef;
	return aft;
}

int main()
{
	string First, Last, conF, conL;
	char do_again;

	cout << "Enter a name (first then last): ";
	do
	{
		cin >> First >> Last;
		conF = conversion(First);
		conL = conversion(Last);
		cout << conF << " " << conL << endl;

		cout << "Would you like to convert another name (y/n)? ";
		cin >> do_again;
	} while (do_again == 'Y' || do_again == 'y');
	
	return 0;
}
Your problem is that you are changing the "middle".

Your instructions hint:
look into the string concatenation operator and the substr() function.


Think about a different approach. First find the position of first vowel.

You have three position categories:
1. There is no vowel. Perhaps you simply append "ay"?

2. The position is at start. You append "way".

3. There is vowel, but not at start. Now you use the substr() with the position to get two strings:
a) the consonants from the start. "head"
b) from vowel to the end. "tail"
The result = tail+head+"ay"
I changed the beginning up a bit. Could someone 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
#include <iostream>
#include <cctype>
#include <string>
using namespace std;

void FirstVowel(string& middle)
{
	char vowel = middle[0];
	for (int i = 0; i < middle.size(); i++) {
		if (vowel == 'a' && vowel == 'e' && vowel == 'i' && vowel == 'o' && vowel == 'u')
			vowel = middle[i];
	}
	string sub;
	string rplc = to_string(static_cast<char>(toupper(middle[0])));
	sub = middle.substr(0, vowel);
	middle.replace(0, sub.length(), rplc);
	middle = middle + sub;
}

string Vowel_Cont(string Med)
{
	string str = "way", str2 = "ay";
	if (Med[0] == 'a' || Med[0] == 'e' || Med[0] == 'i' || Med[0] == 'o' || Med[0] == 'u')
	{
		Med.append(str);
	}
	else
	{
		FirstVowel(Med);
		Med.append(str2);
	}

	return Med;
}

string conversion(string bef)
{
	string aft;
	int x = 0;
	int G = bef.length();
	while (x < G)
	{
		bef[x] = tolower(bef[x]);  //this part converts to lowercase
		x++;
	}
	bef = Vowel_Cont(bef);
	bef[0] = toupper(bef[0]);   //this part converts to uppercase
	aft = bef;
	return aft;
}

int main()
{
	string First, Last, conF, conL;
	char do_again;

	cout << "Enter a name (first then last): ";
	do
	{
		cin >> First >> Last;
		conF = conversion(First);
		conL = conversion(Last);
		cout << conF << " " << conL << endl;

		cout << "Would you like to convert another name (y/n)? ";
		cin >> do_again;
	} while (do_again == 'Y' || do_again == 'y');

	return 0;
}
Pages: 12