- 分享
- 0
- 人气
- 1
- 主题
- 11
- 帖子
- 2767
- UID
- 339187
- 积分
- 2608
- 阅读权限
- 20
- 注册时间
- 2010-8-3
- 最后登录
- 2024-4-21
- 在线时间
- 11975 小时
|
以下是我的code,当user输入record的时候,ID就是1,2,3.......,n
如果我delete 第三个record,他的ID下一次就会变去3
不过现在如果我delete 第二个record,他的ID就要变去4,
就像是1,3,4,5,6,7....,n 请问我应该如何去做- #include <iostream>
- #include <fstream>
- #include<iomanip>
- #include <string>
- using namespace std;
- //--------------------------------CLASS--------------------------------
- class IMDB
- {
- private:
- char mvName[20];
- float rating;
- int mvID;
- public:
- void getMV();
- void getMVEdit();
- void showMV();
- void showMVEdit();
- int deleteID();
- };//end class
- //Insert all the movie details
- void IMDB::getMV()
- {
- //read all file to get the ID and the ID is auto increment by 1
- IMDB st;
- ifstream thisfile;
- thisfile.open("Movie Record.dat",ios::binary);
- if(!thisfile.eof())
- {
- mvID = 1;
- }
- while(thisfile.read(reinterpret_cast<char *> (&st), sizeof(IMDB)))
- {
- ++mvID;
- }
- thisfile.close();
-
- cout << "Movie ID: " << mvID << endl;
- //Enter all the record
- cout << "Enter movie name: ";
- cin.ignore(1000,'\n');
- cin.getline(mvName,20);
-
- cout << "Enter movie rating(0-5): ";
- while((!(cin >> rating)) || (rating < 0 || rating > 5))
- {
- cout << "Enter number between 0-5! Please try again: ";
- cin.clear();
- cin.ignore (1000,'\n');
- }
- }
- //Display all the movie have been added
- void IMDB::showMV()
- {
- cout << "Movie ID: " << mvID;
- cout << "\nMovie Name: " << mvName;
- cout << "\nRating: " << fixed << showpoint << setprecision(1) <<rating << endl;
- }
- //Return the movie ID for the purpose of delete
- int IMDB::deleteID()
- {
- return mvID;
- }
- //--------------------------------FUNCTION DEFINATION--------------------------------
- void displayMenu()
- {
- cout << "Welcome, What would you like to do?\n"
- << "1 => Add a Movie\n"
- << "2 => View a Movie\n"
- << "3 => Delete a Movie\n"
- << "4 => Quit\n"
- << endl;
- cout << "Choice => ";
- }
- //*****************************FUNCTION WRITE MOVIE*****************************
- void writeMV()
- {
- IMDB st;
- ofstream myfile;
-
- //create new file name name Movie Record.dat
- myfile.open("Movie Record.dat",ios::binary | ios::app);
- st.getMV();//get data from getMV()
- myfile.write(reinterpret_cast<char *> (&st), sizeof(IMDB));
- myfile.close();
-
- system("PAUSE");
- }
- //*****************************FUNCTION DISPLAY MOVIE*****************************
- void displayMV()
- {
- IMDB st;
- ifstream thisfile;
- thisfile.open("Movie Record.dat",ios::binary);
- if(!thisfile)
- {
- cout << "You haven't insert any record yet!\n";
- }
-
- //Display all the record
- while(thisfile.read(reinterpret_cast<char *> (&st), sizeof(IMDB)))
- {
- st.showMV();
- cout << "\n--------------------------------------\n";
- }
-
- thisfile.close();
- system("PAUSE");
- }
- //***************************FUNCTION DELETE MOVIE***************************
- void deleteMV(int number)
- {
- IMDB st;
- ifstream thisfile;
- thisfile.open("Movie Record.dat", ios::binary);
-
- if(!thisfile)
- {
- cout<<"You haven't insert any record yet!\n";
- }
- else
- {
- cout << "Enter movie ID to delete the record: ";
- while(!(cin >> number))
- {
- cout << "Enter number only! Please try again: ";
- cin.clear();
- cin.ignore (1000,'\n');
- }
- }
-
- ofstream myfile;
- myfile.open("Temp.dat",ios::out);
-
- thisfile.seekg(0,ios::beg);//read the file from beginning
- while(thisfile.read(reinterpret_cast<char *> (&st), sizeof(IMDB)))
- {
- if(st.deleteID()!= number)
- {
- myfile.write(reinterpret_cast<char *> (&st), sizeof(IMDB));
- }
- }
- myfile.close();
- thisfile.close();
- remove("Movie Record.dat");
- rename("Temp.dat","Movie Record.dat");
-
- cout << "Movie successful deleted!" << endl;
- system("PAUSE");
- }
- //--------------------------------MAIN FUNCTION--------------------------------
- int main()
- {
- int number;
- int selectOpt;
- bool done = false;
-
- do
- {
- displayMenu();
- cin >> selectOpt;
-
- switch(selectOpt)
- {
- case 1 : writeMV();break;
- case 2 : displayMV();break;
- case 3 : deleteMV(number);break;
- case 4 : done = true;
- break;
- default : cout << "Invalid selection, try again!" << endl;
- cin.clear();
- cin.ignore (1000, '\n');
- system("PAUSE");
- }
- }while(!done);
-
- system("PAUSE");
- return 0;
- }
复制代码 |
|