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.

  1. View Characters
  2. View Items
  3. Add Character
  4. Delete Character
  5. 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;
	}
}

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.

Simply don't write the lines to the new file. That's it.

Simply don't write the lines to the new file. That's it.

Ok, I understand that, but how do I skip over the 5 lines included in the character's information during the loop and continue running it afterward?

Commenting out line 25,26,27 should do it. (else part)

The only reason I have the else part there is to make it sort of look like its deleting something. If i didn't have it there, the only like that would be "deleted" would be the

Name: Character Name

The text file would then look like this:

Name: Somenamehere
Intelligence: 12
Constitution: 11
Health: 32
Items: Rapier

***this line deleted***
Intelligence: 13
Constitution: 15
Health: 40
Items: Long sword

I want all of the character's info to be gone as well. And the way I have it right now would do that but it would only work if the character is the last one in the text file. is there anyway to code it so the program "deletes" everything except the selected character and all of its stats. By deletes I mean not copied into a new file which is then renamed and becomes the original file.

After finding the matching name, why don't you continue searching the file until you see the next "Name", ignoring the lines in between.

Simply don't write the lines to the new file. That's it.

Ok, I understand that, but how do I skip over the 5 lines included in the character's information during the loop and continue running it afterward?

You obviously don't understand...

You don't skip anything.
You read and write everything up to the name you want to delete.
Now read but don't write until you've read up to the next name.
Now continue reading and writing.

Ok. How do you suppose I do that? Possibly an example?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.