1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
#include<fstream>
#include<iomanip>
#include<iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;
struct StudentRec//Define structure for student recs
{
string Name;//holds name of the student
string ID;//holds the id
double GPA;//holds the gpa
};
int recordArray = 0;//This will track the number of records
const int ARRAY_SIZE =100;//Define the size of student records
StudentRec Records[ARRAY_SIZE]; //the number of student records
//boolean function that returns true or false representing that a record was read
bool read_a_record (StudentRec& Records, ifstream& myfile)
{
string firstName, lastName;//these two will hold the first and last name
string readstudentID;
double readstudentGPA;
//only activates if the file opens
if(myfile.is_open())
{
bool recordWasRead = myfile>>firstName>>lastName>>readstudentID>>readstudentGPA;//read the file
if(recordWasRead)
{
Records.Name = firstName+","+lastName;//add the two names to make a whole name
Records.ID = readstudentID;
Records.GPA = readstudentGPA;
cout<<Records.Name<<"\t"<<Records.ID<<"\t"<<Records.GPA<<"\n";
return true;
}
return false;
}
else
{
cout<<"File could not be opened.";
return false;
}
}
//loads the file for first use in the program
void LoadDatabase(ifstream& myfile)
{
int i=0;//will be the counter
if(myfile.is_open())//activates if it is open
{
while(read_a_record(Records[i],myfile))//while they can read
{
i++;//increment counter
recordArray++;//increament global counter
}
}
else
cout<<"Unable to open file.";
}
//function to save the new database
void saveDataBase(ifstream& myfile)
{
ofstream myfile2;//File to send data too
myfile2.open("StudentRecords2.txt");
int i = 0;
if(myfile2.is_open())
{
while(i<ARRAY_SIZE)//while the i is less than the number of records
{
if(i<ARRAY_SIZE)
{
if(read_a_record(Records[i],myfile))
{
//sends the name, id, and gpa aligned correctly
myfile2<<left<<setw(18)<<Records[i].Name;//saves the name to the file
myfile2<<left<<setw(10)<<Records[i].ID;//saves the id to the file
myfile2<<left<<setw(10)<<Records[i].GPA<<endl;//saves the gpa to the file
}
}
i++;
}
}
}
//function to get the record wanting to be inserted
void Insert_Record(StudentRec& Records, ifstream& myfile)
{
if(myfile.is_open())//activates if open
{
string fullName;//will hold the full name
string studentID;//will hold the id
double studentGPA;//will hold the gpa
//gets the user input of variables above
cout<<"\nWhat is the student's full name? ";
getline(cin,fullName);
getline(cin,fullName);
cout<<"\nWhat is the student's ID? ";
getline(cin,studentID);
cout<<"\nWhat is the student's GPA? ";
cin>>studentGPA;
cout<<endl;
//will go if the global is less then the size of the records
if (recordArray<ARRAY_SIZE)
{
Records.Name = fullName;//assign full name
Records.ID = studentID;//assign id
Records.GPA = studentGPA;//assign gpa
recordArray++;
}
else
cout<<"The records are full.";
}
else
{
cout<<"File cannot be opened.";
}
}
//will be implemented later
void Delete_Record()
{
}
//function will search through records
void Find_Record()
{
int i;//counter
string findID;//id that will found
//get user input for findID
cout<<"ID Search: ";
getline(cin,findID);
getline(cin,findID);
bool found = false;
//loads through the records for id
for(i=0;i<ARRAY_SIZE;i++)
{
if(Records[i].ID == findID)
{
//display student id
cout<<"\n";
cout<<"The student has been found.\n";
cout<<"Name: "<<Records[i].Name<<"\n";
cout<<"ID: "<<Records[i].ID<<"\n";
cout<<"GPA: "<<Records[i].GPA<<"\n";
cout<<"\n";
found = true;
break;//stop after found
}
}
if(found == false)
cout<<"No Record Found."<<"\n";
}
//prints a student record when called
void Print_Records(StudentRec Records)
{
//prints the name, id, and gpa aligned correctly
cout<<left<<setw(18)<<Records.Name;
cout<<left<<setw(10)<<Records.ID;
cout<<left<<setw(10)<<Records.GPA<<endl;
}
//display menu for the user
void displayMenu()
{
cout<<"Lab5B_AlvaWebster/Alva Webster/DONT YOU COPY THIS"<<endl<<endl;
cout<<"Please choice: "<<endl;
cout<<"(1) Add a student record"<<endl;
cout<<"(2) Delete a student record"<<endl;
cout<<"(3) Find a student's infomation"<<endl;
cout<<"(4) Display all information in the database"<<endl;
cout<<"(5) Exit Program"<<endl<<endl;
cout<<"CHOICE: ";
}
//finally the main function to call all functions
int main()
{
ifstream myfile;
myfile.open("StudentRecords.txt");
LoadDatabase(myfile);//load the file into the records
int choice;//assign the choice variable for the menu
do//do while statement that stops when the user chooses five
{
displayMenu();//call menu function to display inital menu
cin>>choice;//get user input for choice
//calls certain function depending on users choice
switch(choice)
{
//case 1, the user inserts a new record
case 1:
Insert_Record(Records[recordArray],myfile);
break;
//case 2, will delete a record, implement later
case 2:
Delete_Record();
break;
//case 3, find a record based off an id
case 3:
Find_Record();
break;
//case 4, display the records of a file.
case 4:
{
//Header for display
cout<<"No. Name ID GPA\n";
cout<<"----------------------------------------\n";
int rec_num=0;//keeps track oft the rec number
int i=0;//counter to loop through
while(i<ARRAY_SIZE)//while the i is less than the number of records
{
if(i<ARRAY_SIZE)
{
cout<<++rec_num<<"\t";//displays number of record
Print_Records(Records[i]);//print the record
}
else
break;
i++;
}
break;
}
case 5:
break;
default:
cout<<"Please enter a valid choice.\n\n";
}
}while(choice!=5);//if the choice is equal to five stop program
saveDataBase(myfile);
myfile.close();
cout<<"Good Bye!\n";
return 0;
}
|