Why is my string variable not printing a full string?

user12828132

Program:

#include <iostream>
using namespace std;

int main() {
    string str;
    str[0] = 'a';
    str[1] = 'b';
    str[2] = 'c';

    cout << str;

    return 0;
}

Output:

No output.


If I replace cout << str; with cout << str[1], I get a proper output.

Output:

b

And if I change the data type of the variable to a character array, I get the desired output. (Replacing string str; with char str[5];

Output:

abc

Why is my program behaving like this? How do I alter my code to get the desired output without changing the data type?

walnut

Your program has undefined behavior.

string str;

creates an empty string. It has length 0.

You are trying to write to the first three elements of this string with

str[0] = 'a';
str[1] = 'b';
str[2] = 'c';

These do not exist. Indexing a std::string out-of-bounds causes undefined behavior.

You can add characters to a string with any of the following methods:

str += 'a';
str += "a";
str.push_back('a');
str.append("a");

or you can resize the string first to the intended length before you index into any of its elements:

str.resize(3);

As pointed out by @Ayxan in a comment under this answer, you are also missing #include<string>. Without it it is unspecified whether your program will compile since it uses std::string which is defined in <string>. It is unspecified whether including one standard library header will include another one if there isn't a specific exception. You should not rely on unspecified behavior that may break at any point.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why my program is not printing string?

Why is my string not printing in a function?

Why is the program printing numbers when the variable is a string?

Printing string variable in Java

Printing address of a variable as a string

why cout is not printing this string?

Why is my variable not printing to the screen?

Why is Bash not recognising my variable as a string?

Why does my looping variable not convert to a string?

Why my string variable recognized as empty?

Why is my Swift string variable returning nil?

Printing EJS variable with string quotes

Variable Appendendage Printing Literal String

Why is the String not printing on the JLabel? (Java)

Why is x printing the size of string?

Why isn't my Bit String to Hex String code printing the result?

String.replace() method not printing full string in java

Why is my 'char' variable not printing an output?

Why is get_env() treating my boolean environment variable as a string?

Why a method is modifying a List<string> variable in my Load Page?

Why does string not pass correctly to server when there is a '#' in my variable?

Why is setting the variable to an empty string not needed in my code?

Printing variable value prints string Shell scripting

File object variable is not printing newline for \n in the string

How to pass variable to JSON without printing it as String

How to avoid printing variable in doublequote string

Printing a variable number of arguments to a format string

Printing integer variable and string on same line in SQL

String variable preventing class data from printing