Please help me in LOSTMAX

I’ve been doing this problem

https://www.codechef.com/problems/LOSTMAX

In which for taking input from user in C++ I wrote the code

do
 {
 cin>>n>>s;
 }while(s!='\n');

with which I am not able to take inputs but if I changed it to

do
 {
  scanf("%d""%ch",&n,&s);
 }while(s!='\n'); 

it worked perfectly.

Can somebody explain Please.
Thanks.

PS. Also tell me how to format code in answers so it comes in grey box.

Thats because cin>>s will ignore the newline character and automatically goto next line and take input of next line (which will cause WA for you).

for this kind of input, you should use getline(cin,s). Then you can use stringstream to extract int, string etc.

you can use getchar()

do
{
cin>>n;
s=getchar();
}while(s!=’\n’);

Formatted code.