Reading a list of strings

why was cin.get() used before the loop started

When using cin, you type in characters (keys on your keyboard) that appear in the command prompt. That’s what your program - specifically, cin reads. Normally, it will automatically take as many characters as it needs to input. For instance, if you say

cin >> myIntVar;

and you type 37643, it gets all 5 characters, treats them as an integer, and correctly puts 37643 into myIntVar.

By using cin.get(), you get only one of those characters, and it is treated as a char.

Sometimes, the command prompt will close as soon as the program finishes, meaning you can’t see the output. Putting cin.get() forces the program to wait for the user to enter a key before it can close, and you can see the output of your program.

Its not important if are using an ide but u may want to use if u are using terminal

u can refer this discussion The use of function cin.get()
In case of any doubt feel free to ask :slight_smile:
If u got the answer then mark your doubt as resolved

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.