0

I have the next code:

void addContent ( const std::string& message ) {
   std::string newMessage;

   for ( int i = 0, remainder = textCapacity - currentText; i < remainder; i++ ) {
      newMessage[i] = message[i];
      std::cout << newMessage; //here nothing is printed
   }
}

But nothing is printed.

Only if I change newMessage to newMessage[i] everything is good. And I dont undestand why?

Alex K.
  • 171,639
  • 30
  • 264
  • 288
user3402740
  • 45
  • 1
  • 7
  • possible duplicate of [No console output on cout](http://stackoverflow.com/questions/4591915/no-console-output-on-cout) – Nobody moving away from SE Mar 10 '14 at 17:16
  • Try `std::cout << newMessage << "\n"` – herohuyongtao Mar 10 '14 at 17:16
  • `newMessage` is an empty string. To see the error put in a try/catch block. – 001 Mar 10 '14 at 17:18
  • @Nobody: not a duplicate of that, as it's not a case of buffering. It's a case of invalid memory access and an empty string. – Cornstalks Mar 10 '14 at 17:18
  • "everything is good" - no it isn't. Now you're writing to invalid memory, then reading back what you've just written. Maybe it will appear to work; maybe it will crash; maybe it will corrupt something unrelated and cause hideously intractible bugs. – Mike Seymour Mar 10 '14 at 17:20
  • You probably want to [append](http://www.cplusplus.com/reference/string/string/operator+=/) – 001 Mar 10 '14 at 17:22
  • I see that I created an empty string! In that case how I can copy a part of one string to another string? – user3402740 Mar 10 '14 at 17:56

5 Answers5

0

newMessage is an empty std::string. Doing [i] to it is accessing invalid memory. The string is always empty, and you're just writing to invalid memory. That's a recipe for disaster, and you're (un)lucky it's not crashing on you.

I'm not sure what message[i] is, but you probably want newMessage[i] = message[i]. But you might as well skip the temporary newMessage variable and just print out message[i] itself.

Cornstalks
  • 37,137
  • 18
  • 79
  • 144
0

newMessage is an empty string, so nothing will be printed. Also, std::cout is buffered, so in order to flush the buffer you should call std::endl or std::flush

Sean
  • 60,939
  • 11
  • 97
  • 136
0

I would rather change from this:

newMessage[i] = message[i];

to this:

newMessage += message[i];

And when printing:

std::cout << newMessage<<std::endl;

Using [i] on empty string is looking for trouble because your entering invalid out of bound memory. Sometimes it will do nothing sometimes your program will crash.

Ardel
  • 315
  • 2
  • 9
0

As Cornstalks said, you have an out-of-bounds access.

More importantly, the code is way too complex for the task. Don't use a manual loop to partially copy one std::string to another. To copy a part of message to newMessage, use substr on message and assign:

newMessage = message.substr(from_index, number_of_chars);

or the iterator-based stuff:

std::string newMessage(message.begin() + from_index, message.begin() + to_index);

The latter is more efficient. So you want

std::string newMessage(message.begin(), message.begin() + (textCapacity - currentText));
Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
0

Using that string as newMessage[i] is implying that it an array of strings. Replace that line with std::string newMessage[textCapacity];.

Eitan Myron
  • 159
  • 1
  • 4
  • 16