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.
#include <iomanip>
#include <iostream>
#include <cstring>
usingnamespace 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;
constint 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 ;
}
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>
usingnamespace 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;
}
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.
#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
constint maxLine = 100;
char phraseLine [maxLine] = {'\0'};
void pigLatinString ();
usingnamespace 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 ;
}
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])
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.
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.