0

I was going through a code in my school textbook, wherein there is a line who's function is to clear the input buffer (mentioned as a comment in the code).

I couldn't quite understand its purpose. It is definitely required as its removal messes up the console input process.

Please explain what its function is, and what is happening when I remove it.

I have also tried using cin.ignore(); and it works just fine too. How is the function used here, is it an exact replacement of cin.ignore()?

P.S. In school we are using the older version of C++. Hence the ".h" extension, clrscr();, etc.

#include <iostream.h>
#include <fstream.h>
#include <conio.h>

void main(){
clrscr();

ofstream fout("student.txt", ios::out);
char name[30], ch;
float marks = 0.0;

for(int i = 0; i < 5; i++){
    cout << "Student " << (i+1) << ":\tName: ";
    cin.get(name,30);
    cout << "\t\tMarks: ";
    cin >> marks;
    cin.get(ch);  //for clearing input buffer (This thing!)
    fout << name << '\n' << marks << '\n';
}

fout.close();

ifstream fin("student.txt", ios::in);
fin.seekg(0);
cout << "\n";

for(int i = 0; i < 5; i++){
    fin.get(name,30);
    fin.get(ch); //Again
    fin >> marks;
    fin.get(ch); //Same
    cout << "Student Name: " << name;
    cout << "\tMarks: " << marks << "\n";
}

fin.close();
getch();

}
Julian Declercq
  • 1,356
  • 2
  • 14
  • 29
DashwoodIce9
  • 115
  • 6

2 Answers2

1

I just want to start with pointing out that you are not using "the older version of C++". You are just using some c code (like clrscr()) and c styled programming, and indeed probably not cpp11 or cpp14. Also header files for C++ are often still just .h.

Now the answer to your question:

cin.get(ch);  

The '\n' character gets read into ch. '\n' is one character, a newline character.

does indeed do the same thing as

cin.ignore();

It is used to clear the input buffer, which means that the empty end of line ('\n') is being deleted from the input buffer.

Why is this done you ask? This is a good example of why you would do that. Hope this helped you!

Julian Declercq
  • 1,356
  • 2
  • 14
  • 29
  • 1
    No, he is running the "older" version of C++. Most likely he is using turbo C++ which runs a pre-standard/non-standard version of C++. – NathanOliver Jul 21 '16 at 19:44
  • @NathanOliver My bad, sorry. Thanks for pointing that out. You have to admit that "THE older version of C++" sounds pretty weird. I thought he meant "not cpp11 or cpp14". – Julian Declercq Jul 21 '16 at 19:45
  • I am a beginner when it comes to programming. Yes I use Turbo C++, I thought that it was the older version, my bad. Anyways, shouldn't cin.get(ch) be taking a character input from user and pass it to "ch"? – DashwoodIce9 Jul 21 '16 at 19:52
  • @PrakharAgarwal Good point, I updated my answer to clarify it for you ;) – Julian Declercq Jul 21 '16 at 20:16
  • @JulianDeclercq Okay but why is the newline character being passed into 'ch' automatically? I mean, shouldn't the console take separate inputs for 'marks' and 'ch'? – DashwoodIce9 Jul 21 '16 at 20:55
1
cin >> marks;
cin.get(ch);  //for clearing input buffer (This thing!)

This is a not-so-robust way of clearing the input buffer. If you type a number followed by Enter, the first line will consume the number and put the value in marks while the second line will read the newline character and discard it.

It is not robust since it does not account for spaces a user might have entered after the number. A more robust method would be to use istream::ignore.

cin >> marks;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
R Sahu
  • 196,807
  • 13
  • 136
  • 247