@Volatile Pulse
No, it shouldnt. It is supposed to sync the buffers, which means anything left in the buffer after the user has inputted, should be erased. try running these two codes. Enter more than 1 word into each of them. You will notice they react differently.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream>
#include <string>
using namespace std;
int main()
{
string line;
cout<< "Name: "<< endl;
cin>> line;
cin.sync();
cout<< "Date"<< endl;
cin>> line;
cin.sync();
cout<< "Whatever..."<< endl;
cin>> line;
cin.sync();
return 0;
}
|
Whithout Sync:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
#include <string>
using namespace std;
int main()
{
string line;
cout<< "Name: "<< endl;
cin>> line;
cout<< "Date"<< endl;
cin>> line;
cout<< "Whatever..."<< endl;
cin>> line;
return 0;
}
|
Compile and run those. Enter somthing like "Marry went to the market" into both compiled programs. The 2nd one will skip the next two inputs, while the first will wait for each.
What cin does is get only 1 peacie of data. Using cin, you only get 1 string at a time. This means it will recognize spaces as a sparator for each string. This is the same for any piece of data (whether it be integers, floats, or strings). Because of this, we should use
getline(cin, variable)
to get a string of an entire line. cin input can be useful in other ways. A good example is if you have a database full of names. All you have to do to get each one is put it into a file, separate them with spaces (naturally), and whenever you need them just
ifstream in; in>> strvar
until the end of the file.
Because we sync the istream after getting input with cin, whatever data is still in the buffers is deleted to achieve the sync. Therefore, then we ask for input again using cin, there is nothing in the buffer to get and so it prompts the user.