File handling class object
<p>This is a C++ program for implementing offline messaging system. First a user has to register for an account by choosing a username and password. Then they can send and receive messages to other users.</p>

<p>I have written the program but there appears to be some logical errors.</p>

<p>I write the user details into a file email upon registration. Data is written into files as objects.</p>

<p>Suppose I registered 3 accounts. but I can't sign in IE. I can't verify login and pass control to <code>signin()</code>;</p>

<p>To troubleshoot the error I have created a <code>print()</code> function that displays content of file email.</p>

<p>I have placed this just before login authenticating part to see if data records are present</p>

<p>However records are not being displayed.</p>

<p>I can't test other portion of the code until I solve this.</p>

<p>Partial code that I think is the possible cause of error:</p>

<pre><code>#include&lt;fstream.h&gt;
#include&lt;conio.h&gt;
#include&lt;string.h&gt;
#include&lt;process.h&gt;
#include&lt;stdio.h&gt;

int user;           // to keep a track of no. of users

class mail
{
public:
    char un[25];             // user name
    char pd[25];             // passsword
    int ino;                 // no. of inbox messages
    int sno;                 //no.of sent messags
    char isbj[25][25];
    char ssbj[25][25];
    char imsg[25][50];
    char smsg[25][50];       // character array to store inbox and sent messages respecvly
    void reg();             // func to sign up
    void signin();
    mail()
    {
    }
    ~mail()
    {
    }
};

void print();
void print()                     // for testing purpose. retrieving data in the file
{
    mail o;
    cout&lt;&lt;"\nRunning print func();";

    ifstream filein;
    filein.open("email",ios::in|ios::binary);
    if(!filein)
    {
        cout&lt;&lt;"Unable to open file to read\n";
    }
    else
    {
        filein.seekg(0);
        filein.read((char *)&amp;o,sizeof(o));
        while(filein)
        {
            puts(o.un);
            puts(o.pd);
            cout&lt;&lt;o.ino&lt;&lt;"  "&lt;&lt;o.sno;
            filein.read((char *)&amp;o,sizeof(o));
        }
        getch();
    }
}


void mail::reg()                      // sign up function
{
    clrscr();
    cout&lt;&lt;"Enter user name ( enter unique name )\n";
    gets(un);
    cout&lt;&lt;"Enter password\n";
    gets(pd);
    ofstream filout;
    filout.open("email",ios::app|ios::binary);
    if(!filout)
    {
        cout&lt;&lt;"cannot open file\n";
    }
    else
    {
        ino=-1;
        sno=-1;                  // initialising no. of sent and inbox msgs as 0 on regtrn

        filout.write((char *)&amp;this,sizeof(this));
        filout.close();
    }
    cout&lt;&lt;"You are now registered. Plz login\n";
    getch();

}        // end of sign up or register func



void mail::signin()
{
    .
        .
        .
} // end of signin




void screen(void);
void signup(void);


void main()
{
    clrscr();
    char usrnm[25],pwd[25],s[25],c;
    int i;
m:
    clrscr();
    screen();
    gotoxy(30,4);
    c=getch();
    if(c=='i'||c=='I')               //if user opts to sign in
    {
        gotoxy(62,11);
        gets(usrnm);
        gotoxy(62,13);
        for(i=0;i&lt; 25;i++)                   // getting password
        {
            if( i &lt; 0)
                i=0;
            pwd[i]=getch();
            if(pwd[i]=='\b')
            {
                i-=2;
                cout&lt;&lt;"\b \b";
                continue;
            }

            if(pwd[i]=='\r')
                break;
            cout&lt;&lt;"*";
        }
        pwd[i]='\0';
        clrscr();              // pwd now contains password for sign in

        print();

        int f;
        {                       // local block

            mail obj;
            ifstream filein;
            filein.open("email",ios::in|ios::binary);    // to read records from file and authenticating the user
            if(!filein)
            {
                cout&lt;&lt;"Unable to open file to read\n";
            }
            else
            {
                filein.seekg(0);
                filein.read((char *)&amp;obj,sizeof(obj));
                while(filein)
                {
                    if(strcmp(usrnm,obj.un)==0)
                    {
                        f=1;
                        if(strcmp(pwd,obj.pd)==0)
                        {
                            obj.signin();            // user authenticated. now transfering control
                            break;
                        }
                        else
                        {
                            cout&lt;&lt;"wrong password\n";
                            getch();
                            break;
                        }
                    } // end of if
                    else
                    {
                    }
                    filein.read((char *)&amp;obj,sizeof(obj));
                }               // end of while
                filein.close();
            } // end of else

        }


        if(f==0)
            cout&lt;&lt;"Wrong username or password\n";
        getch();
        goto m;
    }
    else if(c=='s'||c=='S')       // user opts to sign up
    {
        signup();
        getch();
        goto m;
    }
    else if(c=='e'||c=='E')       // user choose to exit
        exit(0);
    else
    {
        clrscr();
        cout&lt;&lt;"you have entered a wrong key. please check and try again\n";
        getch();
        goto m;
    }

}  //end of main


void signup()                   // sign up function
{
    int t;
    clrscr();
    {
        mail obj;
        ifstream filin;
        filin.open("user.txt",ios::ate);      // a file to store the no. of currently registered user. THIS IS FOR FUTURE USE
        if(!filin)
        {
            cout&lt;&lt;"cannot open file\n";
        }
        else
        {
            filin.seekg(0);
            filin&gt;&gt;t;
        }
        filin.close();
        user=t;
        ++user;                     // incrementing no. users

        ofstream filout;
        filout.open("user.txt",ios::out);
        if(!filout)
        {
            cout&lt;&lt;"cannot open file\n";
        }
        else
        {
            filout.seekp(0);
            filout&lt;&lt;user;                 //Updating user
        }
        filin.close();
        obj.reg();

    }  // end of block

    return;

}       // end of sign up



void screen()                // function for login screen
{
    .
        .
}
</code></pre>

<p>Full code:</p>

<pre><code>#include&lt;fstream.h&gt;
#include&lt;conio.h&gt;
#include&lt;string.h&gt;
#include&lt;process.h&gt;
#include&lt;stdio.h&gt;

int user;           // to keep a track of no. of users

class mail
{
public:
    char un[25];             // user name
    char pd[25];             // passsword
    int ino;                 // no. of inbox messages
    int sno;                 //no.of sent messags
    char isbj[25][25];
    char ssbj[25][25];
    char imsg[25][50];
    char smsg[25][50];       // character array to store inbox and sent messages respecvly
    void reg();             // func to sign up
    void signin();
    mail()
    {
    }
    ~mail()
    {
    }
};

void print();
void print()                     // for testing. retrieving data in the file
{
    mail o;
    cout&lt;&lt;"\nRunning print func();";

    ifstream filein;
    filein.open("email",ios::in|ios::binary);
    if(!filein)
    {
        cout&lt;&lt;"Unable to open file to read\n";
    }
    else
    {
        filein.seekg(0);
        filein.read((char *)&amp;o,sizeof(o));
        while(filein)
        {
            puts(o.un);
            puts(o.pd);
            cout&lt;&lt;o.ino&lt;&lt;"  "&lt;&lt;o.sno;
            filein.read((char *)&amp;o,sizeof(o));
        }
        getch();
    }
}


void mail::reg()                      // sign up function
{
    clrscr();
    cout&lt;&lt;"Enter user name ( enter unique name )\n";
    gets(un);
    cout&lt;&lt;"Enter password\n";
    gets(pd);
    ofstream filout;
    filout.open("email",ios::app|ios::binary);
    if(!filout)
    {
        cout&lt;&lt;"cannot open file\n";
    }
    else
    {
        ino=-1;
        sno=-1;                  // initialising no. of sent and inbox msgs as 0 on regtrn

        filout.write((char *)&amp;this,sizeof(this));
        filout.close();
    }
    cout&lt;&lt;"You are now registered. Plz login\n";
    getch();

}        // end of sign up or register func



void mail::signin()
{
    int f,i;
    char subj[25],message[50],usrnm[25];    // temporary varibles to store subject,message and username

start:
    clrscr();
    cout&lt;&lt;"1.compose \n2.inbox \n3.Sent\n4.logout";
    cout&lt;&lt;"\nEnter ur choice\n";
    int ch;
    cin&gt;&gt;ch;

    switch(ch)
    {

    case 1:                                       // switch for compose message
        cout&lt;&lt;"enter username\n";
        gets(usrnm);
        {            // beginning of local loop
            mail temp;

            ifstream filein;
            filein.open("email",ios::in|ios::binary);
            if(!filein)
            {
                cout&lt;&lt;"Unable to open file to read\n";
            }
            else
            {
                filein.read((char *)&amp;temp,sizeof(temp));
                while(filein)
                {
                    if(strcmp(usrnm,temp.un)==0)
                    {
                        cout&lt;&lt;" User name valid\n";
                        f=1;
                        cout&lt;&lt;"Enter subject\n";
                        gets(subj);
                        cout&lt;&lt;"Enter message\n";
                        gets(message);
                        temp.ino++;
                        strcpy(temp.isbj[temp.ino],subj);
                        strcpy(temp.imsg[temp.ino],message);  // copying message to recepient inbox

                        sno++;
                        strcpy(ssbj[sno],subj);
                        strcpy(smsg[sno],message);  // copying messages to sender sent folder

                        cout&lt;&lt;"Your message has been sent successfully\n";
                        break;
                    }
                    else      // to avoid if else mismatch
                    {
                    }
                    filein.read((char *)&amp;temp,sizeof(temp));
                }               // end of while
            }
            filein.close();


            mail temp1;

            long pos;                  // below code modifies current users object
            fstream fio("email",ios::in|ios::out|ios::binary);
            pos=fio.tellg();
            fio.read((char*)&amp;temp1,sizeof(temp1));
            while(fio)
            {
                if(strcmp(un,temp1.un)==0)
                {
                    fio.seekg(pos);
                    fio.write((char*)&amp;this,sizeof(this));
                    break;
                }
                pos=fio.tellg();
                fio.read((char*)&amp;temp1,sizeof(temp1));
            }   //end of while
            fio.close();

            mail temp2;               // below code modifies recipients object

            fio.open("email",ios::in|ios::out|ios::binary);
            pos=fio.tellg();
            fio.read((char*)&amp;temp2,sizeof(temp2));
            while(fio)
            {
                if(strcmp(temp.un,temp2.un)==0)
                {
                    fio.seekg(pos);
                    fio.write((char*)&amp;temp,sizeof(temp));
                    break;
                }
                pos=fio.tellg();
                fio.read((char*)&amp;temp2,sizeof(temp2));
            } // end of while
            fio.close();
        } // end of block

        if(f==0)
            cout&lt;&lt;"invalid user. Please enter a valid user name\n";
        getch();
        break;

    case 2:
        if(ino&lt;0)
            cout&lt;&lt;"There are no messages in your inbox\n";
        else
        {
            for(i=0;i&lt;=ino;i++)
            {
                cout&lt;&lt;"\nMessage "&lt;&lt;i+1&lt;&lt;"\n";   // printing inbox messages
                puts(isbj[i]);
                puts(imsg[i]);
            }
        }
        getch();
        break;

    case 3:
        if(sno&lt;0)
            cout&lt;&lt;"There are no sent messages\n";
        else
        {
            for(i=0;i&lt;=sno;i++)
            {
                cout&lt;&lt;"\nMessage "&lt;&lt;i+1&lt;&lt;"\n";      // printing sent messages
                puts(ssbj[i]);
                puts(smsg[i]);
            }
        }
        getch();
        break;

    case 4:
        cout&lt;&lt;"Logout sucess\n";
        return;

    default : cout&lt;&lt;"wrong choice\n";

    } // end of switch

    goto start;

} // end of signin




void screen(void);
void signup(void);


void main()
{
    clrscr();
    char usrnm[25],pwd[25],s[25],c;
    int i;
m:
    clrscr();
    screen();
    gotoxy(30,4);
    c=getch();
    if(c=='i'||c=='I')               //if user opts to sign in
    {
        gotoxy(62,11);
        gets(usrnm);
        gotoxy(62,13);
        for(i=0;i&lt; 25;i++)                   // getting password
        {
            if( i &lt; 0)
                i=0;
            pwd[i]=getch();
            if(pwd[i]=='\b')
            {
                i-=2;
                cout&lt;&lt;"\b \b";
                continue;
            }

            if(pwd[i]=='\r')
                break;
            cout&lt;&lt;"*";
        }
        pwd[i]='\0';
        clrscr();              // pwd now contains password for sign in

        print();

        int f;
        {                       // local block

            mail obj;
            ifstream filein;
            filein.open("email",ios::in|ios::binary);    // to read records from file and authenticating the user
            if(!filein)
            {
                cout&lt;&lt;"Unable to open file to read\n";
            }
            else
            {
                filein.seekg(0);
                filein.read((char *)&amp;obj,sizeof(obj));
                while(filein)
                {
                    if(strcmp(usrnm,obj.un)==0)
                    {
                        f=1;
                        if(strcmp(pwd,obj.pd)==0)
                        {
                            obj.signin();            // user authenticated. now transfering control
                            break;
                        }
                        else
                        {
                            cout&lt;&lt;"wrong password\n";
                            getch();
                            break;
                        }
                    } // end of if
                    else
                    {
                    }
                    filein.read((char *)&amp;obj,sizeof(obj));
                }               // end of while
                filein.close();
            } // end of else

        }


        if(f==0)
            cout&lt;&lt;"Wrong username or password\n";
        getch();
        goto m;
    }
    else if(c=='s'||c=='S')       // user opts to sign up
    {
        signup();
        getch();
        goto m;
    }
    else if(c=='e'||c=='E')       // user choose to exit
        exit(0);
    else
    {
        clrscr();
        cout&lt;&lt;"you have entered a wrong key. please check and try again\n";
        getch();
        goto m;
    }

}  //end of main


void signup()                   // sign up function
{
    int t;
    clrscr();
    {
        mail obj;
        ifstream filin;
        filin.open("user.txt",ios::ate);      // a file to store the no. of currently registered user. THIS IS FOR FUTURE USE
        if(!filin)
        {
            cout&lt;&lt;"cannot open file\n";
        }
        else
        {
            filin.seekg(0);
            filin&gt;&gt;t;
        }
        filin.close();
        user=t;
        ++user;                     // incrementing no. users

        ofstream filout;
        filout.open("user.txt",ios::out);
        if(!filout)
        {
            cout&lt;&lt;"cannot open file\n";
        }
        else
        {
            filout.seekp(0);
            filout&lt;&lt;user;                 //Updating user
        }
        filin.close();
        obj.reg();

    }  // end of block

    return;

}       // end of sign up



void screen()                // function for login screen
{
    int i;
    for(i=0;i&lt;80;i++)
        cout&lt;&lt;"*";
    cout&lt;&lt;" SuperMail                             New to SuperMial ? Create an Account     ";
    for(i=0;i&lt;80;i++)
        cout&lt;&lt;"*";
    cout&lt;&lt;" Press S To Sign up                    Already have a account Press I to sign in";
    for(i=0;i&lt;80;i++)
        cout&lt;&lt;"-";
    gotoxy(1,6);
    cout&lt;&lt;"\n\n\n\n Super mail is built on the idea that \n it can be more intuitive,efficient and \n useful and may be even fun:\n\n Lots of space\n\n Less spam";
    gotoxy(1,22);
    for(i=0;i&lt;80;i++)
        cout&lt;&lt;"-";
    gotoxy(32,23);
    cout&lt;&lt;"PRESS E TO EXIT";
    gotoxy(1,24);
    for(i=0;i&lt;80;i++)
        cout&lt;&lt;"-";
    gotoxy(50,10);
    cout&lt;&lt;"-------------------------------";
    gotoxy(50,11);
    cout&lt;&lt;"| USERNAME :                  |";
    gotoxy(50,12);
    cout&lt;&lt;"-------------------------------";
    gotoxy(50,13);
    cout&lt;&lt;"| PASSWORD :                  |";
    gotoxy(50,14);
    cout&lt;&lt;"-------------------------------";
}
</code></pre>
