Thread: I'll warn you ahead of time...howework help

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    5

    Talking I'll warn you ahead of time...howework help

    I am currently writing a Pig Latin program that simulates the pig latin language. I have gotten the program to read one word and covert it but now I need it to transform an entire sentence and I am stuck. I have no clue where to go from here, I just can't figure out how to read more than one word. Thanks for your help guys!
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    bool isVowel(char ch);
    string rotate(string pStr);
    string pigLatinString(string pStr);
    
    int main()
    {
    	string str;
    	cout << "Enter a string: ";
    	cin >> str;
    	cout << endl;
    
    	cout << "The Pig Latin form of " << str << " is: "
    		 << pigLatinString(str) << endl;
    
    	return 0;
    }
    bool isVowel(char ch)
    {
    	switch (ch)
    	{
    	case 'A': case 'E':
    	case 'I': case 'O':
    	case 'U': case 'Y':
    	case 'a': case 'e':
    	case 'i': case 'o':
    	case 'u': case 'y': return true;
    	default: return false;
    	}
    }
    
    string rotate(string pStr)
    {
    	string::size_type len = pStr.length();
    	
    	string rStr;
    	
    	rStr = pStr.substr(1, len - 1) + pStr[0];
    	
    	return rStr;
    }
    
    string pigLatinString(string pStr)
    {
    	string::size_type len;
    
    	bool foundVowel;
    
    	string::size_type counter;
    
    	if (isVowel(pStr[0]))
    		pStr = pStr + "-way";
    	else
    	{
    		pStr = pStr + '-';
    		pStr = rotate(pStr);
    		len = pStr.length();
    		foundVowel = false;
    
    		for (counter = 1; counter < len - 1; counter++)
    			if (isVowel(pStr[0]))
    			{
    				foundVowel = true;
    				break;
    			}
    			else
    				pStr = rotate(pStr);
    
    		if (!foundVowel)
    			pStr = pStr.substr(1, len) + "-way";
    		else
    			pStr = pStr + "ay";
    	}
    	return pStr;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I just can't figure out how to read more than one word.
    That's easy, just wrap your input and conversion into a loop:
    Code:
    while ( cin>> str )
      cout<< pigLatinString(str) <<' ';
    If you want more control then you'll pretty much need to save the converted words or add a wrapper to pigLatinString that loops over the words in a sentence so that you can use getline.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Representing floats with color?
    By DrSnuggles in forum C++ Programming
    Replies: 113
    Last Post: 12-30-2008, 09:11 AM
  2. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  3. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM