0

Changing String Variables into int in C++

I was trying to change a variable called username in my code. This is what it looked like: string username = "name"; username = 1; cout<<username; And once I ran that code, it had an output of a ? in a box. I am wondering how to fix it, not what the ? in a box means.

4th Aug 2025, 5:02 PM
Ashton Hively
Ashton Hively - avatar
2 Answers
+ 2
What output were you expecting? ⍰ indicates that the character cannot be displayed by the character set you are using. Despite it being supposed to raise a compilation error, it doesn't output anything for me on Sololearn playground (Android).
4th Aug 2025, 5:15 PM
Lisa
Lisa - avatar
0
The compiler will try ti cast an int to char then create a string from latter. In practice, assigning an int to string will create first a char having the int ascii code the will be created a string with that char.
4th Aug 2025, 8:19 PM
KrOW
KrOW - avatar