0
string countx(string sky)
{
    int count=0;
    for(int x=0;x<=sky.size();x++)
    {
         if(sky[x]=='x')
        {
            sky.erase(x);
            ++count;

        }
    }

  stringstream ins;
  string bang; // Declare an input string stream.
cout<<"count  " <<count;

ins << count;
bang = ins.str();
   sky=sky+"x^"+ bang;
  cout<<"Sky == "<<sky<<"\n";
  return sky;
}

Writing a function that will take in a string as input and then searches number of "x" in it then it writes x^count.

I am not able to figure out what is wrong ??

I have put on many cout functions .

Example input is

"x x"

It should actually return

"x^2"
but returns "x^1"

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by Sky Diploma
Featured Replies
  • I made slight adjustment: [CODE=cplusplus] sky.erase(sky.begin()+x); [/CODE] More on: [URL="http://www.cplusplus.com/reference/string/string/erase.html"]http://www.cplusplus.com/reference/string/string/erase.html[/URL] EDIT: oh yes, x <= sky.size() is wrong, should be x < sky.size() Read More

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.