Implement a function called "LoadDatabase(...)", in which you open the file "studentrecord.txt" (provided in Lab 3) and read the file to populate the first N elements (the file initially contains 10 records, but the contents will be changed by your program --- keep reading) of the array you defined, using the code you developed in Lab 7. Note this function should be called by your main function upon starting.
Your program should then becomes menu-driven. Your menu should look like this.
Program Name/Your Name/Copyright info here
Please choice:
(1) Add a student record
(2) Delete a student record
(3) Find a student's information
(4) Display all information in the database
(5) Exit Program
For each menu item, call a function that implement the menu function. After finishing processing, display the menu again and wait for the next input.
Note:
(0) Your array initially has 10 student records from the file.
(1) When the user chooses option 1 (Add a student record), the program should read the info of a student (Name, ID, GPA) from keyboard to a StudentRec variable, and append it at the end of the array (hint: use a integer variable to track the index of the last record in the array and copy the new record to this location. Initially the index has a value of 10, since elements 0-9 are occupied. However, do not hard-code value 10 as the initial value, as the number of records in the file can be changed. You should count the number of records while reading the file, and use the count as the initial value of the index).
You can continue adding records, but your program should giving a warning and do not accept new entry if the number of records exceeds the array size (hint: how to test this? You don't want adding 90 records manually to see if it works! Instead, change the array size to, say, 12, and you only need to add two records before seeing the warning.)
(2) You do not need to implement option 2 (Delete a student record). Write an empty function that does nothing. It is called a "stud" function since it is only a placeholder. We will implement the function next week.
(3) For option 3 (Find a student's information), the program should asking the user to enter the student's ID. The program then uses the ID as the search key to perform a linear search on the array. If the record is found, display all the info about the student (using code developed in Lab 7). Otherwise display "Student Record Not Found".
(4) For option 4 (Display all information in the database), using the code you developed in Lab 7 to display the entire database.
(5) When user chooses option 5 (exit), the program should call a function called "SaveDatabase(...)", in which you save all the records in the array back to the file "studentrecord.txt", so your newly added records will not be lost --- Next time you starts the program again, the newly added records will automatically be loaded! The program then terminates.
This is what I have done so far. Please help me to finish this program
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;
void display();
void addrecord();
void deleterecord();
void info();
void display();
int main()
{
ifstream infile;
infile.open("StudentRecords.txt");
if(!infile) //File could not be opened
{cout << "Error: The file could not be opened.\n";
exit(0);
}
char StudentRec[100];
int userinput;
do{
cout << "Please choose:\n";
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";
int x=-1;
cin >> x;
switch (x)
{
case 1: {
if (x<0)
{
cout << "Error in input.";
exit(0);
}
cout << addrecord << endl;
break;
}
case 2: {
if (x<0)
{
cout << "Error in input.";
exit(0);
}
cout << deleterecord << endl;
break;
}
case 3: {
if (x<0)
{
cout << "Error in input.";
exit(0);
}
cout << info << endl;
break;
}
case 4: {
if (x<0)
{
cout << "Error in input.";
exit(0);
}
display();
break;
}
;
default:
cout << "Improper Input";
break;
};
}
while(userinput!=5);
return 0;
}
void display(){
int x;
fstream infile;
infile.open("StudentRecords.txt");
if(!infile) //File could not be opened
{cout << "Error: The file could not be opened.\n";
exit(0);
}
infile >> x;
}
This post has been edited by JackOfAllTrades: 03 March 2010 - 05:52 AM
Reason for edit:: Added code tags. PLEASE!!! [code]...PUT YOUR CODE IN HERE!!!...[/code]

New Topic/Question
Reply
MultiQuote








|