#include <iostream>
#include <cstdio>
#include <conio.h>
using namespace std;
main()
{
FILE *fp, *ft;
char another, choice;
struct student
{
int id;
string name;
int age;
char gender[7];
string address;
string blood_group;
string doc_name;
int amount;
char op_tr[1];
};
struct student e;
int xid;
int recsize;
int ifound;
fp=fopen("users.txt","rb+");
if (fp == NULL)
{
fp = fopen("users.txt","wb+");
if (fp==NULL)
{
puts("Cannot open file");
return 0;
}
}
recsize = sizeof(e);
while(1)
{
system("cls");
cout << "\t\t====== STUDENT DATABASE MANAGEMENT SYSTEM ======";
cout <<"\n\n ";
cout << "\n\n";
cout << "\n \t\t\t 1. Add Records";
cout << "\n \t\t\t 2. List Records";
cout << "\n \t\t\t 3. Search Record";
cout << "\n \t\t\t 4. Modify Records";
cout << "\n \t\t\t 5. Delete Records";
cout << "\n \t\t\t 6. Exit Program";
cout << "\n\n";
cout << "\t\t\t Select Your Choice :=> ";
fflush(stdin);
choice = getche();
switch(choice)
{
case '1' :
fseek(fp,0,SEEK_END);
another ='Y';
while(another == 'Y' || another == 'y')
{
cout << "\nEnter ID : ";
cin >> e.id;
cin.ignore();
cout <<"\nEnter Name: ";
getline(cin,e.name);
cout <<"\nEnter Age: ";
cin>>e.age;
cin.ignore();
cout <<"\nEnter Blood Group: ";
getline(cin,e.blood_group);
cin.ignore();
cout <<"\nEnter Address: ";
getline(cin,e.address);
cin.ignore();
cout <<"\nEnter Doctor Name: ";
getline(cin,e.doc_name);
cout <<"\nEnter Amount Paid: ";
cin>>e.amount;
cout <<"\nEnter Operation/Treatment: ";
cin>>e.op_tr;
fwrite(&e,recsize,1,fp);
cout << "\n Add Another Record (Y/N) ";
fflush(stdin);
another = getchar();
}
break;
case '2':
system("cls");
rewind(fp);
cout << "=== View the Records in the Database ===";
cout << "\n";
while (fread(&e,recsize,1,fp) == 1)
{
cout << "\n\n";
cout << e.id << endl;
cout << e.name << endl;
cout << e.age << endl;
cout << e.address << endl;
cout << e.blood_group<< endl;
cout << e.amount << endl;
cout << e.op_tr << endl;
cout << "\n ================================";
}
cout << "\n\n";
system("pause");
break;
default:
cout <<"BLAA"<<endl;
}
The problem is that the output from case 2 is not correct all the strings are same as the third record entered why is this happening ?
Reply ASAP
Thanks