Last edited on
What's the problem? A description of how program behaves and what goes wrong would be helpful
I didn't run the code but I guess that you can't write multiple records into file. You are overwriting old record because you do not set file pointer to the end of the file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void add_rec(struct student st[],int& itemcount){
   
    ofstream file;
    file.open("Students.txt,ios::app"); // ios::app - write everything at the end of file
    
    
again:
    cout<<"\nEnter student's ID:";
    cin>>st[itemcount].stnumber;
    if(search(st,st[itemcount].stnumber,itemcount)!=-1){
        cout<<"This ID already exists\n";goto again;
    }
    
    cout<<"Enter student's Name:";
    cin>>st[itemcount].stname;
    cout<<"Enter student's Rate of Math score:";
        cin>>st[itemcount].rateOfMath;
    cout<<"Enter student's Rate of Physics score:";cin>>st[itemcount].rateOfPhysc;
    st[itemcount].total=st[itemcount].rateOfMath+st[itemcount].rateOfPhysc;
     ++itemcount;
    
    file << st[itemcount].stnumber <<setw(3)<<setw(3)<<st[itemcount].stname<<setw(3)<<st[itemcount].rateOfMath<<setw(3)<<st[itemcount].rateOfPhysc<<endl;
    
   file.close();

    
}
Last edited on
Topic archived. No new replies allowed.