Code:
#include <fstream>
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <string>
#include <ctime>
#include <sstream>
#include <string>
using namespace std;
void SetCursorPosition(int X, int Y)
{
COORD XY = { Y,X };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),XY);
}
void SetTextColor(int Color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),Color);
}
void PauseTheScreen(int x, int y, int Color)
{
SetCursorPosition(x,y);
SetTextColor(Color);
system("pause");
}
void ClearLine (int LineNumber)
{
SetCursorPosition(LineNumber ,0);
for ( int k = 0; k < 80; k++)
cout << " ";
}
void ClearTheScreen()
{
system("cls");
}
void PauseTheScreen()
{
system("pause");
}
void Heading()
{
ClearTheScreen();
SetCursorPosition(0,20);
SetTextColor(11);
cout << "VENTURA COUNTY COMMUNITY COLLEGE DISTRICT";
SetCursorPosition(0,20);
SetCursorPosition(1,32);
cout << "Ventura College";
SetCursorPosition(3,37);
cout << "CS V30";
SetCursorPosition(4,33);
cout << "Beginning C++";
SetCursorPosition(6,39);
cout << "by";
SetCursorPosition(7,33);
SetTextColor(11);
cout << "blah";
SetCursorPosition(8,8);
SetTextColor(14);
}
fstream ReadWrite;
char Buffer[65];
char SocialSecurityNumber[12];
char FirstName[16];
char MiddleInitial[2];
char LastName[21];
char DateOfBirth[11];
char State[3];
char ZipCode[6];
void OpenFile()
{
ReadWrite.open("Records.txt",ios::in | ios::out | ios::app);
}
void CloseFile()
{
ReadWrite.close();
}
void FormatData()
{
unsigned int k;
for ( k = 0; k < 65; k++)
{
Buffer[k] = 32; // 32 is the ASCII code for space
}
Buffer[64] = '\0';
for ( k = 0; k < strlen(SocialSecurityNumber); k++)
{
Buffer[k] = SocialSecurityNumber[k];
}
for ( k = 0; k < strlen(FirstName); k++)
{
Buffer[k+11] = FirstName[k];
}
for ( k = 0; k < strlen(MiddleInitial); k++)
{
Buffer[k+26] = MiddleInitial[k];
}
for ( k = 0; k < strlen(LastName); k++)
{
Buffer[k+27] = LastName[k];
}
for ( k = 0; k < strlen(DateOfBirth); k++)
{
Buffer[k+47] = DateOfBirth[k];
}
for ( k = 0; k < strlen(State); k++)
{
Buffer[k+57] = State[k];
}
for ( k = 0; k < strlen(ZipCode); k++)
{
Buffer[k+59] = ZipCode[k];
}
}
void ExplodeData()
{
Heading();
int k;
for (k = 0; k < 11 ; k++)
{
SocialSecurityNumber[k] = Buffer[k];
}
SocialSecurityNumber[11] = '\0';
for ( k = 0; k < 15 ; k++)
{
FirstName[k] = Buffer[k+11];
}
FirstName[15] = '\0';
for ( k = 0; k < 1 ; k++)
{
MiddleInitial[k] = Buffer[k+26];
}
MiddleInitial[1] = '\0';
for ( k = 0; k < 20 ; k++)
{
LastName[k] = Buffer[k+27];
}
LastName[20] = '\0';
for ( k = 0; k < 10 ; k++)
{
DateOfBirth[k] = Buffer[k+47];
}
DateOfBirth[10] = '\0';
for ( k = 0; k < 2 ; k++)
{
State[k] = Buffer[k+57];
}
State[2] = '\0';
for ( k = 0; k < 5 ; k++)
{
ZipCode[k] = Buffer[k+59];
}
ZipCode[5] = '\0';
SetTextColor(10);
SetCursorPosition(10,24);
cout << "SocialSecurity : " << SocialSecurityNumber;
SetCursorPosition(28,24);
SetCursorPosition(12,24);
SetTextColor(10);
cout << "FirstName : " << FirstName;
SetCursorPosition(28,24);
SetCursorPosition(14,24);
SetTextColor(10);
cout << "Middle Initial : " << MiddleInitial;
SetCursorPosition(28,24);
SetCursorPosition(16,24);
SetTextColor(10);
cout << "Last Name : " << LastName;
SetCursorPosition(28,24);
SetCursorPosition(18,24);
SetTextColor(10);
cout << "DateOfBirth : " << DateOfBirth;
SetCursorPosition(28,24);
SetCursorPosition(20,24);
SetTextColor(10);
cout << "State : " << State;
SetCursorPosition(28,24);
SetCursorPosition(22,24);
SetTextColor(10);
cout << "ZipCode : " << ZipCode;
SetCursorPosition(28,24);
SetCursorPosition(24,24);
SetTextColor(10);
PauseTheScreen();
ClearTheScreen();
}
// Bonus
bool isValidSsn()
{
for(int index = 0; index < 11; index++)
{
if(index == 3|| index == 6)
{
if(SocialSecurityNumber[index] != '-')
return false;
}
else
{
if(SocialSecurityNumber[index] < '0' || SocialSecurityNumber[index] > '9')
{
return false;
}
}
}
return true;
}
bool isValidDateofBirth()
{
for(int index = 0; index < 10; index++)
{
if(index == 2|| index == 5)
{
if(DateOfBirth[index] != '/')
return false;
}
else
{
if(DateOfBirth[index] < '0' || DateOfBirth[index] > '9')
{
return false;
}
}
}
int month = (DateOfBirth[0] - '0') * 10 + (DateOfBirth[1] - '0');
int day = (DateOfBirth[3] - '0') * 10 + (DateOfBirth[4] - '0');
if(month < 1 || month > 12)
return false;
if(day < 1 || day > 31)
return false;
if (month ==2)
{
int year = (DateOfBirth[6] - '0');
year = year * 10 + (DateOfBirth[7] - '0');
year = year * 10 + (DateOfBirth[8] - '0');
year = year * 10 + (DateOfBirth[9] - '0');
bool leapyear = year % 4 == 0;
if(leapyear)
{
if ( day > 29 )
return false;
}
else
{
if ( day > 28 )
return false;
}
}
else if( month == 4 || month == 6 || month == 9 || month == 11 )
{
if ( day > 30 )
return false;
}
else
{
if ( day > 31 )
return false;
}
return true;
}
void ReadData()
{
int NumberOfRecords,LengthOfFile,k;
OpenFile();
ReadWrite.seekg(0,ios::end);
LengthOfFile = ReadWrite.tellg();
NumberOfRecords = LengthOfFile/65;
for ( k = 0; k < NumberOfRecords; k++)
{
ReadWrite.seekg( k * 65,ios::beg);
ReadWrite.read(Buffer,65);
ExplodeData();
}
CloseFile();
}
void WriteData()
{
int AddMore = 1;
while ( AddMore )
{
Heading();
bool breakToEnd = false;
do {
SetCursorPosition(14,14);
SetTextColor(12);
cout << "Social Security Number : ";
SetCursorPosition(14,39);
cin>>SocialSecurityNumber;
if ( SocialSecurityNumber[0] == 'Z' && SocialSecurityNumber[1] == 'Z' && SocialSecurityNumber[2] == 'Z' )
{
ClearTheScreen();
Heading();
AddMore = 0;
breakToEnd = true;
break;
}
ClearLine(15);
if(!isValidSsn())
{
ClearLine(14);
SetCursorPosition(14, 40);
cout << "ERROR";
}
}
while(!isValidSsn());
if(breakToEnd)
break;
SetCursorPosition(16,14);
SetTextColor(12);
cout << "First Name : ";
SetCursorPosition(18,14);
cout << "Middle Initial : ";
SetTextColor(12);
SetCursorPosition(20,14);
cout << "Last Name : ";
SetTextColor(12);
SetCursorPosition(16,39);
cin >> FirstName;
SetCursorPosition(18,39);
cin >> MiddleInitial;
SetCursorPosition(20,39);
cin >> LastName;
do {
SetCursorPosition(22,14);
cout << "Date Of Birth : ";
SetTextColor(12);
SetCursorPosition(22,39);
cin >> DateOfBirth;
ClearLine(23);
if(!isValidDateofBirth())
{
ClearLine(22);
SetCursorPosition(22,30);
cout << "ERROR";
}
}
while(!isValidDateofBirth());
SetCursorPosition(24,14);
cout << "State : ";
SetTextColor(12);
SetCursorPosition(26,14);
cout << "Zip Code : ";
SetTextColor(12);
SetCursorPosition(24,39);
cin >> ZipCode;
SetCursorPosition(26,39);
cin >> State;
FormatData();
OpenFile();
ReadWrite.write(Buffer,65);
CloseFile();
}
}