Pig-Latin Translator
-
I have been working on this for a couple of days. I am having trouble understanding why my program seems to hang after the input is translated. From what I can see the program never leaves the while loop. I must clarify that I still am a newbie so forgive me for my stupidity. Any help will be greatly appreciated. Here is what I have so far: #include #include #include class translate { private: char english[20]; public: translate(); }; int main() { translate sentence; return 0; } translate::translate() { char ans; do { cout<<"Please enter a sentence:\n"; while(cin>>english) { if((english[0] == 'a') || (english[0] == 'a') || (english[0] == 'A') || (english[0] == 'e') || (english[0] == 'E') || (english[0] == 'i') || (english[0] == 'I') || (english[0] == 'o') || (english[0] == 'O') || (english[0] == 'u') || (english[0] == 'U')) { cout<> ans; } while((ans == 'y') || (ans == 'Y')); return; }
-
I have been working on this for a couple of days. I am having trouble understanding why my program seems to hang after the input is translated. From what I can see the program never leaves the while loop. I must clarify that I still am a newbie so forgive me for my stupidity. Any help will be greatly appreciated. Here is what I have so far: #include #include #include class translate { private: char english[20]; public: translate(); }; int main() { translate sentence; return 0; } translate::translate() { char ans; do { cout<<"Please enter a sentence:\n"; while(cin>>english) { if((english[0] == 'a') || (english[0] == 'a') || (english[0] == 'A') || (english[0] == 'e') || (english[0] == 'E') || (english[0] == 'i') || (english[0] == 'I') || (english[0] == 'o') || (english[0] == 'O') || (english[0] == 'u') || (english[0] == 'U')) { cout<> ans; } while((ans == 'y') || (ans == 'Y')); return; }
Constructors are for initializing member variables of a class instance, you should place the application specific code within a function that you call after construction, not during. Ex:
class translate
{
public:
translate(){}
int Process();
};int translate::Process()
{
// application code here...
}
Nick Parker
The only man who never makes a mistake is the man who never does anything. - Theodore Roosevelt
-
I have been working on this for a couple of days. I am having trouble understanding why my program seems to hang after the input is translated. From what I can see the program never leaves the while loop. I must clarify that I still am a newbie so forgive me for my stupidity. Any help will be greatly appreciated. Here is what I have so far: #include #include #include class translate { private: char english[20]; public: translate(); }; int main() { translate sentence; return 0; } translate::translate() { char ans; do { cout<<"Please enter a sentence:\n"; while(cin>>english) { if((english[0] == 'a') || (english[0] == 'a') || (english[0] == 'A') || (english[0] == 'e') || (english[0] == 'E') || (english[0] == 'i') || (english[0] == 'I') || (english[0] == 'o') || (english[0] == 'O') || (english[0] == 'u') || (english[0] == 'U')) { cout<> ans; } while((ans == 'y') || (ans == 'Y')); return; }
-
I have been working on this for a couple of days. I am having trouble understanding why my program seems to hang after the input is translated. From what I can see the program never leaves the while loop. I must clarify that I still am a newbie so forgive me for my stupidity. Any help will be greatly appreciated. Here is what I have so far: #include #include #include class translate { private: char english[20]; public: translate(); }; int main() { translate sentence; return 0; } translate::translate() { char ans; do { cout<<"Please enter a sentence:\n"; while(cin>>english) { if((english[0] == 'a') || (english[0] == 'a') || (english[0] == 'A') || (english[0] == 'e') || (english[0] == 'E') || (english[0] == 'i') || (english[0] == 'I') || (english[0] == 'o') || (english[0] == 'O') || (english[0] == 'u') || (english[0] == 'U')) { cout<> ans; } while((ans == 'y') || (ans == 'Y')); return; }
Thanks for the advice, but that doesn't solve my problem.
-
Thanks for the advice, but that doesn't solve my problem.
without looking @ your code in depth.. i can suggest you a ways to solve your infinite loop problem.. instead of using while(cin>english), you can use the length of the string of the input as your while conditional variable.. every loop, you minus the conditional variable by 1.. by the time the conditional variable reach 0(meaning you have finished check every letter), your program will exit the loop for sure..
-
I have been working on this for a couple of days. I am having trouble understanding why my program seems to hang after the input is translated. From what I can see the program never leaves the while loop. I must clarify that I still am a newbie so forgive me for my stupidity. Any help will be greatly appreciated. Here is what I have so far: #include #include #include class translate { private: char english[20]; public: translate(); }; int main() { translate sentence; return 0; } translate::translate() { char ans; do { cout<<"Please enter a sentence:\n"; while(cin>>english) { if((english[0] == 'a') || (english[0] == 'a') || (english[0] == 'A') || (english[0] == 'e') || (english[0] == 'E') || (english[0] == 'i') || (english[0] == 'I') || (english[0] == 'o') || (english[0] == 'O') || (english[0] == 'u') || (english[0] == 'U')) { cout<> ans; } while((ans == 'y') || (ans == 'Y')); return; }
while(cin>>english)
Remove this while loop and just docin>>english
as this is causing an infinite loop and not getting to yourcout<<"Would you like to have another sentence translated,?\n"; cout<<"Type [Y] for yes and [N] for no:\n";
code. Michael The avalanche has started, it's too late for the pebbles to vote. -
without looking @ your code in depth.. i can suggest you a ways to solve your infinite loop problem.. instead of using while(cin>english), you can use the length of the string of the input as your while conditional variable.. every loop, you minus the conditional variable by 1.. by the time the conditional variable reach 0(meaning you have finished check every letter), your program will exit the loop for sure..
How can I get the length of the string if it hasn't even been inputed by that point?
-
How can I get the length of the string if it hasn't even been inputed by that point?
well, you get the input first, then get the length of the string.. once you done all that, you do your while loop.. lemme give you pseudo codes here: let x, y be variables x = input string from the user y = string length of x while(y) { ...do your pig latin thing... y = y -1 }