#include<iostream>
#include<fstream>
#include<stdio.h>
usingnamespace std;
class student{
private:
int roll;
float marks[5];
char nam[34];
public:
void getData(int stud){
cout<<"Please enter the name of student "<<stud<<"\n";
//cin.getline(nam,34);
gets(nam);
cout<<"Please enter your roll number for student "<<stud<<"\n";
cin>>roll;
cout<<"Please enter your marks for student "<<stud<<" in ";
for(int i=0; i<5; i++){
cout<<"subject "<<i+1;
cout<<endl;
cin>>marks[i];
}
}
};
int main(){
//cout<<"Hello World\n";
ofstream of("file.txt");
student s[5];
cout<<"Please enter the data of the first five students\n";
int loop=0;
while(loop<5){
s[loop].getData(loop+1);
of.write((char*)&s[loop],sizeof(student));
loop++;
}
of.close();
cout<<"operation successfful\n";
return 0;
}
yup, that is my code.
I am actually getting a very strange logical error..
It works fine till I get the first output. But then it kinda skips all the cin statements and executes abruptly to the next output, till the loop is completed. (Run it yourself, you will understand.)
Please help me, I am unable to figure out the problem
warning: ‘char* gets(char*)’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
warning: the `gets' function is dangerous and should not be used.