#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

Show us what you typed, and what you got back.

Sorry for the late reply :)
Using Case 1 : enter record

"The First Record"
1
Anna
12
Female
221B Baker Street
B positive
Dr.Shah
19000
TR

"The Second Record"
2
Daniel
15
Male
124A Blaine Street
A negative
Dr.Shah
19455

OP

After listing both the records using case 2:

1
Daniel
12
Female
124A Blaine Street
A negative
Dr.Shah
19000

TR

2
Daniel
15
Male
124A Blaine Street
A negative
Dr.Shah
19455
OP

As you can see in the listing of record the second record is replacing strings from first record.

Your data-record structure is NOT what you need here ...

Try something like this:

// bloodGroups.cpp //

#include <iostream>
#include <string>

#include <cstring> // re. strcpy
#include <cstdio> // re. FILE


const char* FNAME = "users.bin";

const int MAX_NAME_LEN = 39;
const int MAX_ADD_LEN = 99;
const int MAX_BG_LEN = 15;

const char* MENU =
    "====== STUDENT DATABASE MANAGEMENT SYSTEM ======\n\n"
    "1. Add    Records\n"
    "2. List   Record\n"
    "3. Search Record\n"
    "4. Modify Records\n"
    "5. Delete Records\n"
    "6. Exit   Program\n\n"
    "Select Your Choice :=> ";


struct Student
{
    int id;
    char name[MAX_NAME_LEN+1];
    int age;
    char gender; // 'm' or 'f'
    char address[MAX_ADD_LEN+1];
    char blood_group[MAX_BG_LEN+1]; // AB Rho Negative (or Positive)
    char doc_name[MAX_NAME_LEN+1];
    int amount;
    char op_tr; // 'o' for op or 't' for tr
} ;


// 2 utilities follow ... (to facilitate VALID data entry) //

template< typename T >
T takeIn( const std::string& msg,
          const std::string& errMsg = "\nError! Try again ...\n\n" )
{
    T val;
    while( true )
    {
        std::cout << msg << std::flush;
        if( std::cin >> val && std::cin.get() == '\n' )
            break;
        else
        {
            std::cout << errMsg;
            std::cin.clear(); // clear all error flags
            std::cin.sync(); // 'flush' cin stream
        }
    }
    return val;
}
// return a (Non-empty) C string ... with up to maxLen char's ...
const char* takeInLineMaxLen( const std::string& msg, int maxLen,
                              bool okEmpty = false )
{
    std::string val;
    while( true )
    {
        std::cout << msg << std::flush;
        getline( std::cin, val );
        int len = val.size();
        if( len )
        {
            if( len <= maxLen ) break;
            // else
            std::cout << "\nMax allowed length is "
                      << maxLen << " char ... Try again ...\n\n";
        }
        else if( okEmpty ) // case of blank lines accepted as input //
             break;
        else
            std::cout << "\nBlank line input is NOT valid here ...\n\n";
    }
    return val.c_str();
}


void takeIn( Student& e )
{
    e.id = takeIn< int >( "\nEnter ID: " );
    strcpy( e.name, takeInLineMaxLen( "Enter Name: ", MAX_NAME_LEN ) );
    e.age = takeIn< int >( "Enter Age: " );
    e.gender = takeInLineMaxLen( "Enter Gender (m/f): ", 1 )[0];
    strcpy( e.blood_group, takeInLineMaxLen( "Enter Blood Group: ", MAX_BG_LEN ) );
    strcpy( e.address, takeInLineMaxLen( "Enter Address: ", MAX_ADD_LEN ) );
    strcpy( e.doc_name, takeInLineMaxLen( "Enter Doctor Name: ",  MAX_NAME_LEN ) );
    e.amount = takeIn< int >( "Enter (Integer) Amount Paid: " );
    e.op_tr = takeInLineMaxLen( "Enter Operation/Treatment (o/t): ", 1 )[0];
}

void print( const Student& e )
{
    using std::cout; using std::endl; using std::string;

    cout << '\n'
         << e.id << '\n'
         << e.name << '\n';

    if( e.gender == 'm' ) cout << "male\n";
    else if( e.gender == 'f' ) cout << "female\n";

    cout << e.age <<  '\n'
         << e.address << '\n'
         << e.blood_group<< '\n'
         << e.amount << '\n';
    if( e.op_tr == 't' ) cout << "treatment";
    else if( e.op_tr == 'o' ) cout << "operation";
    else cout << e.op_tr;
    cout << '\n' << string( 9, '-' ) << '\n';
}

/*
"The First Record"
1
Anna
12
Female
221B Baker Street
O Rho Positive
Dr.Shah
19000
TR

"The Second Record"
2
Daniel
15
Male
124A Blaine Street
AB Rho Negative
Dr.Shah
19455
OP
*/



int main()
{
    using std::cerr; using std::cout;

    FILE* fp = fopen( FNAME, "rb+" );
    if( !fp )
    {
        fp = fopen( FNAME, "wb+" );
        if( !fp )
        {
            cerr << "Cannot open file " << FNAME << " so exiting now ... " ;
            return -1;
        }
    }


    bool done = false;
    while( !done )
    {
        Student stud;
        char another;

        switch( takeInLineMaxLen( MENU, 1 )[0] )
        {
            case '1': case 'a': case 'A':
                fseek( fp ,0, SEEK_END );
                another = 'Y';
                while( another == 'Y' || another == 'y' )
                {
                    takeIn( stud );
                    fwrite( &stud, sizeof(Student), 1, fp );
                    another = takeInLineMaxLen("\nAdd Another Record (y/n) ? ", 1 )[0];
                }
            break;

            case '2': case 'l': case 'L':
                rewind( fp );

                cout << "\n=== View the Records in the Database ===\n";
                while( fread( &stud, sizeof(Student), 1, fp ) == 1 )
                {
                    print( stud );
                    if( takeInLineMaxLen( "Show more (y/n) ? ", 1, true )[0] == 'n' )
                        break;
                }
            break;

            case '6': case 'e': case 'E':
                 done = true;
            break;

            default:
            cout << "\nNOT implemented here yet ...\n\n";
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.