Having Trouble Looping through rest of string

I am new to this, and I am trying to translate the whole string to pig latin by taking the first letter and putting it at the end of the word then adding "AY " to it. I Am having trouble getting my program to go through the rest of the string. I will only get the first word. I just don't understand how to make it loop through the rest of the string, and would like some helpful hints and tips.
Thank you

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

int main()
{
	string engSentence = "TOM IS A BOY";
	string pigLatin = "";
	string pigEnd = "AY ";
	string str, str2;
	int i = 0;
	int start = 0;
	int end;
	bool isContinue = true;

	while (isContinue)
	{
		if (engSentence.find(" "))
		{
			end = engSentence.find(" ") - 1;
			str = engSentence.substr(start + 1, (end - start));
			str2 = engSentence.substr(start, 1);
			pigLatin += str + str2 + pigEnd;
			start = end + 1;
		}

		if (engSentence.find("\0"))
		{
			end = engSentence.length();
			str = engSentence.substr(start + 1, (end - start));
			str2 = engSentence.substr(start, 1);
			pigLatin += str + str2 + pigEnd;
		}

		if (start = engSentence.length())
		{
			isContinue = false;
		}
	} 

	cout << engSentence << endl;
	
	cout << pigLatin << endl;
	return 0;
}
Last edited on
Hello Newbie9,

Welcome to the forum.

You have included the header file <string>. There are many member functions available for the string class that you should look at and get familiar with because they make you life easier.

I have included the header file <sstream> to use a string stream which is similar to using a file. In this program the "stringstream" only holds one line of text to work with.

This code will do what you want without all the work. If you can not use any of it let me know and I will work on what you have started with. Notice the comments in 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
#include <iostream>
#include <string>
#include <sstream>  // <--- Added.

//using namespace std;  // <--- Best not to use.

int main()
{
	std::istringstream ss;
	std::string engSentence = "TOM IS A BOY";
	std::string pigLatin;  // <--- Does not need initialized.
	std::string pigEnd = "AY ";
	std::string word;
	std::string str, str2;  // <--- May not be needed.
	char firstLtr{};  // <---  Stores first letter of word.
	int i = 0;
	int start = 0;
	int end{};  // <--- Initialized. The only variable that you did not initialize.
	bool isContinue = true;

	ss.str(engSentence);  // <--- Loads "engSentence" into string stream.

	while (ss >> word)  // <--- Extract individual words from string stream.
	{

		firstLtr = word[0];
		word.erase(0, 1);  // <--- erases the first element of the string word.
		pigLatin += word + firstLtr + pigEnd + ' ';

	}

	std::cout << engSentence << std::endl;

	std::cout << '\n' << pigLatin << std::endl;

	return 0;
}


When you have a chance you should look at: http://www.cplusplus.com/reference/string/string/

And for the string stream:http://www.cplusplus.com/reference/sstream/

Hope that helps,

Andy
Topic archived. No new replies allowed.