0
  1. Why does this code hang after logging in (option 1, then enter user name and password)?
  2. Do I need to include both fstream and iostream?
#include<fstream>
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
using namespace std;

class Admin
{
    char Uname[20];
    char Pass[20];
public:
    Admin()
    {
        strcpy(Uname,"Null");
        strcpy(Pass,"Null");
    }
    Admin(int)
    {
        strcpy(Uname,"Aha");
        strcpy(Pass,"you345");
    }
    Admin(char)
    {
        strcpy(Uname,"Mahi");
        strcpy(Pass,"852963");
    }
    char* GetUname()
    {return Uname;}
    char* GetPass()
    {return Pass;}
};

int Adchecker()
{
    fstream F("Admin.dat",ios::binary);
    Admin A,B(1),C('A');          // different data via overloads above
    F.write((char*)&B,sizeof(B)); // so that the file admin has something
    F.write((char*)&C,sizeof(C));
    char U[20],P[20];
    cout<<"\nEnter Username:";
    cin>>U;
    int z=0;                      // return flag
    cout<<"\nEnter Password:";
    cin>>P;
    F.write((char*)&A,sizeof(A));
    F.seekg(0,ios::beg);
    while(!F.eof())
    {
        F.read((char*)&A,sizeof(A));
        if(strcmp(A.GetUname(),U)==0)
        {
            z=2;
            if(strcmp(A.GetPass(),P)==0)
            {
                z=5;
                break;
            }
            else
                break;
        }

    }
    return z;
    F.close();
}

int main()
{
    int B=0;
    int c=4,i=0;
    char A='y';
    char status[20];
    ofstream F("Log.txt",ios::app);
    cout<<"\n\t\t\tWELCOME TO.......\n ~~~***This place!!!***~~~ ";
    while(A=='y'||A=='Y')
    {
        cout<<"\nAre you an...\n1.Admin\n2.Customer(1/2)???";
        cin>>B;
        switch(B)
        {
        case 1:
                while(c!=5)
                {
                    c=Adchecker();
                    i++;
                    if(c==5)
                    {
                        i=0;
                        cout<<"Welcome sire :3";
                        break;
                    }
                    if(c==2)
                    {
                        cout<<"Your password is wrong...\nYou have "<<(3-i)<<"chances left";
                    }
                    if(c==0)
                    {
                        cout<<"Your Username is incorrect..\nYou have "<<(3-i)<<"chances left";
                    }
                    if(i==3)
                    {
                        cout<"Nope";
                        exit(0);
                    }

                }
                break;
        case 2:
            cout<<"\nLater, customer :p\n";
            break;
        default:
            cout<<"good bye";
            exit(0);
        }
        cout<<"Again(Y/n)?)";
        cin>>A;
    }
    return 0;
}
Davis Herring
  • 36,443
  • 4
  • 48
  • 76
  • It's not really fair to criticize a homework assignment that isn't even finished, but you _need_ to know that this code is _atrocious_. If you didn't know that already, you need to start with some basic tutorial material before worrying about this program. – Davis Herring Oct 19 '17 at 04:35
  • I formatted the code and moved the explanatory comments into it. I also fixed the title to not refer to the details of the code. Hope this helps. – Davis Herring Oct 19 '17 at 04:49

1 Answers1

0
  1. It's invalid to construct an fstream with just ios::binary as the mode. Since you have no error checking, the loop to read from the unopened file doesn't terminate. (For reference, proper looping doesn't involve eof.)
  2. Yes; you need #include<fstream> for std::fstream and #include<iostream> for std::cout and friends.
Davis Herring
  • 36,443
  • 4
  • 48
  • 76