I have a logical error that I just can't spot!

Damn I have been stuck on this function for hours trying to figure out why it won't decrypt a pig latin message into the original word.

This is just my function since the entire program is rather large. What happens is if my message is "ig-pay atin-lay" I end up with just "ati" instead of pig latin.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
void FromPigLatin (string inputFileName, string outputFileName) {

  ifstream messageInFile;
  ofstream messageOutFile;
  
  int charPosition;
  char character;
  string word;
  string prefix;      
  string postfix; 
  string decryptedMessage;
 
  messageInFile.open(inputFileName.c_str());
  
  messageInFile >> word;
  while (messageInFile) {
         
     for (charPosition=0; charPosition < word.size(); charPosition++) {
          character = word[charPosition];
       
       if  (IsDash(character)) {
           postfix = word.substr(0, charPosition - 1);  
       } 
	 }  
     for (charPosition=charPosition; charPosition < word.size(); charPosition++) {
    
       if (!IsVowel(character)) {
          prefix = prefix + character;    
	   }   
       if (IsVowel(character)) {         
	      charPosition = word.size(); 
       }
     }
  decryptedMessage = prefix + postfix + " "; 
  prefix = "";
  postfix = "";
  messageInFile >> word;  
  }
  
  messageInFile.close();
  messageOutFile.open(outputFileName.c_str());
  
  messageOutFile << decryptedMessage;
  
  messageOutFile.close();



return;
}
Two things, you have charPosition=charPosition on line 25 and the extraction operator is a horrible way of getting strings, use std::getline (std::istream,std::string) instead.
The only reason I use the extraction operator is because I haven't learned anything else yet really but I'll try to use what you stated. But wouldn't the getline grab all the strings? I just want to work with one string at a time.

About my second for loop.

The reason I put charPosition = charPosition is because I wanted the loop to start at the dash position and not back at 0. What am I supposed to use as my initalizer to keep the curser at that position?
Last edited on
The getline function will only get a line of text, so if all the strings are on the same line then it will get all the strings. So in this case the extraction would be better since it needs to be read in as a single word at a time.

To do what you want to do, you would need to break or store where the dash is in another variable, dashPosition and set charPosition to it in the for loop to start at that position.
I think you need to add this to line 26:
 
character = word[charPosition];

And the extraction operator is the right thing to do in this case.
1
2
3
//postfix = word.substr(0, charPosition - 1);
postfix = word.substr(0, charPosition);
 

The second parameter it's the length of the substring. So you must not do the decrease.

The reason I put charPosition = charPosition is because I wanted the loop to start at the dash position and not back at 0. What am I supposed to use as my initalizer to keep the curser at that position?

No need to do that. A variable doesn't change its value at least you told it to do it. (and you need to move 1 step actually)

In the loop for the prefix you aren't updating character.

1
2
3
if(cond){}
//if(!cond){}
else{} //note that the conditions are complementary 


Also, just a preference, but try to not put so long names to index and aux variables. And keep your functions small (why not just pass the word to convert, instead to asking the function to read it from a file)
Just for the record, you don't have to fill every for clause:
 
for( ;charPosition < word.size(); charPosition++) // first clause empty 
I see now why what I suggested would be unneeded.
Thanks guys for the feedback.

This function really twisted my brain around and I ended up learning the .find command to find specific characters.

So I set a variable for the dash and a variable for the "ay" after the dash if it was found to make sure I had a word in pig latin.

Then I took substrings from position 0 to the dash for the post fix and from the char after the dash to right before the "ay" as the prefix and just added them togethor to get the original word back.

And if a dash and proceeding "ay" weren't found I just sent the word straight through since it obviously wasn't a pig latin word unless it happened to be a freakish word that had a dash and ay after it. haha

My prof wasn't worried about that.

But shit, coding is tough man. This is only my second programming class and I have spent more time working on this class alone than all my other 4 combined!
Topic archived. No new replies allowed.