• 0

Friends Database


Question

In my Friend's Database program, I am using a structure to input their first and last name, phone numbers, and birth date. When I add a new friend to my text file, it is stored. Then when I go to add another friend and I go to my Display option, it always overwrites my old friends in the text file. What do I do to keep track to all the friends entered in and how do I prevent them from being overwritten?

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <conio.h>

using namespace std;
//Declaration of the Struct type
struct StudentType
{
	string First;
	string Last;
	string Phone;
	string Birth;
};

int choice = 0;

void Menu()
{
	cout << "Which of the following would you like to choose?" << endl;
	cout << "" << endl;
	cout << "1. Display" << endl;
	cout << "2. Add a Friend" << endl;
	cout << "3. Edit a Friend" << endl;
	cout << "4. Search" << endl;
	cout << "5. Remove Friend" << endl;
	cin >> choice;

}

void Display()
{	

	int Index = 0;
	int NumData = 1;
	vector <StudentType> Student(NumData);

	//Specifies the File that are used for input and output
	ifstream InFile("InData.txt");

	//Loads array from input file
	while(getline(InFile,Student[Index].First))
	{
		getline(InFile,Student[Index].Last);
		getline(InFile,Student[Index].Phone);
		getline(InFile,Student[Index].Birth);
		NumData++;

		Student.resize(NumData);
		Index++;
	}

	InFile.close();

	Student.resize(Student.size()-1);

	cout << endl;

	ofstream OutFile ("InData.txt");
	for (Index = 0; Index < Student.size(); Index++)
	{
		OutFile << Student[Index].First << endl;
		OutFile << Student[Index].Last << endl;
		OutFile << Student[Index].Phone << endl;
		OutFile << Student[Index].Birth << endl;
		cout << "Name: " << Student[Index].First << " " << Student[Index].Last << " " << Student[Index].Phone <<  " " << Student[Index].Birth << endl;
	}

}
void Process()
{
	int Index = 0;
	int NumData = 1;
	vector <StudentType> Student(NumData);


	cout << "Input First Name/Last Name/Phone Number/Birthdate" << endl;
	cin >> Student[Index].First;
	cin >> Student[Index].Last;
	cin >> Student[Index].Phone;
	cin >> Student[Index].Birth;


	//Diplays array and outputs it to outfile
	ofstream OutFile ("InData.txt");
	for (Index = 0; Index < Student.size(); Index++)
	{
		OutFile << Student[Index].First << endl;
		OutFile << Student[Index].Last << endl;
		OutFile << Student[Index].Phone << endl;
		OutFile << Student[Index].Birth << endl;
		cout << "Name: " << Student[Index].First << " " << Student[Index].Last << " " << Student[Index].Phone <<  " " << Student[Index].Birth << endl;
	}
}

int main()
{
	while (choice != -1)
	{
		Menu();
		system("cls");

		if (choice == 1)
		{
			Display();
			system("Pause");
			system("cls");
		}

		if (choice == 2)
		{
			Process();
			system("Pause");
			system("cls");
		}
	}
}

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.