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.

According to cplusplus.com (http://www.cplusplus.com/reference/string/string/append.html) ...

string& append ( size_t n, char c );
Appends a string formed by the repetition n times of character c.

Here, a string is formed to make the request to the map. This is how I do it. There are other ways to do it, like using iterators. You can see it on the URL I put above.

i get it now. thanks a bunch for the help

Don't forget to make the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.