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.
// 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];
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;
}
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.