Read array

I'm a beginner and currently learning programming by myself. when I read a book I came across an example which I dont quite understand.
Code:
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
   char string1[ 20 ];                // reserves 20 characters
   char string2[] = "string literal"; // reserves 15 characters

   // read string from user into array string2
   cout << "Enter the string \"hello there\": ";
  [B] cin >> string1;                   // reads "hello" [/B]
   cout<< "\nstring1 is: " <<string1<<endl;

   [B]cin >> string1;  // reads "there"[/B]
   cout << "\nstring1 is: " << string1 << endl;

   return 0;  
}
I know that space terminates the input when the first "cin>>string1" statement is executed, but I dunno why the second "cin>>string1" reads "there"...can anyone tell me the reason behind?
Please help me...Thanks very much:)
 

jtbell

Mentor
15,193
2,787
The second input operation starts reading where the first one stopped. Or have I missed the point of your question?
 
Thx jtbell
I wanna know why it starts reading where the first one stopped...
 

jim mcnamara

Mentor
3,356
1,574
cin operates on the basis of a console or terminal. The terminal driver "sends" blocks of text to the OS -> program when it gets a carraige return - newline - ie., you hit <return>
 

jtbell

Mentor
15,193
2,787
I wanna know why it starts reading where the first one stopped...
Because that's the way the language was designed!

I think it's useful, because it lets you read part of a line, examine what you've read, and then read the rest of the line differently depending on what the first part of the line contains.
 

The Physics Forums Way

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top