I need the solution of this program using C++ ..
Develop a database software to maintain the records of students of a college.The software should be user-friendly.The user can easiy add new records,update existing records,create reports of existing records etc.

Recommended Answers

All 20 Replies

You won't find it here, Daniweb is not a homework service. We'll be happy to help you with specific problems in your code, but we won't do your work for you.

Aah OK..but can u please guide me to solve this ...im working on it ..i just need some useful tips how to manage its code...hope u'll help me ..

Once again, please ask a specific question if you want a specific answer. Managing code seems pretty specific to me, so that shouldn't be a problem for you.

Describe your approach, show your code (even pseudo code), and where you are running into roadblocks. This will indicate an honest effort on your part to do the work. We won't do your work for you, but we will critique your work and try to point you in appropriate directions.

I made this code in very simple way ..all cases are working properly but the case 2 (read data) is executing infinitely ...can u tell where the problem is?

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
struct student
{
 int id;
 char name[20];
 int marks;
 };

void main()
{
 student std;
 clrscr();
 fstream xyz("abc.txt",ios::in|ios::out);
 if(!xyz)
 {
 cerr<<"file not found"<<endl;
 exit(1);
 }
 int choice;

cout<<"enter your choice from the following:"<<endl;
cout<<"1. write data" <<endl;
cout<<"2. read data" <<endl;
cout<<"3. update data" <<endl;
cout<<"4. delete data" <<endl;
cin>>choice;

//switch statement begins
switch(choice)
{
//write data
case 1:
int a=1;
while(a<=3)
{
cout<<"record of student #:"<<a<<endl;
cout<<"enter id: ";
cin>>std.id;
cout<<"enter name: ";
cin>>std.name;
cout<<"enter marks: ";
cin>>std.marks;
xyz<<std.id<<std.name<<std.marks;
a++;

}
break;

//read data
case 2:
int b=1;
while(xyz)
{
xyz>>std.id>>std.name>>std.marks;
cout<<"record no is: "<<b<<endl;
cout<<"id is: "<<std.id<<endl;
cout<<"name is: "<<std.name<<endl;
cout<<"marks are: "<<std.marks;
b++;
}
break;

//update data
case 3:
int r;
cout<<"enter record no: " ;
cin>>r;
xyz.seekp((r-1)*sizeof(std));
if(!xyz.eof())
{
cout<<"enter id: ";
cin>>std.id;
cout<<"enter name: ";
cin>>std.name;
cout<<"enter marks: ";
cin>>std.marks;
xyz.write((char*) &std,sizeof(std));
}
else
cout<<"record not found"<<endl;
break;

//delete data
case 4:
int s;
cout<<"enter record to delete ";
cin>>s;
xyz.seekg((s-1)*sizeof(std));
xyz.read((char*) &std,sizeof(std));
if(s!=0)
{
 student std2;
 xyz.seekp((s-1)*sizeof(std2));
 xyz.write((char*) &std2,sizeof(std));
cout<<"account no "<<s<<" is deleted."<<endl;
}
else
 cerr<<"account no " <<s<< " is empty."<<endl;
 break;

 //exit function
 case 5:
 {
 exit(1);}
 break;

//default case
default:
{
cerr<<"incorrect choice"<<endl;
 }
 cout<<"enter 5 to end"<<endl;

 getch();
}}

whoa that was interesting to make this code on my own .Well, i enjoyed my work .
if there is any mistake please notify it.

Let's see, old style headers with extensions, <conio.h>, void main() - let me guess, your professor is using Turbo C++ as your compiler?

/me checks OP's locations Ah, Pakistan, that explains that. This comes up a lot, and the answer is that there's nothing to be done about it. Pity, there's really no chance of you being allowed to use something more modern there, as the university system in Pakistan (like that in India) standardized on Turbo C++ decades ago and won't budge an inch on the subject. It's unfortunate, as it means you are being taught a version of C++ that is nearly twenty years out of date, and using a compiler so old that you can't even install it on newer PCs without using a DOS emulator, but as long as you are going to class at a university in your country, there simply isn't any alternative.

The first piece of advice is to be more careful with indenting your code. Proper indentation makes a world of difference in how readable the code is. The specific indent style is less important than being consistent about it; the whole point is to make the different parts of it stand out.

The other thing is that, no matter what the compiler accepts, and no matter what your professor may have told you, void main() is not valid C++. In C++, the main() function must always return an int, either zero when the program succeeds or an error code when it doesn't. You must declare int main() and return a value at the end.

You must declare int main() and return a value at the end.

Yes to the first part, no to the second. If there's not a return statement in main, 0 will be returned automagically. It's a stylistic choice whether to include a (redundant) return statement for consistency with non-magic functions.

Of course, if the compiler is Turbo C++ then that's all irrelevant since the compiler doesn't sufficiently conform to any C++ standard.

yes I'm using turbo c++ as my compiler ..
i will install modern compiler as stated by you ,but respectfully you don't have any right to notify of which country I belong to and what are our professors teaching us?...i just needed your help and you did it .thank you ..!!

commented: Very rude and uncalled for and inappropriate -2

respectfully you don't have any right to notify of which country I belong to and what are our professors teaching us?

I apologize for overstepping on this matter, then. Your location is posted in your member profile, but you are correct, it was inappropriate for me to point it out.

As for what your professors are teaching, that was meant as a warning to you that what you are being taught is at least partially invalid, and to be aware that your coursework is likely to give you little traction in modern programming.

Yeah i know this is very initial stage of programming that we are using turbo ..
We will use latest versions of compilers in the preceeding semesters . Once again thank you very much..

So, does your code work? If not, what errors are you getting? We can help you more now, and we can appreciate your difficulties using out-of-date tools. We will try to help you work around those. You have made a good first step.

My read code (case 2) isn't still working. If u please do me a favour ,run this code on your compiler (if u have) ...and check what is the error ..here i have done some changes in my code.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
struct student
{
 int id;
 char name[20];
 int marks;
 };

void main()
{
 student std;
 clrscr();
 fstream xyz("abc.txt",ios::in|ios::out);
 if(!xyz)
 {
 cerr<<"file not found"<<endl;
 exit(1);
 }
 int choice;

cout<<"enter your choice from the following:"<<endl;
cout<<"1. write data" <<endl;
cout<<"2. read data" <<endl;
cout<<"3. update data" <<endl;
cout<<"4. delete data" <<endl;
cin>>choice;
while(choice!=-1)
{
//switch statement begins
switch(choice)
{
//write data
case 1:
int a=1;
while(a<=3)
{
cout<<"record of student #:"<<a<<endl;
cout<<"enter id: ";
cin>>std.id;
cout<<"enter name: ";
cin>>std.name;
cout<<"enter marks: ";
cin>>std.marks;
xyz<<std.id<<std.name<<std.marks;
a++;

}
break;

//read data
case 2:
int b=1;
while(xyz)
{
if(!xyz.eof())
{
xyz>>std.id>>std.name>>std.marks;
cout<<"record no is: "<<b<<endl;
cout<<"id is: "<<std.id<<endl;
cout<<"name is: "<<std.name<<endl;
cout<<"marks are: "<<std.marks;
b++;
}}
break;

//update data
case 3:
int r;
cout<<"enter record no: " ;
cin>>r;
xyz.seekp((r-1)*sizeof(std));
if(!xyz.eof())
{
cout<<"enter id: ";
cin>>std.id;
cout<<"enter name: ";
cin>>std.name;
cout<<"enter marks: ";
cin>>std.marks;
xyz.write((char*) &std,sizeof(std));
}
else
cout<<"record not found"<<endl;
break;

//delete data
case 4:
int s;
cout<<"enter record to delete ";
cin>>s;
xyz.seekg((s-1)*sizeof(std));
xyz.read((char*) &std,sizeof(std));
if(s!=0)
{
 student std2;
 xyz.seekp((s-1)*sizeof(std2));
 xyz.write((char*) &std2,sizeof(std));
cout<<"account no "<<s<<" is deleted."<<endl;
}
else
 cerr<<"account no " <<s<< " is empty."<<endl;
 break;

//default case
default:
{
cerr<<"incorrect choice"<<endl;
 }
}
 cout<<"enter -1 to end"<<endl;
 cin>>choice;
}
getch();
}

Your not checking for when the file is at the end properly. Something like this should work:

case 2:
int b=1;
//When the stream can't read any more data it returns false
while(xyz>>std.id>>std.name>>std.marks)
{    
    cout<<"record no is: "<<b<<endl;
    cout<<"id is: "<<std.id<<endl;
    cout<<"name is: "<<std.name<<endl;
    cout<<"marks are: "<<std.marks;
    b++;
}
break;

your code is working same as mine.i tried it also but useless..

//read data
case 2:
while(xyz)
{
xyz.read((char*)&std,sizeof(std));
   if(!xyz.eof()
{  
    cout<<endl;
    cout<<"id is: "<<std.id<<endl;
    cout<<"name is: "<<std.name<<endl;
    cout<<"marks are: "<<std.marks<<endl;
} 
}
break;

I'd try it and debug it, but I don't have a Turbo-C++ compiler on my Linux system. I could modify it to build/run on current compilers when I have the time, but when that is will be next week at the earliest.

If there is any online video tutorials' site to better understand the concept of file processing ,tell.

You could try something like this ... to get you started:

// binFile_structStudent.cpp //


// This example allows ONLY UNIQUE student ID's //

#include <iostream>
#include <fstream>
#include <cstring> // re. strcpy
#include <climits> // re. INT_MAX, INT_MIN


using namespace std;


// NOTE! file gets created first time program is run //
const char* FNAME = "stud.bin";

const int MIN_ID = 1000;
const int MAX_ID = 9999;
const int MIN_MARK = 0;
const int MAX_MARK = 100;
const int TOT_NAME_CHARS = 20;



struct Student
{
    int id;
    char name[TOT_NAME_CHARS];
    int mark;

    // ctors ... //
    Student() : id(-1), mark(-1) { strcpy( name, "unknown"); }
    Student( int i, const char* n, int m ) : id(i), mark(m) { strcpy( name, n); }

    fstream& save( fstream& fs ) const
    {
        fs.write( (char*)this, sizeof(Student) );
        return fs;
    }
    fstream& load( fstream& fs )
    {
        fs.read( (char*)this, sizeof(Student) );
        return fs;
    }

    void print() const
    {
        cout << id << ' ' << name << ' ' << mark;
    }
} ;



////////////////////////////////////////////////////////////
// BEGIN 5 UTILITIES helpful to students here //////////////
int takeInInt( const char* msg, int min = INT_MIN, int max = INT_MAX )
{
    int val = 0;
    while( true )
    {
        cout << msg << flush;
        if( cin >> val && cin.get() == '\n' )
        {
            if( val >= min && val <= max )
            break;
            cout << "Valid input range here is: "
                 << min << ".." << max << endl;
        }
        else
        {
            cout << "Valid integers only here please!\n";
            cin.clear(); // clear error flasgs
            while( cin.get() != '\n' ) ; // 'flush' cin stream //
        }
    }
    return val;
}
void takeInCstring( char str[], int tot_chars, const char* msg, bool emptyOk = false )
{
    while( true )
    {
        cout << msg << flush;
        if( cin.getline( str, tot_chars ) )
        {
            if( emptyOk ) return;
            else if( strlen(str) ) return;
            else
            {
                cout << "Empty strings are NOT valid input here ...\n";
                continue;
            }
        }
        // else ... if reach here ...
        cin.clear();
        while( cin.get() != '\n' ); // flush cin stream //
        cout << "Only a max string length of " << tot_chars-1
             << " char's permitted here!\n";
    }
}
char* toCapsOnAllFirstLetters( char* str )
{
    char *p = str, prev = ' ';
    for( ; *p ; ++p )
    {
        if( prev == ' ' || prev == '\t' )
            if( *p >= 'a' && *p <= 'z' )
                *p -=  ('a' -'A' );
        prev = *p;
    }
    return str;
}
int takeInChr( const char* msg )
{
    cout << msg << flush;
    int reply = cin.get();
    if( reply != '\n' ) while( cin.get() != '\n' ) ; // flush... //
    return reply;
}
bool more()
{
    int reply = takeInChr( "More (y/n) ?" );
    if( reply == 'n' || reply == 'N' ) return false;
    // else ...
    return true;
}
// END 5 UTILITIES helpful to students here ////////////////
////////////////////////////////////////////////////////////



// 5 FORWARD DECLARATIONS //////////////////////////////////
int showMenuGetChrChoice();
int takeInAndWrite( fstream& fs, int siz );
int readAndShowAll( fstream& fs );
void upDateOneRecord( fstream& fs, int siz );
void deleteOneRecord( fstream& fs, int siz );
// IF FOUND ...
// returns record pos ... (a number in range 0..rec_count-1)
// OR IF NOT found
// returns rec_count //
int idFoundAt( fstream& fs, int id );
////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////
int main() // BEGIN MAIN ///////////////////////////////////
{
    Student stud;
    int size = 0;
    fstream fs;

    fs.open( FNAME, ios::in | ios::out | ios::binary );
    if( !fs )
    {
        cout <<"File " << FNAME << " not found ...   "
             << "perhaps it was not yet created?\n";
        char reply = takeInChr( "Create it now (y/n) ? " );
        if( reply == 'y' || reply == 'Y' )
        {
            fs.open( FNAME, ios::out );
            fs.close();
            fs.open( FNAME, ios::in | ios::out | ios::binary );
            cout << "OK ... Empty file " << FNAME
                 << " is created now ...\n";
        }
        else
        {
            cout << "NOT created ... \n\n"
                 << "Press 'Enter' to continue/exit ..."
                 << flush;
            cin.get();
            return -1;
        }
    }
    else
    {
        size = readAndShowAll( fs );
        cout <<"File " << FNAME << " has "
             << size << " records.\n";
    }


    while( true )
    {
        int choice = showMenuGetChrChoice();
        if( choice >= 'A' && choice <= 'Z' ) choice += 'a' - 'A';
        switch( choice )
        {
        case '1': case 'w' : //take in and write stud data  ... size updated by ref... //
        size = takeInAndWrite( fs, size );
        cout << "size is now " << size << "\n\n";
        break;

        case '2': case 'r' ://read and show all file data and set size //
        if( size ) size = readAndShowAll( fs );
        else cout << "\nNo records yet ...\n\n";
        break;

        case '3': case 'u' : //update data //
        if( size ) upDateOneRecord( fs, size );
        else cout << "\nNo records yet ...\n\n";
        break;

        case '4': case 'd' ://delete data
        if( size ) deleteOneRecord( fs, size );
        else cout << "\nNo records yet ...\n\n";
        break;

        case '0' : case 'e' : case 'q' :
        cout << "All done now ...  Press 'Enter' to exit ... "
             << flush;
        cin.get();
        return 0;

        default:
        cout << "The choice " << char(choice)
             << " is NOT implemented here ...\n";
    }
}

} // END MAIN //////////////////////////////////////////////
////////////////////////////////////////////////////////////





int showMenuGetChrChoice()
{
    cout <<"1. Write data\n"
         <<"2. Read data\n"
         <<"3. Update data\n"
         <<"4. Delete data\n"
         <<"0  Exit ... \n\n";

    return takeInChr( "Enter your choice in range 0..4 or W..E : " );
}

// returns new size by ref... //
int takeInAndWrite( fstream& fs, const int siz )
{
    Student stud;
    int count = siz;
    while( true )
    {
        cout << "For Student # "<< ++count << endl;
        while( true )
        {
            stud.id = takeInInt( "Enter id    : ", MIN_ID, MAX_ID );
            if( idFoundAt( fs, stud.id ) == siz )
                break;
            // else ...
            cout << "The id number " << stud.id
                 << " was already used!\n";
        }
        takeInCstring( stud.name, TOT_NAME_CHARS, "Enter name  : " );

        toCapsOnAllFirstLetters( stud.name );

        stud.mark =    takeInInt( "Enter mark  : ", MIN_MARK, MAX_MARK );

        fs.seekp( 0, ios::end );
        stud.save( fs );

        if( ! more() ) break;
    }
    return count;
}
// returns number of records read from file ... //
int readAndShowAll( fstream& fs )
{
    Student stud;
    fs.seekg( 0, ios::beg ) ;
    int count = 0;
    while( stud.load(fs) )
    {
        cout << "Record no is : " << ++count << '\n'
             << "Id is        : " << stud.id << '\n'
             << "Name is      : " << stud.name << '\n'
             << "Mark is      : " << stud.mark << "\n\n";
    }
    fs.clear(); // clears eof flag that was set here ... //
    //cout << "size is now " << count << "\n\n";
    return count;
}

void upDateOneRecord( fstream& fs, int siz )
{
    Student stud;
    cout << "Record no's here are in range 1.." << siz << endl;
    int r = takeInInt( "Enter record no. to update: ", 1, siz );

    fs.seekg( (r-1)*sizeof(Student) );
    stud.load( fs );

    cout << "The record is: "; stud.print(); cout << '\n';

    // take in new id ...
    Student tmp;
    while( true )
    {
        tmp.id = takeInInt( "Enter new id   : ", MIN_ID, MAX_ID );
        int foundAt = idFoundAt( fs, tmp.id );
        if( foundAt == siz ) // i.e.is unique ... //
            break;
        else if( tmp.id == stud.id )
            break;

        // else .. if reach here ...
        cout << "The id number " << tmp.id
             << " is already used!\n";
    }

    stud.id = tmp.id;
    takeInCstring( stud.name, TOT_NAME_CHARS, "Enter new name : " );
    stud.mark = takeInInt( "Enter new mark : ", MIN_MARK, MAX_MARK );

    fs.seekp((r-1)*sizeof(Student));
    stud.save( fs );

    cout << "Record no. " << r  << " now is: ";
    stud.print(); cout << '\n';
}

void deleteOneRecord( fstream& fs, int siz )
{
    Student stud;
    cout << "Record no's here are in range 1.." << siz << endl;
    int r = takeInInt("Enter record no. to delete: ", 1, siz );

    fs.seekg( (r-1)*sizeof(Student) );
    stud.load( fs );

    cout << "The record is: "; stud.print(); cout << '\n';

    fs.seekg( (r-1)*sizeof(Student) );
    strcpy( stud.name, "unknown" );
    stud.mark = -1;
    stud.save( fs );

    cout << "Record no. " << r  << " now is: ";
    stud.print(); cout << '\n';
}

// IF FOUND ...
// returns record pos ... (a number in range 0..rec_count-1)
// OR IF NOT found
// returns rec_count //
int idFoundAt( fstream& fs, int id )
{
    Student stud;
    int recPos = 0;
    fs.seekg( 0, ios::beg );
    while( stud.load( fs ) )
    {
        if( stud.id == id ) break;
        ++recPos;
    }
    if( fs.eof() ) fs.clear();
    return recPos;
}

Addendum:

The comments at the top of the function below ...
are hopefully obviously seen to be wrong.

I simply forgot to change the comments (delete the comments) when I changed the code ...
from returning a void ... and passing in a ref...
to passing in a const value and returning the updated size.

// returns new size by ref... //
int takeInAndWrite( fstream& fs, const int siz )
{
    Student stud;
    int count = siz;
    while( true )
    {
    // ...

Also, I was thinking about the problem called the 'endian problem' ...

http://en.wikipedia.org/wiki/Endianness

A way to handle that problem could be to change both the integers in the struct to fixed length C strings if reading in number data as numbers ...and then converting them to strings to be stored in the struct

Then reverse the proccess after reading the struct back from file.

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.