Why this code is not working

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    char ch;
    ch = getchar();
    string s;
    cin >> s;
    cout << ch <<"\n" << s;
    return 0;
}

h hello    // not working after pressing 'enter'. Has to give input again.

h
hello     // works only if 'enter' is pressed before giving input 'hello'.

h hello      // works without  "ios_base::sync_with_stdio(false);"

HELP TO SOLVE THIS ISSUE!!

h hello          //  I want this to WORK!!!

Please Format your code and also provide a link to the question in the description. :slightly_smiling_face:

Now answer…

remove getchar()

1 Like

i need getchar() to enter a character first.
it’s mandatory.

Then simply do

char ch;
string s;
cin>>ch>>s;
cout<<ch<<" "<<s;

You can use string for both of them.A single character is also a string.

string s1,s2;
cin>>s1>>s2;
cout<<s1<<" "<<s2;

Actually getchar() is essential before cin in my code.
So suggest a way to implement the original code.
And if i remove ios_base::sync_with_stdio(false), my code works perfectly. Can i know why so ?