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
|
Header:
#ifndef RECORDFILE_H
#define RECORDFILE_H
#include <string>
using namespace std;
//****************************************************************************************************
class Records
{
private:
string studentName;
double *quiz;
double *assignment;
int weeks;
public:
void load(ifstream& inf);
void store(ofstream& of);
Records ();
void setData ( string, int, double[], double[]);
~Records ();
};
#endif
main:
#include "RecordFile.h"
#include <string>
#include <iostream>
#include <fstream>
//****************************************************************************************************
Records::Records ()
{
studentName = "";
weeks = 0;
quiz = NULL;
assignment = NULL;
}
//****************************************************************************************************
void Records::setData( string name, int size, double q[], double a[])
{
studentName = name;
weeks = size;
quiz = new double [size];
assignment = new double[size];
for (int i = 0; i < weeks; i++)
{
assignment[i] = a[i];
quiz[i] = q[i];
}
}
//****************************************************************************************************
void Records::store(ofstream& of)
{
double temp;
of.write (reinterpret_cast < char* > (&studentName), sizeof(studentName));
of.write (reinterpret_cast < char* > (&weeks), sizeof(weeks));
for( int i = 0; i < weeks; i++)
{
temp = assignment[i];
of.write (reinterpret_cast < char*> (&temp), sizeof (temp));
temp = quiz[i];
of.write (reinterpret_cast < char*> (&temp), sizeof (temp));
}
}
//****************************************************************************************************
void Records::load(ifstream& inf)
{
double temp;
inf.read (reinterpret_cast < char* > (&studentName), sizeof(studentName));
inf.read (reinterpret_cast < char* > (&weeks), sizeof(weeks));
for( int i = 0; i < weeks; i++)
{
temp = assignment[i];
inf.read (reinterpret_cast < char*> (&temp), sizeof (temp));
temp = quiz[i];
inf.read(reinterpret_cast < char*> (&temp), sizeof (temp));
}
}
//****************************************************************************************************
Records::~Records()
{
}
//****************************************************************************************************
driver:
#include "RecordFile.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
//****************************************************************************************************
int main()
{
Records record;
char selection;
string name;
double *q = nullptr ;
double *a = nullptr ;
int weeks;
Records ();
cout<<"\n Menu";
cout<<"\n========";
cout<<"\n A - Add records to file";
cout<<"\n B - Display all records";
cout<<"\n D - Quit";
cout<<"\n Enter selection: ";
cin>>selection;
switch(selection)
{
case 'A' :
{
cout << "Enter the student's name" << endl;
cin >> name;
cout << "How many assignments and quizes have you had so far?" << endl;
cin>> weeks;
for (int i = 0; i > weeks; i++)
{
cout << "enter assingment score" << endl;
cin >> a[i];
cout << "enter quiz score" << endl;
cin >> q[i];
}
record.setData ( name, weeks, a, q);
ofstream myfile;
myfile.open("Records.dat", ios::binary | ios::out);
record.store (myfile);
myfile.close();
}
case 'B' :
break;
case 'D' :
{
break;
}
}
return 0;
}
|