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;
}