Error when using file handling in C++ project

I just learned file handling and now developing a restaurant management system with 2 different credentials(staff and admin) in c++. I keep facing 2 problems in the code for admin part although there is no error shown in the build log.

Problem 1: In admin page, when I enter 3 to delete staff using staff username, all the staff record will be deleted.

Problem 2: In admin page, when I enter 4 to check list of staff registered by username, the system will keep staying at the same page, the list will not be shown.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

int deleteStaffbyUsername()
{
    string line, username;
    cout << "Please enter the username of record you want to delete: ";
    cin >> username;
    ifstream myfile;
    ofstream temp;
    myfile.open("database.txt");
    temp.open(username+".txt");
    while (getline(myfile, line))
    {
        if (line != username)
        temp << line << endl;
    }
    cout << "The record with the name " << username << " has been deleted if it existed" << endl;
    myfile.close();
    temp.close();
    remove("database.txt");
    rename("temp.txt", "database.txt");
    return 0;
}

int checkListOfRegisteredStaff()
{

    cout<<"-------------------------------------\n\n";
    cout<<"|    Username of registered staff    |\n\n";
    cout<<"-------------------------------------\n\n";

    //check if record already exist..
    ifstream read;
    read.open("database.txt");

    if(read)
    {
        int recordFound =0;
        string line;
        while(getline(read, line))
        {

            char name[100];
            strcpy(name, line.c_str());
            string filename = name;
            cout<<"\n"<<filename;
            int total_lines = 0;
            ifstream read1;
            read1.open(filename.c_str(), ios::app);
            string line;
            while(getline(read1,line))
            {
                ++ total_lines;
            }
            read1.close();
            ifstream read;
            read.open(filename.c_str(), ios::app);
            string l;
            if(read)
            {
                int line_no = 0;
                while (line_no != total_lines && getline(read, l))
                {
                    ++line_no;
                }
                if(line_no==total_lines)
                {
                    cout<<l<<"\n";
                }
            read.close();
            }
    read.close();
    }
    }
    else
    {
        cout<<"\n\n No Record!\n";
    }

    cout<<"\n Please any key to continue..";
    getchar();
    return 0;
}
For deleteStaffByUsername: You open a file for reading called database.txt, and a file for writing called <username>.txt.

Then, you rename a file called temp.txt to database.txt. It appears you should be writing to temp.txt and not <username>.txt. (Or the other way around, but point is, it's not consistent.)

For checkListOfRegisteredStaff: First thing I would do is make sure that the file you open on line 48 is successfully opened. Actually, this applies to both functions in question. I bet it's not opening the file correctly (which it expects to be the name of a username, and without any explicit extension). i.e. the file doesn't exist.

1
2
3
4
5
ifstream read(filename, ios::app); // note: automatically calls open
if (!read)
{
    cout << "Error opening " << filename << '\n';
}


PS: If your compiler requires you to do ifstream.open(filename.c_str()) instead of just ifstream.open(filename), then you are using an old compiler.
Last edited on
Registered users can post here. Sign in or register to post.