Pig-Latin

So I'm converting a python program used to turn any phrase into a pig-latin phrase. I got the file working completely correct in python and almost complete in C++. The problem is I need a way to separate the string into each word.
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
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string vowels = "aeiou";
string upvowels = "AEIOU";
string new_word;
string pig_message = "";
string play_again = "Y";
string message;
while (play_again == "Y" || play_again == "y")
{ 
  cout << "Input Word or phrase: ";
  getline (cin, message);
  if (message[0] == vowels[0] || vowels[1] || vowels[2] || vowels[3] || vowels[4])
    {new_word = message + "ay ";
     pig_message = pig_message + new_word;
		}
  else if (message[0] != vowels[0] || vowels[1] || vowels[2] || vowels[3] || vowels[4])
    {new_word = message[1] + message[0] + "ay ";
     pig_message = pig_message + new_word;
	}
  cout << new_word << endl;
  cout << "Would you like to play again? (Y or N)";
  getline (cin, play_again);
}
cout << "Omecay Againay! (Press Enter to exit)";
return 0;
}
Last edited on
To parse message into words, try something like 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

void ParseString( const string& aLine, vector< string >& tokens )
    {
    string buffer;

    stringstream ss( ALine );

    while( ss >> buffer )
        tokens.push_back( buffer );

    }
        /*    ParseString()    */

/* Then loop through the tokens and apply your existing logic:    */

#include <vector>

/*    In main():    */


vector< string > Tokens;


getline( cin, message );

ParseString( message, Tokens );

vector< string >::iterator itr;

for( int i = 0, itr = Tokens.begin(); itr != Tokens.end(); itr++, i++ )
    {
     string AWord = *itr;

    /*    Do your thang here:

    }    /*    for( itr != Tokens.end() )    */


Also I think your if tests aren't quite right. Shouldn't they be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

if( message[ 0 ] == vowels[ 0 ] || message[ 0 ] == vowels[ 1 ] ||
    message[ 0 ] == vowels[ 2 ] || message[ 0 ] == vowels[ 3 ] ||
    message[ 0 ] == vowels[ 4 ] )

    ?

/*    This would be better in a loop:    */

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

        pig_message += new_word;

        }    /*    if( message[ 0 ] == vowels[ 0 ] )    */

    }    /*    for( i < vowels.length() )    */

You've confused me a little I'm a beginner to C++, I imagine what you're doing would work perfectly I'm just confused how to apply it. From 'Void ParseString down to #include <vector>', is all of that code going to work for my code or do I need to change certain variables to fit my code, for example I noticed you used 'string buffer' and I wasn't sure if I needed to change buffer to a variable I had already defined or keep it as is, everything below '#include <vector>' looks like it was designed to fit my code though.


Also I think your if tests aren't quite right. Shouldn't they be:

I mean the code works as is, but if you think you're version is better syntax then I'll change it.
Here's my new 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Pig Latin
// Hunter Griffin
// Based on Python file
// 7-26-11
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main ()
{
string vowels = "aeiou";
string upvowels = "AEIOU";
string new_word;
string pig_message = "";
string play_again = "Y";
string message;
void ParseString( const string& aLine, vector< string>& tokens )
	{
	string buffer;
	
	stringstream ss( ALine);
	
	while( ss >> buffer )
		tokens.push_back( buffer );
	}
while (play_again == "Y" || play_again == "y")
{ 
  cout << "Input Word or phrase: ";
  getline (cin, message);
  if (message[0] == vowels[0] || message[0] == vowels[1] || 
      message[0] == vowels[2] || message[0] == vowels[3] || 
      message[0] == vowels[4])
    {new_word = message + "ay ";
     pig_message += new_word;
	}
  else if (message[0] == upvowels[0] || message[0] == upvowels[1] || 
           message[0] == upvowels[2] || message[0] == upvowels[3] || 
           message[0] == upvowels[4])
    {new_word = message + "ay ";
     pig_message += new_word;
	}
  else if (message[0] != vowels[0] || message[0] != vowels[1] ||
           message[0] != vowels[2] || message[0] != vowels[3] || 
           message[0] != vowels[4])
    {new_word = message[1] + message[0] + "ay ";
     pig_message += new_word;
	}
  else if (message[0] != upvowels[0] || message[0] != upvowels[1] ||
           message[0] != upvowels[2] || upmessage[0] != upvowels[3] || 
           message[0] != upvowels[4])
    {new_word = message[1] + message[0] + "ay ";
     pig_message = pig_message + new_word;
	}
  cout << new_word << endl;
  cout << "Would you like to play again? (Y or N)";
  getline (cin, play_again);
}
cout << "Omecay Againay! (Press Enter to exit)";
return 0;
}


Also I noticed you used stringstream, does that mean i should have a '#include <sstream>' in my header?
Sorry to be confusing. It should all work as-is, but let's break it down:

The ParseString() function is a way to take the user's input and split it out into separate words, which is what you were wondering about. The ParseString() function takes two parameters, a reference to a string (aLine), and a reference to a vector of strings (tokens).

The #include <vector> statement should go right after #include <string>.

In your main() function, add the vector declaration "vector< string > Tokens;". A good place is rignt after string message;

The call to the ParseString() function should be placed after the user types in the input: i.e., right after getline( cin, message );

Now each word the user typed is in the Tokens vector and to get them back out, you use the for() loop I showed in my previous post.

Instead of using message[ 0 ], you would use the string variable AWord, then compair AWord to your vowels.

About your if tests:

You can't compare them like you are doing, because they will always return TRUE. The syntax you have literally says:

1
2
3
4
5
6

if( message[ 0 ] == vowels[0 ] /* Which may or may not be TRUE */ || vowels[ 1 ] /* Which always will be TRUE */ because vowels[ 1 ] and the rest of this array are set to non-zero! */

That is why you must compare them like I showed you.  If you don't believe me, just set vowels = "a" and try it without my changes!

 
My new code has errors, here's 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
38
39
40
41
42
43
44
45
46
// Pig Latin
// Hunter Griffin
// Based on Python file
// 7-26-11
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main ()
{string vowels = "aeiou";
string upvowels = "AEIOU";
string new_word;
string pig_message = "";
string play_again = "Y";
string message;
vector< string > Tokens;
while (play_again == "Y" || play_again == "y")
{ 
  cout << "Input Word or phrase: ";
  getline (cin, message);
  void ParseString( const string& aLine, vector< string>& tokens )
	{
	string buffer;
	
	stringstream ss( ALine);
	
	while( ss >> buffer )
		tokens.push_back( buffer );
	for( int i = 0; i < vowels.length(); i++ )
	    {
		if( message[ 0 ] == vowels[ i ] )
		    {
			new_word = message + "ay ";
				
			pig_message += new_word;	
				}	
			}
	}
  cout << new_word << endl;
  cout << "Would you like to play again? (Y or N)";
  getline (cin, play_again);
}
cout << "Omecay Againay! (Press Enter to exit)";
return 0;
}


and here's the error I get when I attempt to compile it
1
2
3
4
5
6
7
8
hunter@ubuntu:~/Desktop/Programming-Languages/C++/cpp$ g++ Pig-Latin.cpp
Pig-Latin.cpp:19:24: warning: trigraph ??= ignored, use -trigraphs to enable
Pig-Latin.cpp:20:23: warning: trigraph ??= ignored, use -trigraphs to enable
Pig-Latin.cpp:23:80: warning: trigraph ??= ignored, use -trigraphs to enable
Pig-Latin.cpp: In function ‘int main()’:
Pig-Latin.cpp:79:2: error: a function-definition is not allowed here before ‘{’ token
Pig-Latin.cpp:102:1: error: expected ‘}’ at end of input
Pig-Latin.cpp:102:1: error: expected ‘}’ at end of input
I have some ascii art thats why the line errors are messed up, here's the code with the ascii art:
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
92
93
94
95
96
97
98
99
100
101
102
// Pig Latin
// Hunter Griffin
// Based on Python file
// 7-26-11
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main ()
{
cout <<"  .....~~~~~=,.....                              ...... .. "<< endl;
cout <<"         ...:~~~~~~===....                            . ......... " << endl;           
cout <<"        ......~~::~::~~==~....................  ... ......:~::~~~......." << endl;
cout <<"        .....:~::~~=~~~==+=+....................... ...:~~~~~~~~==~....." << endl;        
cout <<"        ....~~~~~~====~===~::::::::~~~:~~~~:::~:.....~~~~~~~~~::~===...." << endl;        
cout <<"        ..,~~~~=========~:::::~~~~~~~~~~~~~~::~~~~~::~~~~~:~~~~~~~=+=..." << endl;       
cout <<"        ..~~====++++==:::~:~~~::~~:~~:~~~:~~~~~~~~~~~::::~~:~======+=~." << endl;         
cout <<"        ,~===++??=~::~:::~::~~:~~~~:~~::~~~~~~~~~~~~~~~~~~=======+=+++=:" << endl;        
cout <<"    ...:~~==++??=:::::~~:::::::~:~~~~~~~~~~~~~~~~~~~~~~~~~:~=++=+=++++==,..." << endl;   
cout <<"    ..~~==++???~~~~~:~~::::::::::~~:~~::~~:~~:~~~~~~~~~~~=~==++++++++++==,.." << endl;    
cout <<"    :~~===?+?+~~~:~~::~:::::::::~~~~~~~~~~~~~~:::~~~~~~=~=====+?+++=+??+==,." << endl;    
cout <<"...=+===++I?+==~~~~~::::::::::::~:~~~~~~~~~~~~~::::~~~~=~=====++++??++???==+...." << endl;
cout <<"..++=++??III==~~~~~~=::::::::::~~~~~~~~~~=~~~~~~~:~~~~~~======+++++??++++++==. ." << endl;
cout <<"..,?I?III77?==~~~~::::::::NNN,:~~~~~~~~~~~~~~~==~~~~~~~~===+==++++??????+?+++=,." << endl;
cout <<".....I7III7$+==~~~~~~~:$,,~~::+~~~~~~~~==~~~~======~===~====+?=+++?IIII????I??I," << endl;
cout <<"...........,==~~~~~~~~~~~~~:~:~:~~~~~~=~====~=~===:$MM7,++===++++??I7II7III7I?,." << endl;
cout <<"        ....=======~~~~~~~~~~~~~~~~~==~==========O,~=~=,M=++++++++??77777777?..." << endl;
cout <<"        ...=~====~=~~===+=+===~======================+====++?+++?+???..........." << endl;
cout <<"        ...========~~=+=D$ON~===~=========+=+=++=++++++++++++??+++?+=..........." << endl;
cout <<"        ..============$N,,,::?~===============++?IIO7++++?++??++??+++,..        " << endl;
cout <<"        ..=====~==~==+M.,,,,:M================++M8,::ON+++++?++??????+..        " << endl;
cout <<"        .=====~+==~===$::,,,:M~==+=======+=+==I=N:::~:N7+++++?++++??++..        " << endl;
cout <<"    ....~+===========~?,IMM.:Z:============++++M,~:~=~,M++++??I??????+.         " << endl;
cout <<"    ..,=++===========~7NNNDMMM:+=======+++++=?=N:,..,=.N+?++??????????:.        " << endl;
cout <<"......+==============+MNN..NNM=~==+===++++=+++~D+MNMNM,M++????????????+.....    " << endl;
cout <<" ....+==~==========~==,NNNMNN:~~========++==++~MNN$,NNM?++??+?????????+?....    " << endl;
cout <<"...,====~~~~====~==~~~:~~NN~=:~~=~==========++?+M:MMMNM~?+?+??+:?+???+++~...  . " << endl;
cout <<"..:+==~~~~~~~~=~:~~~~:~:~::~::::~~~~~~==~=~==~~~D~~NMM~+~+~~??+~????~++~==..    " << endl;
cout <<"..===~~~~~~~~:~~~~~~~~:~:::~~~~::::~::~:~~::::~~::~~~=~=++?+???+????++====+.    " << endl;
cout <<".++====~~~:~:~~~~~~~:::::::::~~~~~~~~~~~~~~~:~::::~~~~=~+++?+??++?+=+=+=====    " << endl;
cout <<"+++=====~~~~~~~~~~~~::::::~:~:~~~~~~~~~~~~~=~~~~~~~~~~=~===++++++=======+=+==..." << endl;
cout <<"?++++===~~~~===~~~~~::::~~~~~:~~====~=~~~~~~~===~~~~~=======+++==========+=++?.." << endl;
cout <<"I?+++===?====+==~~~:~:~~:~::~~============~~~~==+========+=++++==========+++++.." << endl;
cout <<"I??++++++++++=====~~~~~~~:~~=========+===========+======+=+++?+=========+++?+?.." << endl; 
cout <<"I?I??+++++??++====~~~~~~~===================+====++++==+=++++++++====+++?++??I.." << endl;
cout <<"I?I???????II?++===~=~~=~=====++++======++++=+=++++++++++++++?????+++?=??I????I.." << endl;
cout <<".II?????+++++=======+=~=======$7=+=++++88=+++==+=+?++++++++????I?++++I+????III.." << endl;
cout <<"..I????++++==~=++===+=========$7?+==++I$$~++=+===+=+??+?+????IIII??????+???I7:.." << endl;
cout <<"..?7?I??+?+===~=+============~7I+++==+OO7~++=++++++????+?+?IIIIII???????I??II..." << endl;
cout <<"...,7???++++++=~=++===========ZZ===+++=$?~+++=++++????????II?=?I7???I??????I~..." << endl;
cout <<".....?I???++++====+++======+=++==+=+++=~===++++++???????III?++?IIII7IIIIIIII...." << endl;
cout <<"     .I?????++++==~~?++=+++=+=+=++++=+++++=+++++??????II7I??????IIIIIIIIIII.    " << endl;
cout <<"    ...?II+++++++==~=+???+++++++++++++++++?++????IIII$7????I???IIIII7I7III..    " << endl;
cout <<"    ....?I???++++++==++?I?+?I???+++?+?++???????IIII8$?????????III7I7II7II...    " << endl;
cout <<"        ..II??++=+++=+++?III7$I???II?IIIII?I+I$Z$$$7??I??II7?III77II777,....    " << endl;
cout <<"        ...+7????++++==++?IIII77$$$ZZ$$$Z$$Z$$ZZ$7I????II?IIIIIIIIII7......     " << endl;
cout <<"            .II+??+++===+??I????77$7$ZOZZZZ$ZZ$$$I????IIIIIII7I?7$?.            " << endl;
cout <<"            ...I??+++=====++I=+=?I7777$$ZZ$$$$$7II???IIIIII7777$:...            " << endl;
cout <<"            .....I???+++===++?+=+????I777$II7?????I?IIII7I77II......            " << endl;
cout <<"                 ..II??++=++=+++++?????I$+?????I??III77I7$$~....                " << endl;
cout <<"                 ....=II??+===+++====+===+=++???IIII7777,.......                " << endl;
cout <<"                 .......??+?++++==++++=+++??I?IIII77$...........                " << endl;
cout <<"                        ...????????+????II?IIII7$:.....                         " << endl;
cout <<"                         .....??II???I??I7I7=..........                         " << endl;
cout <<"                         .........,,:::,......... .....                         " << endl;
string vowels = "aeiou";
string upvowels = "AEIOU";
string new_word;
string pig_message = "";
string play_again = "Y";
string message;
vector< string > Tokens;
while (play_again == "Y" || play_again == "y")
{ 
  cout << "Input Word or phrase: ";
  getline (cin, message);
  void ParseString( const string& aLine, vector< string>& tokens )
	{
	string buffer;
	
	stringstream ss( ALine);
	
	while( ss >> buffer )
		tokens.push_back( buffer );
	for( int i = 0; i < vowels.length(); i++ )
	    {
		if( message[ 0 ] == vowels[ i ] )
		    {
			new_word = message + "ay ";
				
			pig_message += new_word;	
				}	
			}
	}
  cout << new_word << endl;
  cout << "Would you like to play again? (Y or N)";
  getline (cin, play_again);
}
cout << "Omecay Againay! (Press Enter to exit)";
return 0;
}
I just realized I left some of your code out but I don't know where to put it in.
I missed a question: Yes please add #include <stringstream>.

First, ParseStiring() should NOT be placed in your main() routine. Put it above.

Next add the code I first posted in your main routine like I stated, and make the changes.
Try 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

void ParseString( const string& aLine, vector< string>& tokens )
    {
    string buffer;

    stringstream ss( aLine);

    while( ss >> buffer )
        tokens.push_back( buffer );

    }
        /*      ParseString()   */


int main( int argc, char** argv )
    {
    bool bFoundVowel = false;

    int
        i = 0,
        j = 0;

    string
        message = "",
        new_word = "",
        pig_message = "",
        play_again = "Y",
        vowels = "aeiou";

    vector< string > Tokens;

    while( play_again == "Y" || play_again == "y" )
        {
        pig_message = "";

        Tokens.clear();

        cout << "Input Word or phrase: ";

        getline( cin, message );

        ParseString( message, Tokens );

        vector< string >::iterator itr;

        for( i = 0, itr = Tokens.begin(); itr != Tokens.end(); i++, itr++ )
            {
            string AWord = *itr;

            bFoundVowel = false;

            if( AWord.length() < 2 )
                {
                cerr << "[" << AWord << "] is too short!" << endl;
                    continue;

                }       /* if( AWord.length() < 2 )     */

            for( j = 0; j < vowels.length(); j++ )
                {
                if( tolower( AWord[ 0 ] ) == vowels[ j ] )
                   {
                    bFoundVowel = true;

                    new_word = AWord + "ay ";

                    pig_message += new_word;

                    break;

                    }       /*      If we found a vowel     */

                }       /*      for( j < vowels.length() )      */

            if( ! bFoundVowel )
               {
               new_word = AWord.substr( 1 );

               new_word += AWord[ 0 ];

               new_word += "ay ";

               pig_message += new_word;

              }       /*      if( ! bFoundVowel )     */

          }       /*      for( itr != Tokens.end() )      */

        cout << pig_message << endl;

        cout << "Would you like to play again? (Y or N) ";

        getline( cin, play_again );

    }       /*      while() */

    cout << "Omecay Againay! (Press Enter to exit)";

    return 0;

    }
        /* main()    */
Last edited on
You are an absolute genius!!!!! :) thanks so much
You're welcome. Please mark this as answered.
Topic archived. No new replies allowed.