Right now im making a converting program for myself. I got it to convert correctly but it only converts one character

so here is my code

map<string,string>datamap;

              datamap["A#"]="b";
              datamap["C"]="DD";

              string change;

              getline(cin,change);

              for(int i=0; i<change.size(); i++)
              {
                  //output based on what character gets passed to the map
                  cout << datamap[string(change.begin() +i,change.begin() +i+1)];
              }

the problem is it only converts the C and not AA. so if i eneter CCA#A#C it would cout DDDDDD. i read somewhere that you can peek at the next character and if it is a # you read it the way you were reading characters so that you can add it to your key for the map. but i dont get how you would do this.

Ok, first, it is very ugly to do:

string(change.begin() +i,change.begin() +i+1)

Instead, do this:

change[i]

It will return the ith character of the string.

About my last post, I have realized that operator[] returns a const char& and not a const char* that will have be used to construct a string.

Instead of using peek, you can use conditions, like this (it works):

map<string,string>datamap;

	  datamap["A#"]="b";
	  datamap["C"]="DD";

	  string change = "CCA#A#C";

	  //getline(cin,change);

	  for(size_t i=0; i<change.size(); i++)
	  {
		  if (i != change.size() && change[i+1] == '#')
		  {
			  string toAsk;
			  toAsk.append(1, change[i]);
			  toAsk.append(1, change[i+1]);
			  cout << datamap[toAsk];
			  ++i;					//We will be after the #...
			  continue;
		  }

		  //output based on what character gets passed to the map
		  cout << datamap[string(change.begin() +i,change.begin() +i+1)];
	  }

I hope that it helps...

For more info about peek (for your information), look at this page:

http://www.cplusplus.com/reference/iostream/istream/peek.html

Wow. that works perfectly thanks so much :D

can you explain what this part of the code does a bit?

string toAsk;
			  toAsk.append(1, change[i]);
			  toAsk.append(1, change[i+1]);

i understand by the append that it adds on to the string but thats about it.

This question has already been answered. Start a new discussion instead.