The string will not be the first part of the cout

I am writing a game in C ++ and I want it to output the string character by character and slow down the Sleep () function. Line:

string output = "It is Super Effective!" + '\n';  

      

when i try cout << output

it returns "r Effective"

without newline the
code for the display function

void textDisplay(string s){
    s = " " + s;
    for (int i = 0; i < s.size(); i++){
        char c = s[i];
        cout << c;
        Sleep(20);
    }
}

      

+3


source to share


3 answers


This is because it is "It is Super Effective!" + '\n'

interpreted as a pointer to a C string plus an integral character value \n

, which is 10 in ASCII. This happens before being assigned to a C ++ string.

You will see that if you count ten characters from the beginning of your C string, you are indeed in r

.

So, here's what happens, in detail and in sequence:

  • The expression is "It is Super Effective!"

    interpreted as a char pointer to the original character I

    (C string).
  • Then you add ten (cumulative value \n

    ) to that to get a pointer to r

    (still a C string).
  • This C string is "r Effective!"

    then used to build a C ++ string.

In graphical form:

base +  0: | I  | -------+
base +  1: | t  |        |
base +  2: |    |        |
base +  3: | i  |        |
base +  4: | s  |        |
base +  5: |    |     Add ten
base +  6: | S  |        |
base +  7: | u  |        |
base +  8: | p  |        |
base +  9: | e  |        |
base + 10: | r  | <------+
base + 11: |    |
base + 12: | E  |
  :
base + 21: | !  |
base + 22: | \0 |

      

You can change it to add the character to the C ++ string, not the C string:



string output = "It is Super Effective!";
output += '\n';

      

but I'm not really sure why you didn't just do:

string output = "It is Super Effective!\n";

      


One more thing you can pay attention to. It is possible that flushing characters to standard output will only happen when you send a newline, which means you seem to get the entire line in one hit, about seven minutes after you waited for the first character to appear.

If this happens, don't forget to clear the stream after each character:

cout << c << flush;

      

+9


source


In this context, it +

does not concatenate a character with a string literal, but returns the offset value of the pointer \n

byte. In ASCII it will be 0x0A

or 10. This is because a string literal is an array of const char

, which is then decomposed to the address of its first element. The operator +

then offsets 10 characters for that element.

                     1
 0 1 2 3 4 5 6 7 8 9 0
|I|t| |i|s| |S|u|p|e|r| |E|f|f|e|c|t|i|v|e|!|

      

You can fix this by first initializing string

and then adding.

string output = "It is Super Effective!";
output += '\n';

      



Or, you can use string literal concatenation by dropping +

and using "\n"

instead:

string output = "It is Super Effective!" "\n";  

      

Or just do it all in the line that is most understandable.

string output = "It is Super Effective!\n";  

      

+3


source


When the C ++ compiler encounters a statement in your program, it performs overload resolution, just as it does for overloaded functions. Binary operator+

has a lot of overloads, but you care about all of these two:

std::string operator+(std::string, char)   // string concatenation

const char* operator+(const char*, std::ptrdiff_t)     // pointer arithmetic

      

Other answers have explained what these two alternative meanings are +

, I will show you how the compiler determines the second meaning is better.

Your actual parameters are of type const char[N]

and char

. The first argument can implicitly convert to const char*

using array-to-pointer decay and to std::string

, using blur between arrays and pointers, followed by a "user-defined conversion" ( std::string

implicit constructor). The second argument can be converted to any other integral type, including std::ptrdiff_t

implicit expansion.

All the different types of conversions are ranked according to the table in the C ++ standard, and in this table, custom conversions are used only as a last resort. Thus, the arithmetic interpretation of the pointer wins.

+3


source







All Articles