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
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.
#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;
}