String Display

hello, i am a beginner in C++ and i am currently working with strings. My question is why when compiling the script i'm providing below, i can get the string's characters when i use index notation, but cannot get the string itself using "cout". The script is this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>
#include <string>

using namespace std;

int main()
{
  string original; //original message
  string altered; //message with letter-shift

  original = "abc";
  cout << "Original : " << original << endl; //Display the original message
  for(int i = 0; i<original.size(); i++)
     altered[i] = original[i] + 5;

  //Display altered message
  cout << altered[0] << " " << altered[1] << " " << altered[2] << endl;
  cout << "altered : " << altered << endl;

    return 0;
}


When i run this script, the characters in the string "altered" are displayed correctly with this line :
 
cout << altered[0] << " " << altered[1] << " " << altered[2] << endl;

But the string itself is not displayed with this line :
 
cout << "altered : " << altered << endl;

I would like to know why this happens?
altered is an empty string. Printing an empty string will output nothing.
Last edited on
Topic archived. No new replies allowed.