Thread: poiglatin problem?

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    poiglatin problem?

    Greetings,
    I am stuck, plks help fix? Any help is appreciated.
    Code:
    #include <iostream>                 // cin, cout, <<, >>
    #include <string>                   // class string
    using namespace std;
    
    string Piglatin(string englishWord);
    
    int main()
    {
      cout << "\nTo translate a sentence from English to Pig-latin,\n"
           << " enter a sentence: ";
    
      string englishWord,
             piglatinWord;
      char separator;
    
      for (;;)
        {
          cin >> englishWord;
          cin.get(separator);
          piglatinWord = Piglatin(englishWord);
          cout << piglatinWord;
          if (separator == '\n')
    	{
    	  cout << endl;
    	  break;
    	}
          else
    	cout << ' ';
        }
      return 0;
    }
         
    
    /*********************************************************
     * Piglatin translates a word from English to Pig-latin. *
     * Receive: englishWord, a string.                       *
     * Return: piglatinWord, a string.                       *
     *********************************************************/
    
    string Piglatin(string englishWord)
    {
    int vowelPosition = englishWord.find_first_of("aeiouyAEIOUY", 0);
    string lastPart = englishWord.substr(vowelPosition, 
                                           englishWord.size() - vowelPosition);
    string pigLatinWord = lastPart + firstPart + "ay";
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Well, for one thing, you have your Piglatin function returning 0 instead of piglatinWord, that'll screw things up.
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Couple of other things...
    1. Instead of using cin.get() to find the next character, use peek(). get() removes the next character, which is the first character of the next word ("cin >> word" strips the whitespace separator as well).

    2. Move the peek to after the calculation and display of the pig latin word. Don't know why this helped but it did.

    3. You never calculated firstPart in the Piglatin function.

    Your revised code:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string Piglatin( string );
    
    int main() {
        cout << "\nTo translate a sentence from English to Pig-latin,\n"
             << " enter a sentence: ";
    
        string englishWord, piglatinWord;
        char separator;
    
        for (;;) {
            cin >> englishWord;
            piglatinWord = Piglatin( englishWord );
            cout << piglatinWord;
            separator = cin.peek();
            if (separator == '\n') {
                cout << endl;
                break;
            } else {
                cout << ' ';
            }
        }
        return 0;
    }
    
    string Piglatin( string englishWord ) {
        int vowelPosition = englishWord.find_first_of( "aeiouyAEIOUY", 0 );
        string firstPart = englishWord.substr( 0, vowelPosition );
        string lastPart = englishWord.substr( vowelPosition,
                        englishWord.size() - vowelPosition);
        string pigLatinWord = lastPart + firstPart + "ay";
        return pigLatinWord;
    }
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  4. #4
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    I know it is in C but
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void printLatinWord( char * );
    
    int main()
    { 
       char sentence[ 80 ];
       char *tokenPtr;
       
       printf( "Enter a sentence:\n" );
       gets( sentence );
       
       printf( "\nThe sentence in Pig Latin is:\n" );
       tokenPtr = strtok( sentence, " .,;" );
       
       while ( tokenPtr ) { 
          printLatinWord( tokenPtr );
          tokenPtr = strtok( NULL, " .,;" );
          
          if ( tokenPtr )
             printf( " " );
       }
       
       printf( "." );
    
       return 0;
    }
    
    void printLatinWord( char *word )
    { 
       int i;
    
       for ( i = 1; i < strlen( word ); i++ )
          printf( "%c", word[ i ] );
    
       printf( "%c%s", word[ 0 ], "ay" );
    }
    Mr. C: Author and Instructor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM