+ 1
Data file handling
i made this program but it goes to infinite loop whenever i try to input the second time (this program doesnt work on SL compiler so i am using cxxdroid). i am not able to figure out my mistake plz help me https://code.sololearn.com/cAWWSWiApu21/?ref=app
3 Answers
+ 1
The last read operation in the first iteration of the loop is cin>>c. Now cin maintains an input buffer for all read operations and then copies data from this buffer. But this operation leaves a newline in the buffer, which is then interpreted by the cin.getline call, and the output is skipped.
To remove the issue, add this after cin>>c:
cin.ignore(numeric_limits<streamsize>(cin),'\n');
And include the header, <limits>
What this does is remove all characters till a newline, and after removing the newline, it stops.
+ 1
Saurabh Tiwari The numeric_limits part is just to specify the size of the cin's buffer. You can use a smaller number, or even 1 for this case.
- 1
Kinshuk Vasisht thnx for the help
can you provide any alternative way of this as i am not familiar with limits