A Reservation, Explain what happen at the choice F

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
int main()
{
    string userName;
    string userPassword;
    int loginAttempt = 0;
    bool loginStatus = false;

    while (loginAttempt < 5)
    {
        cout << "Please enter your user name: ";
        cin >> userName;
        cout << "Please enter your user password: ";
        cin >> userPassword;

        if ((userName == "BSIT" || userName == "bsit") && (userPassword == "1UG1" || userPassword == "1ug1"))
        {
            cout << "Welcome BSIT!\n";
            loginStatus = true;
            break;
        }
        else
        {
            cout << "Invalid login attempt. Please try again.\n" << '\n';
            loginAttempt++;
            loginStatus = false;
        }
    }
    if (loginAttempt == 5)
    {
        cout << "Too many login attempts! The program will now terminate.";
        cout << "Thank you for logging in.\n";
    }

    if(loginStatus == true){


        char choice;
        char home;
        start:
        system("cls");
        cout<<"\t\t    <><><> MCD Aesthetics Dental Clinic <><><>"<<endl;
        cout<<"\t\t\t'Where Beautiful smile begins'..\n"<<endl;
        cout<<" \n\n\t\tGOOD DAY! WELCOME TO OUR CLINIC\n "<<endl;
        cout<<"\t\t (A) Patient Reservation\n\t\t (B) Clinic Services Offered\n\t\t (C) Dentist and Clinic Background\n\t\t (D) Clinic Hours\n\t\t (E) View Appointment List\n\t\t (F) Delete Patient Details"<<endl;
        cout<<"\n \n How can we help you?: ";

        cin>>choice;
        Patient myPatient;
        fstream myFile;
        int lineCounter=1;
        string strOutput;
        string temp;
        ifstream seek1;
        ofstream seek2;
        ofstream temp1;
        string s_delete;

        switch(choice){
            case 'a':
            case 'A':

                cout<<"\n\t\t\t<><> PATIENT RESERVATION <><>\nPlease fill up the form:\n\n";

                myFile.open("AppointmentList.txt",ios::app|ios::in|ios::out);

                cout<<"Patient's name: ";
                cin>>myPatient.name;
                cout<<"Age: ";
                cin>>myPatient.age;
                cout<<"Address: ";
                cin>>myPatient.address;
                cout<<"Patient's Concern: ";
                cin>>myPatient.ptcrn;
                cout<<"Contact Number: ";
                cin>>myPatient.cntnum;

                myFile<<myPatient.name<<"|"<<myPatient.age<<"|"<<myPatient.address<<"|"<<myPatient.ptcrn<<"|"<<myPatient.cntnum<<"|\n";

                myFile.close();

                cout<<"\nPATIENT DETAILS SUCCESSFULLY ADDED\n";

                cout << endl << " Go back to main menu? (Y/N): ";
                cin >> home;
                if (home == 'y' || home == 'Y')
                    {
                        goto start;
                    }
                else if (home == 'N' || home == 'n')
                {
                    system("exit");
                }
                else
                {
                    goto start;
                }

                cout<<endl;
                break;

            case 'b':
            case 'B':

                cout<<"\n\t<><> CLINIC SERVICES OFFERED <><>\n"<<endl;
                cout<<"\t<>Tooth Cleaning\n\t<>Tooth Pasta\n\t<>Orthodontics\n\t<>Tooth Surgery\n\t<>Tooth Dentures\n\t<>Crowning\n\t<>Tooth Bridges\n\t<>Root Canal";
                cout<<"<>Implants\n\t<>Laminate\n\t<>Tooth Whitening"<<endl;

                cout << endl << " Go back to main menu? (Y/N): ";
                cin >> home;
                if (home == 'y' || home == 'Y')
                    {
                        goto start;
                    }
                else if (home == 'N' || home == 'n')
                {
                    system("exit");
                }
                else
                {
                    goto start;
                }
                break;
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
case 'c':
            case 'C':
                cout<<"\t\n<><> DENTIST AND CLINIC BACKGROUND <><>\n"<<endl;
                cout<<"MCD Aesthetics Dental Clinic,"<<"where beautiful smiles begin...\nLocated @ 970 Gil Carlos Poblacion,Baliuag,Bulacan\n\nSince May 4,2016\n"<<endl;
                cout<<"Dentist: Doctor Maria Carla H. Dizon\n\nGraduated at UP Manila,\n\nOctober 31,2007(Year Graduated)\n\nSecretary: Lizel Dionisio"<<endl;

                cout << endl << " Go back to main menu? (Y/N): ";
                cin >> home;
                if (home == 'y' || home == 'Y')
                    {
                        goto start;
                    }
                else if (home == 'N' || home == 'n')
                {
                    system("exit");
                }
                else
                {
                    goto start;
                }
                break;

            case 'd':
            case 'D':
                cout<<"\n <><> CLINIC HOURS <><>\n"<<endl;
                cout<<"Mondays-Fridays: 9am to 6pm\nThursdays: CLOSED\nSaturdays-Sundays: 10am-5pm "<<endl;

                cout << endl << " Go back to main menu? (Y/N): ";
                cin >> home;
                if (home == 'y' || home == 'Y')
                    {
                        goto start;
                    }
                else if (home == 'N' || home == 'n')
                {
                    system("exit");
                }
                else
                {
                    goto start;
                }
                break;

            case 'e':
            case 'E':

                cout<<"\n\t\t\t<><> PATIENT RESERVATION <><>\nPlease fill up the form:\n\n";

                myFile.open("AppointmentList.txt",ios::app|ios::in|ios::out);

                myFile.seekg(0,ios::beg);

                cout<<"\nPATIENT DETAILS\n";

                while(getline(myFile,strOutput,'|')){
                    if(lineCounter==1){
                        strOutput.erase(std::remove(strOutput.begin(), strOutput.end(), '\n'), strOutput.end());
                        myPatient.name=strOutput;
                    }else if(lineCounter==2){
                        myPatient.age=strOutput;
                    }else if(lineCounter==3){
                        myPatient.address=strOutput;
                    }else if(lineCounter==4){
                        myPatient.ptcrn=strOutput;
                    }else if(lineCounter==5){
                        myPatient.cntnum=strOutput;
                        lineCounter = 0;
                        cout<<"*********************************************\n";
                        cout<<"\nName: "<<myPatient.name<<"\nAge: "<<myPatient.age<<"\nAddress: "<<myPatient.address<<"\nPatient's Concern: "<<myPatient.ptcrn<<"\nContact Number: "<<myPatient.cntnum<<"\n";
                    }

                    lineCounter ++;

                }
                myFile.close();

                cout << endl << " Go back to main menu? (Y/N): ";
                cin >> home;
                if (home == 'y' || home == 'Y')
                    {
                        goto start;
                    }
                else if (home == 'N' || home == 'n')
                {
                    system("exit");
                }
                else
                {
                    goto start;
                }

                cout<<endl;
                break;

            case 'f':
            case 'F':

                cout<<"\n\t\t\t<><> PATIENT RESERVATION <><>\nPlease fill up the form:\n\n";

                seek1.open( "AppointmentList.txt");
                seek2.open( "AppointmentList.txt", fstream::in | fstream::out | fstream::ate | ios::app);
                cin.ignore();

                temp1.open("temp.txt");
                cout << "Which record do you want to remove? "<<endl;
                getline(cin, s_delete);
                while (getline(seek1,temp))
                {
                    if (temp.find(s_delete) == string::npos){
                             temp1 << temp << endl;
                    }
                 }
                temp1.close();
                seek1.close();
                seek2.close();

                remove("AppointmentList.txt");
                rename("temp.txt","AppointmentList.txt");
                cout <<endl<<"Delete success.\n";


                cout << endl << " Go back to main menu? (Y/N): ";
                cin >> home;
                if (home == 'y' || home == 'Y')
                {
                    goto start;
                }
                else if (home == 'N' || home == 'n')
                {
                    system("exit");
                }
                else
                {
                    goto start;
                }

                cout<<endl;
                break;

            default:
                cout<<"\nYou have entered invalid input, Please try again."<<endl;

                cout << endl << " Go back to main menu? (Y/N): ";
                cin >> home;
                if (home == 'y' || home == 'Y')
                    {
                        goto start;
                    }
                else if (home == 'N' || home == 'n')
                {
                    system("exit");
                }
                else
                {
                    goto start;
                }

            }

    }

    return 0;
}
ALl the 2 ABove are the code pls help me to understand the process Thanks
option F appears to delete an entry from the file.
It does this by rewriting the file without the offending data and swapping the new file over the old one.

this code has a lot of issues... gotos, system exit (closes the terminal, but not the program, I think?), plaintext login visible in the compiled code, monolith, and more. It looks like someone who know what they were doing (sort of) got in a hurry and hacked this out to 'get it done' . I would just rewrite it rather than try to fix or understand it. Understanding it, its a bad example of doing things.
Last edited on
As jonin mentioned, you have lots of problems.

A few that jump out:
Line 31: You should do a return 0; here. Then there would be no need to put the rest of the program under an if statement at line 34.

You program should be broken into functions. One function for each branch of your switch statement.

Line 77: You're outputting multiple fields to appointmentlist.txt.
Line 309: When you checking to delete, you're not checking just the name, you're checking against the whole record that was written at line 77. Not user friendly.

Line 301: Why are you opening seek2? Your don't use it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
                cout << endl << " Go back to main menu? (Y/N): ";
                cin >> home;
                if (home == 'y' || home == 'Y')
                    {
                        goto start;
                    }
                else if (home == 'N' || home == 'n')
                {
                    system("exit");
                }
                else
                {
                    goto start;
                }

This code is just bad.

It is repeated in every branch of your switch statement. It should appear only once AFTER the switch statement.

You should avoid gotos.

If you want to terminate the program, just return 0 from main or call exit(0);
system("exit") will exit your system shell.
Last edited on
there is a part at the reservation


look like this getline(myfile,strOutput,'|")

what does '|' mean??
Have you read the documentation for getline?
http://www.cplusplus.com/reference/string/string/getline/?kw=getline

'|' is a delimiter.

istream& getline (istream& is, string& str, char delim);

Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, '\n', for (2)).
Topic archived. No new replies allowed.