Program Help

I have a program that compiles and runs fine, I am having problems with the output. The program takes a group of words and translates them into pig latin. It should read:

Enter a group of words or ENTER to quit: Hello Daddy Jim

Original Words: Hello Daddy Jim
New Words: elloHay addyDay imJay

But this is what comes out:
Enter a group of words or ENTER to quit: Hello Daddy Jim

Original Words: Hello Daddy Jim
Hello
Daddy
New Words: elloHay addyDay imJay

I know it has to be something stupid, but I have been working on this for hours and cannot seem to find the problem. Any suggestions?

#include <iostream>
#include <string>

using namespace std;

//Function prototype
string PigLatinString(string pigString);

int main()
{
// Declare variables
string str;

// Display heading
cout << "**************************************************\n";
cout << "*** You will be prompted to enter a string of ***\n";
cout << "*** words. The string will be converted into ***\n";
cout << "*** Pig Latin and the results displayed. ***\n";
cout << "*** Enter as many strings as you would like. ***\n";
cout << "**************************************************\n";

// While condition is true loop
while(true)
{
// Prompt user to enter a group of words or quit
cout << "\nEnter a group of words or ENTER to quit: ";
getline(cin, str);

// Break loop if string is empty
if(str.length() == 0)
break;

// Display original words
cout << "Original words: " << str << endl;

// Display pig latin words
cout << "New Words: " << PigLatinString(str) << endl;
}
return 0;
}

// PigLatinString function
string PigLatinString(string pigString)
{
// Declare function string variables
string::size_type len = pigString.length();
string::size_type counter();
string::size_type start = 0;
string::size_type begin = 0;
string phrase, newString = "";
phrase = pigString.substr(begin, pigString.length() - begin);

// PigLatinString function loop
while(true)
{
// PigLatinString conversion
start = pigString.find(' ', start);
if(start == string::npos) break;
phrase = pigString.substr(begin, start - begin);
cout << phrase << endl;
newString += phrase.substr(1) + phrase.substr(0, 1) + "ay ";
start++;
begin = start;
}
// Call substr() and update new string
phrase = pigString.substr(begin, pigString.length() - begin);
newString += phrase.substr(1) + phrase.substr(0, 1) + "ay";

return newString;
}

1
2
3
4
5
6
7
8
9
10
11
while(true)
{
// PigLatinString conversion
start = pigString.find(' ', start);
if(start == string::npos) break;
phrase = pigString.substr(begin, start - begin);
cout << phrase << endl;
newString += phrase.substr(1) + phrase.substr(0, 1) + "ay ";
start++;
begin = start;
}
aaarrrggghhhh!! I hate it when it is something that is so obvious and I still cannot see it. Thanks JJ, I appreciate the help!
On another note, I am really trying to develop good habits when writing the programs. Any comments or suggestions on the content or comments written in the program are welcomed. The Professor only seems concerned on the program working and not the layout or substance of the program. Thanks!
Readability is one important aspect of coding.

and this code, while syntactically correct

while(...){if(start==string::npos)break;phrase=pisString.substr(begin,start-begin);newString+=phrase.substr(1)+....;}

is much harder for yourself and others to read and debug compared to

1
2
3
4
5
6
7
8
while (...)
{
   if(start==string::npos)
      break;

   phrase=pisString.substr(begin, start-begin);
   newString += phrase.substr(1) + ....;
}
Topic archived. No new replies allowed.