Using append and erase on strings

I need some advice and to be pointed in the right direction on how to use append and erase properly. On an assignment I'm working on, I have to mix around some string data that is laid out in a modified form of pig latin. I have to convert the modified pig latin to english and I can do that for words that begin with vowels but for words with consonants, I'm having issues. Here is an example of what I can't seem to figure out.
Take "ig-pay" and make it "pig", or "anslation-tray" and make it "translation"

Below is my code for the void function I have, which like I said converts words that begin with a vowel easily using the srt.erase function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void convert_pigs (ofstream& out, string& word)

{

  string temp; // Temporarily stores string data
  string temp2; // Also temporatily stores string data

  for (int i=0;i<word.length();i++)
    {
      if (word[i]=='-')
	{
	  if (word[i+1]=='a')
	    word.erase(i);
	  else
	    {
	      temp.append(word,i+1,2);
	      word.erase(i);
	      temp+word=temp2;
	      temp2 = word;
	    }
	}
    }

}


Any thoughts?
Use string::find to locate the '-' (e.g. from "ig-pay"). Then use string::substr to extract the suffix "ig" and latin "pay".

Iterate the latin to retrieve original characters of prefix. Finally combine the prefix and suffix back to word.

Note:
temp+word=temp2;
is not legal. You attempt to create a nameless temporary object that holds the result of temp+word, and then assign value of temp2 into that nameless object.

In assignment lhs = rhs; the value of rhs is copied into object lhs.
Ah, thanks for the reply and help. I always forget the order of how copying works. What I intended to do when I was using temp+word = temp2 was concatenate the two values into the temp 2 string, but alas I see where I was wrong.

Thanks again
Topic archived. No new replies allowed.