main()
{ char ch;
ch=cin.get();
while(ch!=’!’)
{ cout<<ch;
ch=cin.get();
}
what is the work of the last cin.get()
Cin.get problem
Hello @guptaaman155 ,
Let’s understand the difference between cin and cin.get() first:
cin is an istream type object(variable) while cin.get() is invoking a function get() with it.
char A,B;
cin>>A;
B=cin.get()
In C++, >> is also a function. In both the cases, a function is getting invoked with cin object which takes a character from key board and returns the same. What ever they return, the same is assigned to variables A or B.
In your code, you are missing a } at the end.
Now, coming back to your query,
The last cin.get() is used to input a character from the user one by one until he/she enters “!”.
After receiving “!” the loop will terminate.
Hope, this would help.
Give a like if you are satisfied.
i simply say …why cin.get() used 2nd time in this program?
I have answered that also, if you read it carefully.
I repeat, The last cin.get() is used to input a character from the user one by one until he/she enters “!”.
After receiving “!” the loop will terminate.
this cin.get() statement is just used to input the character until the loop terminates.
Hope, this would help.