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

  • Context: C/C++ 
  • Thread starter Thread starter RuthKom
  • Start date Start date
  • Tags Tags
    C++ Program
Click For Summary

Discussion Overview

The discussion revolves around understanding how input operations work in C++, particularly focusing on the behavior of the `cin` object when reading strings from user input. The scope includes conceptual clarification and technical explanation relevant to beginners in programming.

Discussion Character

  • Conceptual clarification
  • Technical explanation

Main Points Raised

  • One participant notes that the first `cin >> string1` reads "hello" and the second reads "there," seeking clarification on why this occurs.
  • Another participant suggests that the second input operation continues reading from where the first one stopped, questioning if this understanding is correct.
  • A further response explains that `cin` operates based on terminal behavior, where input is sent to the program upon hitting the carriage return.
  • One participant asserts that the design of the language allows reading part of a line, examining it, and then reading the rest based on the first part's content, implying a purposeful design choice.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the behavior of `cin`, with some seeking deeper clarification while others provide explanations. The discussion does not reach a consensus on the underlying reasons for the input behavior.

Contextual Notes

There are assumptions about the reader's familiarity with console input and the specifics of how `cin` processes input, which may not be explicitly stated. The discussion does not resolve the technical nuances of input handling in C++.

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.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
4K
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K