Code
#include<iostream>
int main()
{
char s[20];
char ch;
std::cin.getline(s,10);
std::cout<<"x"<<s<<"x"<<"\n";
std::cin>>ch;
std::cout<<"x"<<ch<<"x";
std::cin>>ch;
std::cout<<"x"<<ch<<"x";
std::cin>>ch;
std::cout<<"x"<<ch<<"x";
}
Output-1
bonaparte // I entered these 9 characters as input
xbonapartex
a // input
xax
b // input
xbx
c // input
xcx
Output-2
bonaparte2 // I entered these 10 characters as input
xbonapartex
x x
x x
x x
I just entered one extra letter means total 10 char through keyboard bonaparte2
compared to 1st output.
According to me getline is going to extract 9 characters and add '\0' and store in string s
. So stream remains with characters 2
and \n
. so first cin>>ch
should take 2
and print x2x
.
Now 2nd cin>>ch
should ignores \n
as it is leading whitespace character but none of this is happening. Program is not asking from input from keyboard.