By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

Why does it show the last data 2 times ?

P: 3
#include <iostream>
using namespace std;

#include <fstream>

struct student
{
char name[40];
int id;
};

int main ()
{
int menu;
student std;
do
{
cout << "\n ========================================" << endl;
cout << " Welcome To Student Record Program " << endl;
cout << " ========================================" << endl;
cout << "\n 1. Show A Student Info " << endl;
cout << " 2. Insert A New Student Record " << endl;
cout << " 3. Exit Program " << endl;
cout << "\n Enter Your Choice : ";
cin >> menu;
cin.ignore(80,'\n');

if (menu <1 || menu >3) cout << "\n Please Enter The Correct Choice" <<endl;

else if (menu == 1)
{
ifstream stdfile("student", ios::in | ios::binary);
if (stdfile.is_open())
{
cout << "\n\n ========================================"<<endl;
cout << " The Student Record "<< endl;
cout << " ========================================\n"<<endl;
while (!stdfile.eof())
{
stdfile.read((char*)(&std), sizeof(student));
cout << " " << std.id << "\t" << std.name << endl;
}
stdfile.close();
}
else
{
cout << "\n\n ========================================"<<endl;
cout << " No Data Record "<< endl;
cout << " ========================================\n"<<endl;
}
system("PAUSE");
}

else if (menu == 2)
{ cout << "\n\n ========================================"<<endl;
cout << " Enter A New Student Record "<< endl;
cout << " ========================================\n"<<endl;
cout << " Enter Student ID number: ";
(cin >> std.id).get();
cout << " Enter Student Name: ";
cin.getline(std.name,40);
ofstream rstdfile("student", ios::out | ios::app | ios::binary);
rstdfile.write((char*)(&std), sizeof(student));
rstdfile.close();
}
} while (menu != 3);
system("PAUSE");
return 0;
}




========================================
The Student Record
========================================

123 abc
456 def
789 ghi
789 ghi
Press any key to continue . . .
Jun 19 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.