Pig Latin Function Issues
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Pig Latin Function Issues

  1. #1
    Join Date
    Mar 2008
    Location
    USA
    Posts
    12

    Pig Latin Function Issues

    I am working on a program that inputs various sentences as C-strings and does various string conversions on each sentence. I've done all of the conversions but one: converting the sentence into Pig Latin. I am only allowed to use the <cctype> and <cstring> libraries to manipulate the strings, and no string class functions.

    The Pig Latin rules I was given are:
    1. if a word begins with a vowel, append "-way" ot the end
    2. if a word starts with a consonant, identify the consonants up to the first vowel and move these consonants to the end of the word, then add "ay"
    3. include "y" as a vowel

    So if the original sentence was: This is another fine day.
    The Pig Latin sentence would be: Is-thay is-way another-way ine-fay ay-day.

    I created a function just for the Pig Latin portion of the program. This is what I have so far:
    Code:
     
    void findPigLatin(int len, char array1[], char pigLatin[], ofstream &outputFile)
    {
    	int index, begin, i, j, k, l, start;
    	 char word[15];
    	 start = 0;
    	 for (index = 0; index < len; index++)
    	 {
    		 if(isspace(array1[index]))
    		 {
    			 for(j = start; j < index; j++)
    			 {
    				 start = index;
    				 word[k] = array1[j];
    				 cout << word[k];
    				 if(word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u' || word[0] == 'y')
    				 {
    					 for (l = begin; l < index; l++)
    						 outputFile << word[l];
    					 outputFile << "-way";
    				 }
    				 else if(word[k] == 'a' || word[k] == 'e' || word[k] == 'i' || word[k] == 'o' || word[k] == 'u' || word[k] == 'y')
    				 {
    					 for(i = k; i < index; i++)
    						 outputFile << word[i];
     
    				 }
    				 k++;
    			 }
    		 }
    	 }
    }
    Everytime I run it, it crashes and doesn't output anything to the output file. However, when I comment out this function, the rest of the program runs fine.

    Can anyone help me figure out what I'm doing wrong, and/or give any suggestions on how I can do this Pig Latin portion. Thanks in advance.

  2. #2
    Join Date
    Mar 2008
    Location
    USA
    Posts
    12

    Re: Pig Latin Function Issues

    Bump. I've been trying to figure something out while waiting for a response, but I'm not getting anywhere. Any help is greatly appreciated.

  3. #3
    Join Date
    Mar 2008
    Posts
    8

    Re: Pig Latin Function Issues

    Have you tried stepping through the function? That might give you a better idea where the issue might be. Sorry if that isn't much help.

  4. #4
    Join Date
    Apr 2008
    Posts
    4

    Re: Pig Latin Function Issues

    Without running this and debugging I can see a few problem areas:
    Code:
    word[k] = array1[j];
    You never initialise 'k'. You cannot guarantee that k is 0 at this point. I suggest initialising it before first use. Secondly I notice that you are incrementing 'k' in a for loop which is limited only by the value of 'len' which is passed into the function. However the 'word' array is only 15 elements long. Does 'k' ever go over 15? Here
    Code:
    outputFile << word[i];
    Does 'i' ever get bigger than 15? Again,
    Code:
    outputFile << word[l];
    does 'l' get bigger than 15?

    A crash usually indicates that you're trying to access an invalid place in memory. Check that everywhere you are accessing an element in an array that it's valid.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center