Help with program (Arrays and data files)

Hi everyone. I'm having some trouble with my program involving arrays and data files. I have a text file called StudentRecords with:

Jone Adams M123456 3.65
Mike Smith M452355 2.65
Mary Ann M456712 4.00
Allen Brown M123456 3.65
Mike Davis M452355 2.65
Mary Wilson M456712 4.00

I am instructed to write functions that lets users add their own records. Im having two problems:
1. When I add a file using my add_record_function, 0.00 is written on a line by itself before the user input record for some reason.
2. If I do not add a record and hit 5 to exit program, my exit_program_function only saves 1 record.

Heres my code so far:


#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int menu_switch(int);
void LoadDatabase();
void add_record_function(int);
void delete_record_function();
void find_record_function(int);
void display_all_function(int);
void SaveDatabase(int);

// Declare the structure StudentRec
struct StudentRec
{
string Name; // Define Name as string
string ID; // Define ID as string
double GPA; // Define GPA as a double
};

// Define array with each element as a structure
const int array_size = 100;
StudentRec studentarray[array_size];

const int add_record = 1,
delete_record = 2,
find_record = 3,
display_all = 4,
exit_program = 5;

int count = 0;
int new_amount = count;


int main()
{
LoadDatabase();
int choice, loop_variable;

cout << "Please select a choice:\n";
do
{
cout << "(1) Add a student record\n";
cout << "(2) Delete a student record\n";
cout << "(3) Find a student's information\n";
cout << "(4) Display all information in the database\n";
cout << "(5) Exit Program\n";
cin >> choice;

loop_variable = menu_switch(choice);
}while(loop_variable == 1);

return 0;
}

void LoadDatabase()
{
ifstream inputfile;
inputfile.open("StudentRecords.txt");
string first_name, last_name;

while (count < array_size && inputfile >> first_name)
{
inputfile >> last_name; // Read last name
studentarray[count].Name = first_name + " " + last_name; // Set studentarray[count].Name equal to the first and last name
inputfile >> studentarray[count].ID; // Reads in ID
inputfile >> studentarray[count].GPA; // Reads in GPA
count++; // Increment variable count
}
}

int menu_switch(int choice)
{
switch (choice)
{
case add_record:
add_record_function(count);
return 1;

case delete_record:
delete_record_function();
return 1;

case find_record:
find_record_function(count);
return 1;

case display_all:
display_all_function(count);
return 1;

case exit_program:
SaveDatabase(new_amount);
return 0;

default:
cout << "You selected " << choice << " which is not a valid option. Please reselect a valid choice:\n";
return 1;
}
}

void add_record_function(int n)
{
new_amount = n + 1;
string first_name, last_name;
cout << new_amount;
cout << "Please input the student's first and last name seperated by a space: ";
cin >> first_name;
cin >> last_name;
studentarray[new_amount].Name = first_name + " " + last_name;
cout << "Please enter student ID number including M: ";
cin >> studentarray[new_amount].ID; // Reads in ID
cout << "Please input student's GPA (2 decimal places): ";
cin >> studentarray[new_amount].GPA; // Reads in GPA

}

void delete_record_function()
{
cout << "\nDelete function called.\n\n";
}

void find_record_function(int records)
{
string user_ID;
cout << "Please enter the ID to find information: ";
cin >> user_ID;
for(int x = 0; x < records; x++)
{
if (user_ID == studentarray[x].ID)
cout << studentarray[x].Name << "\t" << studentarray[x].ID << "\t" << fixed << setprecision(2) << studentarray[x].GPA << endl;
}
cout << endl;
}

void display_all_function(int num_records)
{
cout << endl;
for (int increment = 0; increment < num_records; increment++)
cout << increment + 1 << "\t" <<studentarray[increment].Name << "\t" << studentarray[increment].ID << "\t\t" << fixed << setprecision(2) << studentarray[increment].GPA << endl;
cout << endl;
}

void SaveDatabase(int new_amount)
{
ofstream outputfile("StudentRecords.txt");
for (int k = 0; k <= new_amount; k++)
outputfile << studentarray[k].Name << "\t" << studentarray[k].ID << "\t" << fixed << setprecision(2) << studentarray[k].GPA << endl;
outputfile.close();
}
I just fixed the second issue of the program only saving 1 file. I'm still not sure why its outputting a 0.00 to the text file though. Is it to signify the end of the array? Any help is appreciated.
Topic archived. No new replies allowed.