-4

This is a problem relating to file handling where we input data to a file and then display its contents. I am unable to understand how the line cin.get(ch); helps in the output. I found that if I remove this line from the code, the program is unable to take the input for the variable marks in the second iteration. Why is it so ? I am a bit confused in the working of get() here. My code:

 #include<iostream>
 #include<conio>
 #include<fstream>
 int main()
 {

  ofstream fout;
  fout.open("student",ios::out);
  char name[30],ch;
  float marks=0.0;
  for(int i=0;i<5;i++)
  {
   cout<<"Student "<<(i+1)<<":\t Name:";
   cin.get(name,30);
   cout<<"\t\t Marks: ";
   cin>>marks;
   cin.get(ch); **// the confusion is in this line**
   fout<<name<<"\n"<<marks<<"\n";
  }
  fout.close();
  ifstream fin;
  fin.open("student",ios::in);
  fin.seekg(0);
  cout<<"\n";
  for(i=0;i<5;i++)
  {
   fin.get(name,30);
   fin.get(ch);
   fin>>marks;
   fin.get(ch);
   cout<<"Student Name: "<<name;
   cout<<"\t Marks: "<<marks<<"\n";
  }
  fin.close();
  return 0;
}
scohe001
  • 15,110
  • 2
  • 31
  • 51
  • Maybe there to read the newline that is in the input buffer after the previous input? – Some programmer dude Aug 17 '17 at 17:08
  • How does the input look like? – user0042 Aug 17 '17 at 17:08
  • use a `std::string` and then read from the stream as normal `cin >> name`. What's the purpose of `cin.get(ch)` anyway? Please provide input and expected output. – AndyG Aug 17 '17 at 17:08
  • 1
    @AndyG That's part of his question, that he doesnt understand why `cin.get(ch)` is needed/used/ – Javia1492 Aug 17 '17 at 17:10
  • If you mix cin.get() and cin >> the program will skip cin.get's placed after a cin >>. Since '\n' is being detected by cin.get()s. Edit: You need to add cin.ignore() to related places to make it work. Also to learn what cin.get is for show your effort and do research. – Tuğberk Kaan Duman Aug 17 '17 at 17:11
  • @Javia1492: OP said it was their code. That they don't understand what one of the commands does it is something that can be looked up in the documentation. It also raises other questions like "why did you write it in the first place?" In addition to "what is your input and expected output?' As stated, the question is currently not answerable. – AndyG Aug 17 '17 at 17:13
  • Sorry @Javia1492 i mean its a code from a textbook. Added the inputs – Bhargab Kakati Aug 17 '17 at 17:20

1 Answers1

1

From the description of the function

basic_istream<charT,traits>& get(char_type* s, streamsize n,
char_type delim );

in the C++ Standard:

Characters are extracted and stored until any of the following occurs: ...

— traits::eq(c, delim) for the next available input character c (in which case c is not extracted).

And from the description of the function

basic_istream<charT,traits>& get(char_type* s, streamsize n);

Effects: Calls get(s,n,widen(’\n’))

Thus this call

cin.get(ch); 

serves to extract the new line character from the input buffer. Otherwise a next call of the function

cin.get(name,30);

will read an empty string.

Instead of the call

cin.get(ch); 

you could use a call of the member function ignore

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335