Pig latin help

If i enter "ask" twice, it will print out 'ask askyay' instead of 'askyay askyay'.
Im not sure what im doing wrong.

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
#include <iostream>
#include <string>
#include <cctype>
#include <ctime>

using namespace std;




int main() {

	string vowels = "aeiou";
	string new_word;
	string pig_message;
	string message;
	getline(cin, message);

	


			for (unsigned int i = 0; i <= vowels.length(); i++)
		{
			if (message[0] == vowels[i])
			{
				new_word = message + "yay ";

				pig_message += new_word;
				cout << pig_message;

			}

		}
	
	


	
	
	system("pause");
	return 0;
}
You can use std::stringstream.
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
#include <iostream>
#include <string>
#include <cctype>
#include <ctime>
#include <sstream>

using namespace std;

int main() 
{
	string vowels = "aeiou";
	string new_word;
	string pig_message;
	string message;
	getline(cin, message);

	stringstream ss;
	ss << message;
	while(ss >> message)
	{
		message += "yay ";
		cout << message;
	}
	
	system("pause");
	return 0;
}


ask ask

askyay askyay


http://cpp.sh/9g4r
what is sstream
http://www.cplusplus.com/reference/sstream/stringstream/
I think i understand it, but why is it (ss >>message) in the while loop .
I think i understand it, but why is it (ss >>message) in the while loop .

Exactly the same as:
 
while( cin >> message )

It extracts data until a newline or whitespace character.
Last edited on
Since we didnt talk about stringstream in class im not allowed to use it. This is what i came up with but it isnt working right. If i enter "ask dog" it will not print out the pig latin version.

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
#include <iostream>
#include <string>
#include <cctype>
#include <sstream>

using namespace std;




int main() {

	string vowels = "aeiou";
	string new_word;
	string pig_message;
	string message;
	getline(cin, message);

	for (unsigned int i = 0; i <= vowels.length(); i++)
	{
		if (message[0] == vowels[i])
		{
			new_word = message + "yay ";
			cout << new_word;
		}
		else if (!message[0] == vowels[i]) {
			pig_message = message.substr(1) + message[0] + "ay";
			cout << pig_message;
		}

		}


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
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main() 
{
	int i;
	string message;
	getline(cin, message);

	for(i = 0; i < message.length() - 1; i++)
	{
		if(isalpha(message[i]) && isspace(message[i + 1]))
		{
			i++;
			message.insert(message.begin() + i, 'y');
			message.insert(message.begin() + i, 'a');

			i += 2;
		}
	}

	i = message.length() - 1;
	if(isalpha(message[i-1]))
	{
			i++;		
			message.insert(message.begin() + i, 'y');
			message.insert(message.begin() + i, 'a');

			i += 2;
	}

	cout << message;

	return 0;
}


ask dog
askay dogay


http://cpp.sh/5b2mm
Thanks for the reply, but it should be "askyay ogday". because one starts with a vowel and the other doesnt.
Thanks for the reply, but it should be "askyay ogday". because one starts with a vowel and the other doesnt.

What are the rules you are using to form this sentence?
Last edited on
User enters a sentence if a word starts with either aeiou then add "yay" at end, if non-vowel in first spot, then take first letter add it to end of word and then add "ay" to that.
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
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

bool isVowel(char c)
{
	c = tolower(c);
	if(c == 'a') return true;
	if(c == 'i') return true;
	if(c == 'u') return true;
	if(c == 'e') return true;
	if(c == 'o') return true;

	return false;
}

bool trackVowelWord(string message, int i, int &i_first)
{
	for(i; i >= 1; i--)
	{
		if(isalpha(message[i]) && !isalpha(message[i-1]))
		{
			i_first = i;
			return isVowel(message[i]);
		}
	}

	if(isalpha(message[i])) 
	{
		i_first = i;
		return isVowel(message[i]);
	}

	i_first = i;
	return false;
}

void equipPigLatin(string &message, int &i)
{
	int i_first;
	bool isVowel = trackVowelWord(message, i, i_first);

	if(isVowel)
	{
		i++;
		message.insert(message.begin() + i, 'y');
		message.insert(message.begin() + i, 'a');
		message.insert(message.begin() + i, 'y');

		i += 3;
	}
	else
	{
		char c = message[i_first];
		message.erase(message.begin() + i_first);
		message.insert(message.begin() + i, c);

 		i++;
		message.insert(message.begin() + i, 'y');
		message.insert(message.begin() + i, 'a');

		i += 2;
	}
}

int main() 
{
	int i;
	string message;
	getline(cin, message);

	for(i = 0; i < message.length() - 1; i++)
	{
		if(isalpha(message[i]) && !isalpha(message[i + 1]))
		{
			equipPigLatin(message, i);		
		}
	}

	i = message.length() - 1;
	if(isalpha(message[i-1]))
	{
		equipPigLatin(message, i);
	}

	cout << message;

	return 0;
}
I was not sure how that code work and i felt as though a really important thing that i was missing was being able to seperate and deal with words individually within a string and i wanted to go with my way. But i appreciate the response.
fivestar wrote:
I was not sure how that code work and i felt as though a really important thing that i was missing was being able to seperate and deal with words individually within a string and i wanted to go with my way. But i appreciate the response.

Then don't steal my code if you couldn't find a solution!
Steal your code? I literally posted my own code and wanted ideas on how to make it better. You chose to make your own program and post it and i chose not to use it.
Topic archived. No new replies allowed.