-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVmake.cpp
95 lines (84 loc) · 2.17 KB
/
Vmake.cpp
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
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include "Vmake.h"
using namespace std;
const int MINCODE = 1;
const int MAXCODE = 51;
const int LENGTH = 11;
Vmake::Vmake() : _minCode(MINCODE), _maxCode(MAXCODE), _length(LENGTH)
{
char INFILE[] = "vmake.db";
ifstream inputFile;
inputFile.open(INFILE);
if(!inputFile)
{
cerr << "Problem opening input file: " << INFILE << endl;
}
else
{
inputFile.seekg(0);
inputFile.read((char *) & _numberVmakes, sizeof(_numberVmakes));
_pVmakes = new char[_numberVmakes * _length];
inputFile.seekg(_length);
inputFile.read(_pVmakes, _numberVmakes * _length);
inputFile.close();
_pVmake = new char[_length + 1];
}
}
Vmake::~Vmake()
{
delete[] _pVmakes;
delete[] _pVmake;
}
char * Vmake::GetVmake(int inVmakeCode) const
{
_pVmake[_length] = '\0';
memset(_pVmake, '*', _length);
if(inVmakeCode < _minCode || inVmakeCode > _maxCode)
return _pVmake;
strncpy(_pVmake, _pVmakes + ((inVmakeCode - _minCode)*_length), _length);
return _pVmake;
}
void Vmake::DisplayVmakes() const
{
const int NUMBERROWS = 13;
const int COLUMNWIDTH = 20;
const int BUFFERWIDTH = 90;
const int codeLength = 2;
char outputBuffer[NUMBERROWS][BUFFERWIDTH];
char singleData[_length + 1];
int columnPosition;
int currentColumn;
int currentRow;
int i;
if(_numberVmakes <= 0)
{
cerr << "Error! No Vehicl makes to Display!" << endl;
return;
}
currentRow = 0;
currentColumn = 0;
memset(outputBuffer, 0x20, sizeof(outputBuffer));
for(i = 0; i < _numberVmakes; i++)
{
columnPosition = currentColumn * COLUMNWIDTH;
snprintf(outputBuffer[currentRow] + columnPosition, codeLength + 1, "%02d", i + _minCode);
outputBuffer[currentRow][columnPosition + codeLength] = ' ';
strncpy(singleData, GetVmake(i + _minCode), _length);
strncpy(outputBuffer[currentRow] + (columnPosition + (codeLength + 1)),singleData, _length);
++currentRow;
if (currentRow >= NUMBERROWS)
{
currentRow = 0;
currentColumn++;
continue;
}
}
for(i = 0; i < NUMBERROWS; i++)
{
cout.write(outputBuffer[i], BUFFERWIDTH);
cout << "\n";
}
}