is there any difference between
cin >> var
and
getline (cin , var )
?
Yes, there is a difference between cin >> var and getline(cin, var).
cin >> var reads input from the standard input stream (cin) into the variable var until it encounters a whitespace character (such as a space or a newline). This means that if the user inputs a string with spaces, only the first word will be stored in var, and the rest will be left in the input buffer.
getline(cin, var), on the other hand, reads input from the standard input stream (cin) into the variable var until it encounters a newline character (\n). This means that getline will read an entire line of input, including any spaces, and store it in var.
Here's an example to illustrate the difference:
// User input: "Hello world"
string var1, var2;
cin >> var1; // var1 contains "Hello"
getline(cin, var2); // var2 contains " world"
As you can see, cin >> var1 only reads the first word ("Hello"), while getline(cin, var2) reads the rest of the line, including the space character, and stores it in var2.