mardi 24 octobre 2017

Weird behaviour of std::string

I am new to c++ and I have found odd behaviour, when iterating though std::string some last characters are undefined - �. In code below I am using printf with character from string and it works fine, but when I add some other character/new line/white space it breaks. I have also tried cout and iteration, all had ended up with the same result. What could be the problem?

std::string text = "..."; // about 270 characters

for(int i = 0; i < text.size(); ++i) printf("%c", text[i]); //normal
for(int i = 0; i < text.size(); ++i) printf("%c \n", text[i]); //last characters are unknown or random

Output for first for:

.x-..x..-x-.-.x.-x-x..x---x-.xx..x...xx.--x....x.-x-xx.-.x.x--x.-x..x-.x...xx.-x..-.x-x.x.-.xx---x-.x.xx....x.-x...xx..-.x---x.-.x--.x---x-x-x.x-.xx.x...-x.x.-.x-.--x-x....x..x-.x--.xx....x.xx.-..x.x.-x.-.x-.x.x-..xx..x-.xx...x-.-.x....x---x---x.-..x·−·−·−x

Output for second for:

. x - . . x . . - x - . - . x . - x - x . . x - - - x - . x x . . x . . . x x . - - x . . . . x . - x - x x . - . x . x - - x . - x . . x - . x . . . x x . - x . . - . x - x . x . - . x x - - - x - . x . x x . . . . x . - x . . . x x . . - . x - - - x . - . x - - . x - - - x - x - x . x - . x x . x . . . - x . x . - . x - . - - x - x . . . . x . . x - . x - - . x x . . . . x . x x . - . . x . x . - x . - . x - . x . x - . . x x . . x - . x x . . . x - . - . x . . . . x - - - x - - - x . - . . x � � � � � � � � � � � � � � � x

Aucun commentaire:

Enregistrer un commentaire