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
|
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int get_double(double default_value);
int get_int(int default_value);
void write_report();
void read_report();
int main()
{
int choice;
do {
cout << "This is the Automotive report system. Press 1 to write. Press 2 to read. Press 0 to exit... ";
cin >> choice;
if (choice == 1) {
write_report();
}
else if (choice == 2) {
read_report();
}
} while (choice != 0);
return 0;
}
#define COL_WIDTH 80
int get_double(double default_value) {
char s[COL_WIDTH + 1];
cin.getline(s, COL_WIDTH);
if (strlen(s) == 0) {
return default_value;
}
return atof(s);
}
int get_int(int default_value) {
char s[COL_WIDTH + 1];
cin.getline(s, COL_WIDTH);
if (strlen(s) == 0) {
return default_value;
}
return atoi(s);
}
void write_report() {
char model[20];
char make[20];
char year[5];
int mileage = 0;
double value = 0;
int rcn = 0;
int recsize = sizeof(model) + sizeof(int);
fstream automobile("automobile.data", ios::binary | ios::out);
cout << "Enter a record number: ";
rcn = get_int(0);
cout << "Enter the model of the car: ";
cin.getline(model, sizeof(model) - 1);
cout << "Enter the make of the car: ";
cin.getline(make, sizeof(make) - 1);
cout << "Enter the year of the car: ";
cin.getline(year, sizeof(year));
cout << "Enter the mileage on the car: ";
mileage = get_int(0);
cout << "Enter the value of the car: ";
value = get_double(0);
automobile.seekp(rcn*recsize);
automobile.write(model, sizeof(model) - 1);
automobile.write(make, sizeof(make) - 1);
automobile.write(year, sizeof(year) - 1);
automobile.write((char*)(&mileage), sizeof(int));
automobile.write((char*)(&value), sizeof(double));
automobile.close();
}
void read_report() {
char model[20];
char make[20];
char year[5];
int mileage = 0;
double value = 0;
int rcn = 0;
int recsize = sizeof(model) + sizeof(int);
fstream automobile("automobile.data", ios::binary | ios::in);
cout << "Enter a record number" << endl;
rcn = get_int(0);
automobile.seekp(rcn*recsize);
automobile.read(model, sizeof(model) - 1);
automobile.read(make, sizeof(make) - 1);
automobile.read(year, sizeof(year) - 1);
automobile.read((char*)(&mileage), sizeof(int));
automobile.read((char*)(&value), sizeof(double));
cout << "The model is: " << model << endl;
cout << "The make is: " << make << endl;
cout << "The year is: " << year << endl;
cout << "The milage is: " << mileage << endl;
cout << "Value of car is: " << value << endl;
automobile.close();
}
|