Anomaly of successive use of cin for char arrays
Successive use of cin will pose some difficulty when used with char arrays or c-style strings.
Learning Objectives
- Anomaly of cin when used one after the another for collecting string or character arrays inputs.
Source Code
|
Run Output
Code Understanding
char c1[50]; char c2[50];
Two character arrays have been declared here.
cout<<“Input a name: “; cin>>c1;
cout<<“Input another name: “; cin>>c2;
Successive use of cin for two character arrays is being done. If only one word is entered for first one then second input will be taken properly. If more than one word is entered for first input then second input will not be collected as demonstrated in the run output of second case above.
Notes
Solution of this anomaly shall be discussed in the topic – String handling – Null Terminated.
Suggested Filename(s): cinanomaly.cpp, cinchararr2.cpp
CSKC| Created: 15-Dec-2017 | Updated: 5-Sep-2018|