Where am I going wrong with my Pig Latin program?

I am currently working on a program for class that is supposed to convert English to Pig Latin. I've gotten it to compile with no errors, but when I execute the program, the response I get in Pig Latin is gibberish. I do believe the function definition is set up properly, as we worked through the logic in class. The main function is supposed to prompt the user for input, invoke the function and display the original input and the output from the function and ask the user to continue. After talking with my professor and other classmates, I think the function call in the main is messed up. My question is this: what am I missing in my set up that ends up returning gibberish? Any assistance that helps my understanding is very, very appreciated. Attached code below.

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
109
110
111
112
113
114
115
116
117
118
#include <iomanip>
#include <iostream>
#include <cstring>

using namespace std;

// Function prototypes
void pigLatinString (char [], int);


void main ()
{
	// Displayed heading of program
	cout << "*********************************************" << endl;
	cout << "* You will be prompted to enter a string of *" << endl;
	cout << "* words. The string will be converted into  *" << endl;
	cout << "* Pig Latin and the results displayed.      *" << endl;
	cout << "* Enter as many strings as you would like.  *" << endl;
	cout << "*********************************************" << endl;

	const int maxLine = 100;
	char phraseLine [maxLine] = {'\0'};
	
	//prompt the user for a group of words or press enter to quit
	cout << "Please enter a word or group of words. (Press enter to quit)\n";
	cin.getline(phraseLine, 100, '\n');
	cout <<endl;

	// This is the main loop. Continue executing until the user hits 'enter' to quit.
	while (phraseLine[0] !='\0')
	{

			// Display the word (s) entered by the user
			cout << "You entered the following: " << phraseLine << endl;

			// Display the word (s) in Pig Latin
			cout << "The same phrase in Pig latin is: " << pigLatinString << endl;
			cout << endl;

			//prompt the user for a group of words or press enter to quit
			cout << "Please enter a word or group of words. (Press enter to quit)\n";
			cin.getline(phraseLine, 100, '\n');
	}	
	return;
}
	
	
void pigLatinString (char phraseLine[], int maxLine) //phraseLine is a cstring for the word,  maxline is max length of line
{ //variable declarations
	char tempConsonant [10];
	tempConsonant [0] = '\0';
	int numberOfConsonants = 0;
	char previousCharacter = ' ';
	char currentCharacter = ' ';
	bool isInWord = 0;
	
	// for loop checking each index to the end of whatever is typed in
	for (int i = 0 ; i < maxLine ; i++)
	{

		//checking for the end of the phraseline 
		if ( phraseLine[i] == '\0')
		{//checking to see if it's in the word
			if (isInWord)
			{//checking to see that there wasn't a space ahead of the word and then sending the cstring + ay to the console
				if (previousCharacter != ' ')
					cout << tempConsonant << "ay" << endl;
			}

			return ;
		}

		// this covers the end of the word condition
		if (isInWord)
		{// covers the condition of index [i] being the space at the end of the word
			if (phraseLine [i] == ' ')
			{
				// spits out pig latin word, gets you out of the word, flushes the temp consonants array and resets the # of consonants to 0
				cout << tempConsonant << "ay";
				isInWord = 0;
				tempConsonant [0] = '\0';
				numberOfConsonants = 0;
			}
			cout << phraseLine [i];
		
		}
		else
		{//this covers for the first vowel that makes the switch
			if (phraseLine[i] != ' ')
			{// sets the c string to what is in the phraseline at the time and makes it capitalized
				char currentCharacter = phraseLine [i];
				currentCharacter = toupper( currentCharacter);

				// this takes care of the condition that currentCharacter is not a vowel
				if ((currentCharacter != 'A') && (currentCharacter != 'E') && 
					(currentCharacter != 'I') && (currentCharacter != 'O') && (currentCharacter != 'U'))
				//this sets the array to temporarily hold the consonants for display before the 'ay'
				{//this sets the null operator at the end of the c string and looks for the next consonant
					tempConsonant [numberOfConsonants] = phraseLine [i];
					tempConsonant [numberOfConsonants + 1] = '\0';
					numberOfConsonants++;
				}
				else
				{// this sets the boolean isInWord to true and displays the phraseline
					isInWord = 1;
					cout << phraseLine[i];
				}
			}
			else
			{
				cout << phraseLine [i];
			}
		}

		previousCharacter = phraseLine[i];
	}
	return ;
}
Last edited on
closed account (10oTURfi)
Will this do for pig latin? I am pretty sure i missed some "rules" of pig latin, but I think my code will help you.
EDIT: I am interested in finishing this program, so can you please explain me rules of pig latin :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string input;
    cout << "Enter a word to translate to pig latin: ";
    cin >> input; //Get the user input
    string temp (input.begin()+1,input.end());//Get rid of first letter and get the rest
    temp = temp + *input.begin() + "ay";//Add the first letter on the end + add the "ay"
    cout << temp << endl;//Output
    return 0;
}
Last edited on
Well, thank you for the assistance you gave thus far- but i need to have the pig latin conversion happen in the function and not the main. The lesson at play this week is using cstring arrays and manipulating them with functions, hence the pig latin exercise. The rules of pig latin are simple, you move all of the consonants before the first vowel to the end of the word and add -ay to the end. For words that begin with vowels, add way to the existing word. An example:

Programming is fun becomes ogrammingpay isway unfay.

Either way, I still get garbage when I try to display the result of the function I call. So, I'm not sure that the function is set up properly or called properly. Everything else with the code seems to work the way I need. I'm just not sure what I'm missing here.
closed account (10oTURfi)
Oh my god. I was reading your pigLatinString function over and over again, failing to understand what is wrong with it.

When you said that you are not sure about function call, I looked at function call and... there is the problem.
Here is fixed 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
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
109
110
111
112
113
114
115
116
117
118
119
120
#include <iomanip>
#include <iostream>
#include <cstring>

//Since those are used in ALL function of program, it wont hurt to set it to global
//Else it is considered EVIL to declare global variables
const int maxLine = 100;
char phraseLine [maxLine] = {'\0'};

void pigLatinString ();

using namespace std;


void main ()
{
	// Displayed heading of program
	cout << "*********************************************" << endl;
	cout << "* You will be prompted to enter a string of *" << endl;
	cout << "* words. The string will be converted into  *" << endl;
	cout << "* Pig Latin and the results displayed.      *" << endl;
	cout << "* Enter as many strings as you would like.  *" << endl;
	cout << "*********************************************" << endl;
	
	//prompt the user for a group of words or press enter to quit
	cout << "Please enter a word or group of words. (Press enter to quit)\n";
	cin.getline(phraseLine, 100, '\n');
	cout <<endl;

	// This is the main loop. Continue executing until the user hits 'enter' to quit.
	while (phraseLine[0] !='\0')
	{

			// Display the word (s) entered by the user
			cout << "You entered the following: " << phraseLine << endl;

			// Display the word (s) in Pig Latin
			cout << "The same phrase in Pig latin is: ";
                        pigLatinString();
			cout << endl;

			//prompt the user for a group of words or press enter to quit
			cout << "Please enter a word or group of words. (Press enter to quit)\n";
			cin.getline(phraseLine, 100, '\n');
	}	
	return;
}
	
	
void pigLatinString () //phraseLine is a cstring for the word,  maxline is max length of line
{ //variable declarations
	char tempConsonant [10];
	tempConsonant [0] = '\0';
	int numberOfConsonants = 0;
	char previousCharacter = ' ';
	char currentCharacter = ' ';
	bool isInWord = 0;
	
	// for loop checking each index to the end of whatever is typed in
	for (int i = 0 ; i < maxLine ; i++)
	{

		//checking for the end of the phraseline 
		if ( phraseLine[i] == '\0')
		{//checking to see if it's in the word
			if (isInWord)
			{//checking to see that there wasn't a space ahead of the word and then sending the cstring + ay to the console
				if (previousCharacter != ' ')
					cout << tempConsonant << "ay" << endl;
			}

			return ;
		}

		// this covers the end of the word condition
		if (isInWord)
		{// covers the condition of index [i] being the space at the end of the word
			if (phraseLine [i] == ' ')
			{
				// spits out pig latin word, gets you out of the word, flushes the temp consonants array and resets the # of consonants to 0
				cout << tempConsonant << "ay";
				isInWord = 0;
				tempConsonant [0] = '\0';
				numberOfConsonants = 0;
			}
			cout << phraseLine [i];
		
		}
		else
		{//this covers for the first vowel that makes the switch
			if (phraseLine[i] != ' ')
			{// sets the c string to what is in the phraseline at the time and makes it capitalized
				char currentCharacter = phraseLine [i];
				currentCharacter = toupper( currentCharacter);

				// this takes care of the condition that currentCharacter is not a vowel
				if ((currentCharacter != 'A') && (currentCharacter != 'E') && 
					(currentCharacter != 'I') && (currentCharacter != 'O') && (currentCharacter != 'U'))
				//this sets the array to temporarily hold the consonants for display before the 'ay'
				{//this sets the null operator at the end of the c string and looks for the next consonant
					tempConsonant [numberOfConsonants] = phraseLine [i];
					tempConsonant [numberOfConsonants + 1] = '\0';
					numberOfConsonants++;
				}
				else
				{// this sets the boolean isInWord to true and displays the phraseline
					isInWord = 1;
					cout << phraseLine[i];
				}
			}
			else
			{
				cout << phraseLine [i];
			}
		}

		previousCharacter = phraseLine[i];
	}
	return ;
}
Last edited on
closed account (10oTURfi)
Ow and one more thing:
You might want to add
#include <cctype>
and then if the first letter is upper
if(isupper(something_here[at_some_place]))
then
something_here = tolower(something_here[at_some_place])
Krofna,

THANK YOU THANK YOU THANK YOU!!! (sorry for the shout, but it's one of happiness). I see exactly what I did wrong. When the prof had mentioned that it was in the way that I was calling the function, I thought he meant what arguments should be put in the call. I never even thought of having the call be on a separate line. Sorry for bringing up such a rookie error. Now that I see it, it makes complete sense why one would want to have the call be on a separate line. It just amazes me (as a novice) how something so small can make all the difference in the world... and that it would compile fine, but return garbage the way I had it set up. I doubt I'll ever forget how to call a void function in my main again, I'll tell you that. Also, I agree that I would want to add that to a fully functional program, but that was not one of the requirements of the assignment, so I opted not to put that in.
closed account (10oTURfi)
Function indeed can be called in cout, but ONLY when it will return a value. Your function is valueless and the cout stuff happens inside it, therfore you MUST call it on separate line.
Also, inside the brackets when calling function you must pass arguments you will use in it, but since I made the variables global it is not required anymore.

Dont worry if error is "stupid". I have both seen and asked worse in past. :)

Also you should consider changing your main function return type to int and make it return 0.
Topic archived. No new replies allowed.