-1
#include <iostream>
using namespace std;

int main()
{
   const int SIZE=4;
   char Sentence[SIZE];
   char Sentence2[SIZE];

   cout << "Enter the sentence" <<endl;
   cin >> Sentence;

   cout << "\nThe string read with cin was"
 << endl <<Sentence <<endl;
  char ch = cin.get();

  cout << "Enter the second sentence: "<<endl;
  cin.get(Sentence2,SIZE,'$');
  cout << Sentence2 <<endl;
}

OUTPUT


Enter the sentence
This is my first sentence

The string read with cin was
This
Enter the second sentence:
is

I have just started learning C++ and I am not able to understand this program as well as it's output. Pls can anyone suggest me where to learn it from or explain it in detail .

Ash Ketch
  • 17
  • 3
  • Which part do you not understand? – Aykhan Hagverdili Feb 26 '21 at 11:43
  • 2
    `char` array of size 4 can only fit 3 characters in it, leaving a space for null terminator. – Yksisarvinen Feb 26 '21 at 11:43
  • I don't understand the output of this part : cout << "Enter the second sentence: "< – Ash Ketch Feb 26 '21 at 11:44
  • Yes Can u please explain it – Ash Ketch Feb 26 '21 at 11:45
  • If you ask how `cin.get(Sentence2,SIZE,'$');` works, you can check out the [reference](https://en.cppreference.com/w/cpp/io/basic_istream/get). Might be a bit hard to read as beginner, but it's worth to try understanding it. – Lukas-T Feb 26 '21 at 11:48
  • 1
    This doesn't address the question, but that's rather poorly written code, so you don't need to spend much time trying to understand it. – Pete Becker Feb 26 '21 at 13:14
  • I agree with @PeteBecker, this is very poorly written code. My favorite part is asking for a sentence but only allowing for 3 characters. What kind of sentence is that? Although even if the size was fixed or the code was updated to use `std::string` the `cin >> Sentence;` would read the first word not a sentence anyways. – drescherjm Feb 26 '21 at 15:20
  • Trying to put 5 characters into an array that can hold 4 is undefined behavior: [https://stackoverflow.com/questions/11455302/where-c-really-stores-a-string-if-the-char-array-that-stores-it-is-smaller-tha](https://stackoverflow.com/questions/11455302/where-c-really-stores-a-string-if-the-char-array-that-stores-it-is-smaller-tha) – drescherjm Feb 26 '21 at 15:44

1 Answers1

0
cout << "Enter the second sentence: "<<endl;

Prints Enter the second sentence: to the console.

cin.get(Sentence2,SIZE,'$');

Reads up to SIZE - 1 number of characters, or until the first $ character from console to Sentence2 array (including space characters. 1 character reserved for the zero terminator).

cout << Sentence2 <<endl;

Prints Sentence2 variable to console.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • I can't understand that here we haven't input any Sentence2 so how can it work. Do u understand what I mean – Ash Ketch Feb 26 '21 at 11:57
  • @AshKetch the input is like a pipe. You enter "This is my first sentence" and this stays there until it is fully extracted. Firstly `cin >> Sentence;` reads only the first word, but the rest stays there. When you do `cin.get` it continues to read the rest of the characters from the pipe. – Aykhan Hagverdili Feb 26 '21 at 12:08
  • 1
    Ohh Okay I understood. Thank you for explaining . – Ash Ketch Feb 26 '21 at 12:13