Why my code is not taking 'n' inputs?

Hello,

My program is not taking ‘n’ inputs, instead, it is taking ‘n-1’ inputs.

I tried to take array of strings.

Below is my code:

#include’<‘algorithm’>

#include’ <‘string’>

#include’ <‘algorithm’>

using namespace std;

int main(){

    int n;
    cin>>n;

    string s[n];

    for(int i=0;i<n;i++){
        getline(cin,s[i]);
    }

    sort(s,s+n);

    for(int i=0;i<n;i++){
         cout<<s[i]<<endl;
    } 

    return 0;

}

Input :

3

apple juice

mango juice

// It is not taking 3rd input

Output:

Apple juice

mango juice

Maybe because I have not put cin.get() here. But, what is the use of cin.get() in this context?

hi @siddharth_1_3277
welcome to coding blocks community

yes you are right
this happen because you have not use cin.get();

n the video we write cin.get(); after taking n
this is because
after n there will be a newline (this is come when you press enter and remain in input stream because cin ignore newline) so we have to remove it from input stream that’s why we write cin.get();

otherwise getline read an empty new line

and you think it is taking only n-1 input but actuall it is taking n inputs one is empty
i hope you understand now

i hope this help

if you have more doubts regarding this feel free to ask
if your doubt is resolved mark it as resolved from your doubt section inside your course

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.