vendredi 24 mai 2019

How to read a line into a char array when the line is longer than the array?

I tried to read a line into a char array with this:

char arr[3];
char arr2[3];
cin.getline(arr,3);
cout<<arr<<endl;
cin.getline(arr2,3);
cout<<arr2<<endl;

I entered something with 4 letters and the program yielded only the first 2 characters of arr without giving me a chance to input arr2,how can I avoid this from happening?

Aucun commentaire:

Enregistrer un commentaire