My doubt is how to read a string in next line followed by an integer like as follows
8
111111110001
we have to read from keyboard 8 as integer and then after pressing enter we have to read 111111110001 as a string .
Below is my code , when i gave input as 8 111111110001 (separated by space) , it works properly but when i press enter , program is not reading string.
Thanks in advance .
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int n;
string s;
cin >> n;
getline(cin,s);
return 1;
}
After the user types the integer, the enter key is pressed. All those characters, including the newline character '\n' are stored in the input buffer. After cin >> n that newline still remains in the buffer. We can remove it using cin.ignore().
what is the purpose of 1000 in the param of cin.ignore?
1000 was an arbitrary number I chose. it means, ignore up to 1000 characters from the input stream, or until the delimiter is found, whichever comes first.