jeudi 7 janvier 2021

Removing a text file

I am beginner in C++. I programmed a code for deleting and editing time of movies in text file. But I got stuck there. The program need to create a temp file first to transfer all the available data in "Movies.txt" first. Then, after copying it, my program must remove the existing Movies.txt name rename the temp.txt to Movies.txt. For me, the program is fine until copying the data into temp.txt but then its not deleting the Movies.txt and renaming the temp.txt. Can anyone help me on this?

Avenger|09:30|08:00|07:00
Policeman|09:30|08:00|07:00
Superman|09:30|08:00|07:00
Batman|09:30|08:00|07:00

My code:

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>

using namespace std;

showmovielist()
{
    char name [25];
    char time1[25];
    char time2[25];
    char time3[25];

    system("CLS");
    fstream movie;
    movie.open("Movies.txt",ios::in);

    int count = 0;
    string line;

    ifstream file("Movies.txt");
    while (getline(file, line))
        count++;

    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<< endl;
    cout<<"                                               MTRS TICKET BOOKING SYSTEM                                            "<< endl;
    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<< endl;
    cout<< endl;
    cout<<"======================================================================================================================" << endl;
    cout<<"|SERIAL NUMBER|               TIME[SHOW1]              TIME[SHOW2]              TIME[SHOW3]            |MOVIE NAME|" << endl;
    cout<<"======================================================================================================================" << endl;
    int i = 0;
    while(!movie.eof(), i++ , i<= count)
    {
        movie.getline(name,25,'|');
        movie.getline(time1,25,'|');
        movie.getline(time2,25,'|');
        movie.getline(time3,25);
    cout<<"    (" << i << ") " << "\t\t\t " << time1 << "\t\t\t " << time2 << "\t\t\t " << time3 << "\t\t\t " <<name << "\n";
    }
    movie.close();
    return 0;
}

 blank_line(const char *file_name)
{

ifstream fin(file_name);

ofstream fout;
fout.open("temp.txt", ios::out);

string str;
while(getline(fin,str))
{
while (str.length()== 3 )
getline(fin,str);

fout<<str<<endl;
}
fout.close();
fin.close();

remove(file_name);
rename("temp.txt", file_name);
return 0;
}

add_movie()
{
    char name [25];
    char time1[25];
    char time2[25];
    char time3[25];

    fstream movie;
    movie.open("Movies.txt",ios::app);

    cin.ignore();
    cout<<" Enter the movie name: ";
    cin.getline(name,25);
    cout<<" Enter the time for First show  (HH:MM): ";
    cin.getline(time1,25);
    cout<<" Enter the time for Second show (HH:MM): ";
    cin.getline(time2,25);
    cout<<" Enter the time for Third show  (HH:MM): ";
    cin.getline(time3,25);
    movie << name<<'|'<<time1<<'|'<<time2<<'|' <<time3<<'\n';
    cout << endl;
    cout<< "You are done, Manager!";
    movie.close();
    return 0;
}

delete_movie()
{
fstream movie;
fstream temp;

movie.open("Movies.txt",ios::in);
temp.open("temp.txt",ios::out);
char name [25];
char time1[25];
char time2[25];
char time3[25];
char a[25];
cin.ignore();
cout << endl;
cout<<"Enter the name of the movie that you want to delete : ";
cin.getline(a,25);
while(!movie.eof())
{
    movie.getline(name,25,'|');
    movie.getline(time1,25,'|');
    movie.getline(time2,25,'|');
    movie.getline(time3,25);
    if(strcmp(name,a)==0)
    {
        continue;
    }
    else
    {
        temp<< name<<'|'<<time1<<'|'<<time2<<'|' << time3 <<'\n';
    }
}
temp.close();
movie.close();

movie.open("Movies.txt",ios::out);
temp.open("temp.txt",ios::in);
while(!temp.eof())
{
    temp.getline(name,25,'|');
    temp.getline(time1,25,'|');
    temp.getline(time2,25,'|');
    temp.getline(time3,25);
    movie<< name<<'|'<<time1<<'|'<<time2<<'|' << time3 <<'\n';
}
temp.close();
movie.close();
remove("temp.txt");
blank_line("Movies.txt");
cout<<"\n done !!! \n";
return 0;
}

update_movie()
{
    fstream movie;
    fstream temp;

    movie.open("Movies.txt",ios::in);
    temp.open("temp.txt",ios::out);
    char name [25];
    char time1[25];
    char time2[25];
    char time3[25];
    char a[25];
    cin.ignore();
    cout<<"Enter the name of the movie that you want to update : ";
    cin.getline(a,25);
    while(!movie.eof())
    {
        movie.getline(name,25,'|');
        movie.getline(time1,25,'|');
        movie.getline(time2,25,'|');
        movie.getline(time3,25);
        if(strcmp(name,a)==0)
        {
            cout<<" Enter the first showtime [SHOW 1]: ";
            cin.getline(time1,25);
            cout<<" Enter the second showtime [SHOW 2]: ";
            cin.getline(time2,25);
            cout<<" Enter the third showtime [SHOW 3]: ";
            cin.getline(time3,25);
            temp<< name<<'|'<<time1<<'|'<<time2<<'|' << time3 <<'\n';
        }
        else
        {
            temp<< name<<'|'<<time1<<'|'<<time2<<'|' << time3 <<'\n';
        }

    }
    temp.close();
    movie.close();

    movie.open("Movies.txt",ios::out);
    temp.open("temp.txt",ios::in);
    while(!temp.eof())
    {
        temp.getline(name,25,'|');
        temp.getline(time1,25,'|');
        temp.getline(time2,25,'|');
        temp.getline(time3,25);
        movie<< name<<'|'<<time1<<'|'<<time2<<'|' << time3 <<'\n';
    }
    temp.close();
    movie.close();
    remove("temp.txt");
    blank_line("Movies.txt");
    cout<<"\n You have done editing the showtime manager!!! \n";
    return 0;
}

int main()

{
    char name [25];
    char time1[25];
    char time2[25];
    char time3[25];
    string customer, phone_number;
    fstream movie;
    movie.open("Movies.txt",ios::in);
    char selection_role ;
    int selection_movie, selection_show,seat_price;
    char seat_row,seat_column, seat_type, response, MTRS_response;



    cout << "                  *************************************************************** " << endl;
    cout << "                              MOVIE TICKET RESERVATION SYSTEM (MTRS)              " << endl;
    cout << "                  *************************************************************** " << endl;
    cout << endl << endl ;
    cout << "                           Please specify the selection : (1) CUSTOMER"   << endl;
    cout << "                                                          (2) MANAGEMENT" << endl;
    cout << endl;
    cout << "                                           INPUT : ";
    cin >> selection_role;

    if (selection_role == '1')
{
    cout << endl;
    cout << "MOVIES AVAILABLE : ";
    showmovielist();

    cout << endl;
    cout << "Please select a movie: ";
    cin >> selection_movie ;

    int i = 0;
    while(i++ , i <= selection_movie)
    {
      movie.getline(name,25,'|');
      movie.getline(time1,25,'|');
      movie.getline(time2,25,'|');
      movie.getline(time3,25);
    }

      cout << endl;
      cout << "======You have selected " << name <<".=========" <<endl;

      cout << "Which show you want to select for [SHOW1 / SHOW2 / SHOW3]: ";
      cin >> selection_show;
      cout << endl;

      system("CLS");
      cout << "                                  SCREEN                                        "<<endl << endl<< endl;
      cout << "[A,1]          [A,2]          [A,3]          [A,4]          [A,5]          [A,6]" <<endl << endl;
      cout << "[B,1]          [B,2]          [B,3]          [B,4]          [B,5]          [B,6]" <<endl << endl;
      cout << "[C,1]          [C,2]          [C,3]          [C,4]          [C,5]          [C,6]" <<endl << endl;
      cout << "[D,1]          [D,2]          [D,3]          [D,4]          [D,5]          [D,6]" <<endl << endl;
      cout << "[E,1]          [E,2]          [E,3]          [E,4]          [E,5]          [E,6]" <<endl << endl;

      cout << endl;
      cout << "Enter the row of seat you would like to book for [ROW : A,B,C...]: ";
      cin >> seat_row ;
      cout << endl;
      cout << "Enter the column of seat you would like to book for [COLOMN : 1,2,3...]:  ";
      cin >> seat_column;
      cout << endl<< endl;

      cout <<"Your selection of seat is : (" << seat_row << "," << seat_column << ")" << endl;

      cout <<"There is two types of seats are available:  (A) DELUXE (RM 18) " << endl;
      cout <<"                                            (B) NORMAL (RM 13) " << endl << endl;

      cout <<"Do yo want futher explanation about the type of seats available in our cinemas before selecting it (Y/N) :" ;
      cin >> response;

      if (response == 'y' || response == 'Y')
      {
          cout << "- 'DELUXE' seats provides a unique experience to the customers where they will be provided with food during the showtime."<<endl;
          cout << "- 'NORMAL' seats does not include the special privileges" << endl;
      }

     cout <<"Please select the type of seat:- ";
     cin >> seat_type;

     cout <<"Enter your name Mr/Ms   : ";
     cin >>customer;
     cout <<"Enter your phone number : ";
     cin >> phone_number;

     cout <<" Do you have MTRS reward card(Y/N): ";
     cin >> MTRS_response;

     if (seat_type== 'A' || seat_type== 'a')
        seat_price = 18;

     else
        seat_price = 13;


   system("CLS");
   cout << "              MOVIE TICKET               " << endl;
   cout << "=========================================" << endl;
   cout << endl;
   cout <<"To Mr/Ms " << customer << "," << endl;
   cout <<"Seat selected         : (" << seat_row << "," << seat_column << ")" << endl;
   cout <<"Price of seat         : " << seat_price << endl;
   movie.close();

}

  if (selection_role == '2')
{
    system("CLS");
    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<< endl;
    cout<<"                                         MTRS TICKET BOOKING SYSTEM (MANAGEMENT)                                     "<< endl;
    cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<< endl;
    string userName;
    string userPassword;
    int loginAttempt = 0;

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

        if (userName == "Mathavan" && userPassword == "MTRS")
        {
            cout << "Welcome Mathavan!" << endl;
            break;
        }
        else if (userName == "Sofea" && userPassword == "MTRS")
        {
            cout << "Welcome Sofea!" << endl;
            break;
        }
        else if (userName == "Nicole" && userPassword == "MTRS")
        {
            cout << "Welcome Nicole!" << endl;
            break;
        }
        else if (userName == "Weishan" && userPassword == "MTRS")
        {
            cout << "Welcome Weishan!" << endl;
            break;
        }
        else if (userName == "Winston" && userPassword == "MTRS")
        {
            cout << "Welcome Winston!" << endl;
            break;
        }
        else
        {
            cout << "Invalid login attempt. Please try again.\n" << '\n';
            loginAttempt++;
        }
    }
    if (loginAttempt == 5)
    {
            cout << "Too many login attempts! The program will now terminate.";
            return 0;
    }

    bool flag=true;
    while (flag)
    {
        system("CLS");
        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<< endl;
        cout<<"                                         MTRS TICKET BOOKING SYSTEM (MANAGEMENT)                                     "<< endl;
        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<< endl;
        cout<< endl << endl << endl;
        cout << "                                          (1) Enter a new movie " << endl<< endl<<endl;
        cout << "                                          (2) Update the showtime for a movie"<< endl << endl<< endl;
        cout << "                                          (3) Delete a movie "<< endl <<endl << endl;
        int c;
        cout << "Enter your selection manager: ";
        cin>>c;
        switch (c)
        {
        case 1 :
            add_movie();
            break;
        case 3:
            showmovielist();
            delete_movie();
            break;
        case 2:
            showmovielist();
            update_movie();
            break;

        default :
            cout << " \n choose right number \n";
        }
        cout << " if you continue to use program press ' y ' \n ";
        char f;
        cin>>f;
        if(f!='y')
        {
            flag=false;
        }
    }
    return 0;
}
}

Aucun commentaire:

Enregistrer un commentaire