String Variables in C++

void main(void)
{
  char word[10];

  cout << "Enter a string ";
  cin >> word;
  
  for (int i = 0; i < 10; i++)
    cout << word[i]
}

Output:
Celsius ûü

           word (refers to the entire array)
           ¯	
word[0] ® C	
word[1] ® e	
word[2] ® l	
word[3] ® s	
word[4] ® i	
word[5] ® u	
word[6] ® s	
word[7] ® \0   NULL character added by cin
word[8] ® ?    garbage
word[9] ® ?    garbage
As you can see, it is very important to declare the size of the array large enough to hold the data. Notice the NULL character. This is a sentinel value that marks the end of the string; it does NOT mark the end of the array.

Previous page
Next page

Back to Lesson 12 Index
Back to Outline