I have a difficulty for saving a file in C++.
I have an existing file which is opened by my program. The program should save the changes to the existing file (Example: D:\A4.txt) when I select 4 (as known as the menu). However, the program does not save the changes. No matter I add a new name or delete a name, the program doesn't save the changes.

Can someone tell me what the error?

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

//function forward declaration

void displayNames (); // function to display names
void addName ();
void removeName ();
void writeNames (string a);

vector<string> vecStudent;

ifstream inFile;
ofstream outFile;

int main()
{
	char cInput;
	string strFileName;
	

	string strFName, strLName;

	cout << "Please enter the data file name (with location): ";
	cin >> strFileName;

	//open input file
	inFile.open(strFileName.c_str());
	if (inFile.fail())
	{
		cout << "Input file failed to open!!" << endl;
		return -1;
	}

	// read file into vector of strings
	while (inFile >> strFName >> strLName)
		vecStudent.push_back(strFName+" "+strLName);
	inFile.close();

	while (true)
	{
			cout << "----------------------------------------" << endl;
			cout << "  Student Record  - Main Menu  " << endl;
			cout << "  Enter 1 to display ALL students  " << endl;
			cout << "  Enter 2 to add a student name  " << endl;
			cout << "  Enter 3 to delete a student name  " << endl;
			cout << "  Enter 4 to SAVE and quit the program  " << endl;
			cout << "----------------------------------------" << endl;
			cout << "  Enter menu option: ";
			cin >> cInput;

			switch (cInput)

			{
				case '1':
				displayNames(); // calls function to display names
					break;
				case '2':
				addName(); // calls function to add name
					break;
				case '3':
				removeName();
					break;
				case '4':
				writeNames(strFileName);
					return 0;
				default:
						cout << "Invalid input." << endl;
						break;
			}
	}
	return 0;
}

void displayNames()
{
	for (int i=0;i<vecStudent.size(); i++)
		cout << vecStudent[i] << endl;
}

void addName()
{
	string strFName, strLName;
	cout << "Enter a new name (First and Last Name): " <<endl;
	cin >> strFName >> strLName;
	vecStudent.push_back(strFName+" " +strLName);
	cout << "Name '" << strFName << " " << strLName << "' has been added." << endl;
}

void removeName()
{
	string strDelFirstName, strDelLastName;
	string strStudent;

	string strDelName;
	cout << "Enter the name to be deleted (First and Last Name): " << endl;
	cin >> strDelFirstName >> strDelLastName;

	strDelName = (strDelFirstName+" "+strDelLastName);

	if (strStudent == strDelName)
	{
		for (int i=0;i<vecStudent.size(); i++)
		{
		strStudent = vecStudent[i];
			vecStudent.erase (vecStudent.begin() +i);
			cout << "Name '" << strDelName << "' has been deleted." << endl;
		}
	}	
	else
			cout << "Name '" << strDelName << "' does not exist in file." << endl;
	}
	
void writeNames(string a)
{
	ofstream outFile;

	outFile.open(a.c_str());
	if (outFile.fail())
	{
		cout << "Output file error!" << endl;
	}

	for (int i=0; i<vecStudent.size(); i++)
	{
		outFile << vecStudent[i] << endl;
		outFile.close();
	}

	cout << "Thank you for using the program!" << endl;
}

Edited 5 Years Ago by chuyauchi: n/a

another bug for this program: This program cannot delete names.
When I enter the name I want to delete, the program shows "[the name I entered]" does not exist in file.

Could anyone help me to find the error?

Edited 5 Years Ago by chuyauchi: n/a

On line 130, you're closing the outfile on every loop.

Thanks, could you also help me to find the error for the removeName

in removeName, "strStudent" is empty on first evaluation and will only ever be modified if strDelName is also empty.

in removeName, "strStudent" is empty on first evaluation and will only ever be modified if strDelName is also empty.

how should I change it?

You will need to loop through/search into your container of students and compare them to the desired student to delete.

You could also have the user choose the student by the ID (rather than name) for easy searching and less typing.

This article has been dead for over six months. Start a new discussion instead.