The following is an excerpt from a complete program that runs without any errors. It has 2 file dependencies which it uses to access and store information.
The program, in its entirety, displays a menu with 5 options the user can choose from.
- View Characters
- View Items
- Add Character
- Delete Character
- Exit
The problem I have happens after the program asks you which character you wish to delete and confirms that information.
The program will go through the file until it reaches the line that the character's name is on and will stop copying right there making it seem like it deletes the character. This works if the character you wish to "delete" is the last in the file.
The characters are put into the text file in this format.
Name: Character's Name
Intelligence: 11
Constitution: 12
Health: 34
Items: Rapier, Embroidered Robes, etc.
I would like for the program to be able to "delete" any character and all of its information from the text file. I believe this involves reading and copying the text file until it reaches the characters name than skipping that line and the next four informational lines before it continues its reading and copying. I am just not sure how I would skip the lines.
If someone can answer my question or redirect me to a page that can, it would be greatly appreciated.
Thank you,
Derek Tinkey
else if (input == 4) {
string delChar;
string confirm;
cout << "Which character do you wish to delete?" << endl;
getline(cin,delChar);
cout << "Are you sure?" << endl;
getline(cin, confirm);
if (confirm == "yes" || confirm == "Yes") {
string line;
ifstream in("characters.txt");
if(!in.is_open()) {
cout << "Input failed to open" << endl;
return 1;
}
ofstream out("temp.txt");
string charDel = "Name: " + delChar;
while( getline(in,line) ) {
if (line != charDel) {
out << line << endl;;
}
else {
break;
}
}
in.close();
out.close();
remove("characters.txt");
rename("temp.txt","characters.txt");
cout << "Character has been deleted."<<endl;
}
}