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
|
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
using namespace std;
char instr[81];
FILE *stream;
int getline(char*,int);
//double getline(string*,double);
class Custome {// The class
int jj;
public:
Custome(){
jj = 0.0;
}
Custome(int jj){
this->jj = jj;
}
void set_jj(){
getline(instr,81);
jj = atof(instr);
}
int get_jj(){return jj;}
void calc_jj_precent(){
}
~Custome(){};
};
class New : public Custome{
int account;
char* firstName;
char* lastName;
double minutes;
char* length;
double active;
char stat[81];
New(){};
New(int account, char* firstName, char* lastName, double minutes, char* length, double active, char* stat, int jj, double calc_jj_precent) : Custome(jj){
this->account=account;
std::strcpy(this->firstName,firstName);
this->minutes=minutes;
std::strcpy(this->length,length);
this->active=active;
std::strcpy(this->stat,stat);
}
void set_account(){
getline(instr,81);}
int get_account(){return account;}
void set_firstName(){
getline(firstName,81);}
char *get_firstName(){return firstName;}
void set_lastName(){
getline(lastName,81);}
char *get_lastName(){return lastName;}
void set_minutes(){
getline(instr,81);}
double get_minutes(){return minutes;}
void set_length(){
getline(length,81);}
char *get_length(){return length;}
void set_active(){
getline(instr,81);}
double get_active(){return active;}
void set_stat(){
getline(stat,81);}
char *get_stat(){return stat;}
double calc_jj_precent(){return (get_jj()/100.0);}
friend istream & operator >> (istream &in, New &New)
{
New.set_account();
New.set_firstName();
New.set_lastName();
New.set_minutes();
New.set_length();
New.set_active();
New.set_stat();
New.set_jj();
New.calc_jj_precent();
return in;
}//friend
friend ostream &operator<<(ostream &output, New &New){
output << "\nAccount " <<New.get_account();
output << "\nFirstnamed " <<New.get_firstName();
output << "\nLastnamed " <<New.get_lastName();
output << "\nMinutes " <<New.get_minutes();
output << "\nLength " <<New.get_length();
output << "\nActive " <<New.get_active();
output << "\nStat " <<New.get_stat();
output << "\nJj " <<New.get_jj()<<endl;
output << "\nJjPrecent " <<New.calc_jj_precent()<<endl;
return output;
}
void display();
~New(){};
};
void New::display(){
cout << "\nAccount: " << account;
cout << "\nFirstName: " << firstName;
cout << "\nLastName: " << lastName;
cout << "\nMinutes: " << minutes;
cout << "\nLength: " << length;
cout << "\nActive: " << active;
cout << "\nStat: " << stat;
cout << "\nJj: " << get_jj();
cout << "\nJjprecent: " << calc_jj_precent()<<endl;
}
int n,num_recs=0;
int main(void) {
// Create an instance of the Player class
New New(New.get_account(),New.get_firstName(),New.get_lastName(),New.get_minutes(),New.get_length(),New.active(),New.get_stat(),New.get_jj(),New.calc_jj_precent());
//Used to create the file initially.
stream = fopen("New.txt","w+b");
for(int i=0;i<3;i++){
cin >> New;
/*New.set_account();
New.set_firstName();
New.set_lastName();
New.set_minutes();
New.set_length();
New.set_active();
New.set_stat();
New.set_jj();
New.calc_jj_precent();
*/
//Write to the File
fseek(stream,i*sizeof(New),0);
fwrite(&New,sizeof(New),1,stream);
}
fclose(stream);
stream = fopen("New.txt","r+b");
for(;;){
//Read a record
n = fread(&New,sizeof(New),1,stream);
if(!n)break;
cout <<"\nPrinting output from New.Display() function:";
New.display();
cout <<"\n\nPrinting output from New friend function:";
cout << New;
num_recs++;
void pause();
{
cout << "\n PRESS ANY KEY TO CONTINUE.... \n";
}
}
cout << "\n\nThe number of records read from the file was: " << num_recs <<endl;
fclose(stream);
void pause();
{
cout << "\n PRESS ANY KEY TO CONTINUE.... \n";
}
}//main
| |