C/C++ Understanding C++ Program Inputs: A Beginner's Guide

  • Thread starter Thread starter RuthKom
  • Start date Start date
  • Tags Tags
    C++ Program
AI Thread Summary
The discussion revolves around a beginner's confusion regarding the behavior of the `cin` input stream in C++. The user notes that when they input the string "hello there", the first `cin >> string1` reads "hello" and the second `cin >> string1` reads "there". This behavior occurs because `cin` stops reading input at the first space, treating it as a delimiter. The subsequent input operation continues from where the previous one left off, allowing for partial line reading. This design enables programmers to process input in segments, which can be useful for examining and manipulating data based on user input. The responses clarify that this functionality is inherent to the language's design, emphasizing its practical applications in programming.
RuthKom
Messages
2
Reaction score
0
I'm a beginner and currently learning programming by myself. when I read a book I came across an example which I don't 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 don't know why the second "cin>>string1" reads "there"...can anyone tell me the reason behind?
Please help me...Thanks very much:)
 
Technology news on Phys.org
The second input operation starts reading where the first one stopped. Or have I missed the point of your question?
 
Thx jtbell
I want to know why it starts reading where the first one stopped...
 
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>
 
RuthKom said:
I want to 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 let's 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top