Doubt in the code of this question

my doubt is what is the role of “ch = cin.get()” at the last of the while loop.
and how is the loop seeing the next character bit after everytime the loop works. If ch = cin.get() is doing it then please explain how

You take the first character input outside the loop, you need to process it, after that you will take the next character. This is why it is logical to have cin.get() at the end.

The loop knows the character because we have already the stored the next character in ch.

Alternatively you can write something like this.

while(true){
cin>>ch; (or you can do ch = cin.get())
if(ch == ‘\n’) break;
otherwise process the character.

}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.