I don't know much about how to restart the program, you can use system(char*) of <stdio.h> but people say it wrong to do so (there is an article on this website in the articles section which lists out the reasons).
There are other ways using Windows API which I know of. I'll only give the lengthy source code if you are interested and have windows.
cin>> interprets any type of whitespace and the Enter key as seperators. It will take input only till it encounters a space or an enter.
What happens is that when you hit enter, cin stores everything you entered in a memory location called the input buffer. Then it starts reading the input buffer and stops when it encounters '\n', or any whitespace character (like space). It translates the input into a double, int or any other data type if you are using cin>> with that data type. If it is a string, it just copies what it has read into your string. It then deletes the part that it has just read.
If you didn't enter a whitespace, it will delete everything except the '\n'.
Otherwise, it will delete everything before the first whitespace and everything else will remain in the output buffer. When you use cin the next time, instead of taking input from you, it will continue reading leftover characters from the input buffer.
So a possible workaround is to use
cin.getline(char* a,int size,char delim='\n')
. For eg, if you want to take input into a c-string, this will work.
1 2
|
char a[30];
cin.getline(a,30);
|
This will take input till it encounters a '\n' or it has read 30 characters.
But there is a catch. getline will remove the '\n' from the input buffer, cin>> wouldn't. cin removes it before reading.
As cin leaves behind a '\n', using getline after cin will cause getline to read the '\n', remove it from the input buffer and then stop. It wouldn't read after that.
So to be able to use getline after a cin, you have to check the first character in the input buffer and remove it if it is a '\n'. This is done like this:
if(cin.peek()=='\n')cin.ignore();
Just after this, you can use getline
For more information on these functions, you can refer to the Reference section of this website. Look in the IOstream library under istream class