All pastes #1035446 Raw Edit

Stuff

public cpp v1 · immutable
#1035446 ·published 2008-06-01 01:23 UTC
rendered paste body
//---------------------------------------------------------------------------//Programmer:	 Jonathon Lewis 2701374//Date:         26 May 2008//Folder:		 H:\C++\Assignent 9\a09.cpp//Reference:    Assignment 9 ( IO file )//Description:  This program reads in and out from a file on a USB stick.//Guarantee:	 Program is not complete.//---------------------------------------------------------------------------#include <iostream>#include <iomanip>#include <fstream>#include <conio.h>#include <ctime>#include <windows.h>using namespace std;#pragma hdrstopstruct Account{	 char first[ 24 ];	 char last[ 24 ];	 int age;	 char accountType[ 12 ];	 float balance;	 bool active;	 char time[ 28 ];};void menu();int printMenu( char );void addRecord();void displayAll();int search();int displayRecord( int );void deleteRecord();void updateRecord();void timeStamp( int );void sortRecords();void bubbleSort( Account[], int );char filePath[] = "H:\\testfile";char sortedFilePath[] = "H:\\sortedFile";fstream file( filePath, ios::binary | ios::in | ios::out );//---------------------------------------------------------------------------#pragma argsusedvoid main(int argc, char* argv[]){	menu();}//---------------------------------------------------------------------------//void menu()//Purpose:			Controls the switch for the program menu.//Inputs:			NULL//Outputs:			NULL//---------------------------------------------------------------------------void menu(){	char choice = 0;	int i;	char c = 0;	if ( !file )	{		cout << "File could not be found! A new file will be created." << endl			  << endl << "How many new records would you like to add?" << endl			  << "Number to add: ";		cin >> i;		fstream file( filePath, ios::binary | ios::out );		file.close();		for ( int j = 0; j < i; j++ )			addRecord();	}	while ( choice != 27 )	{		clrscr();		choice = printMenu( choice );		clrscr();		switch ( choice )		{			case '1':				while ( c != 'n' )				{					addRecord();					cout << endl << endl << "Your file has been saved!" << endl << endl						  << "Would you like to create another record? y/n" << endl;					c = getch();				}				break;			case '2':				displayAll();				break;			case '3':				c = search();				displayRecord( c );				system( "pause" );				break;			case '4':				updateRecord();				break;			case '5':				deleteRecord();				break;			case '6':				sortRecords();				break;			case '27':				file.close();				break;			default:				cout << "That is not a valid choice!"<< endl << endl << endl;		}	}}//---------------------------------------------------------------------------//int printMenu()//Purpose:			Prints the program menu.//Inputs:			NULL//Outputs:			char choice//---------------------------------------------------------------------------int printMenu( char choice ){	cout << "Please choose from the follwing options" << endl		  << "press the number of the choice you want:" << endl << endl		  << "1. Add a new record." << endl		  << "2. Display all records." << endl		  << "3. Display a record." << endl		  << "4. Update a record." << endl		  << "5. Delete a record." << endl		  << "6. Sort records into a new file." << endl		  << "ESC. Exit." << endl << endl << "Your choice was: " << choice;	choice = getch();	return choice;}//---------------------------------------------------------------------------//int addRecord()//Purpose:			Allows the user to add a record to the file.//Inputs:			NULL//Outputs:			int i//---------------------------------------------------------------------------void addRecord(){	Account temp;	file.open( filePath, ios::binary | ios::in | ios::out );	file.clear();	clrscr();	file.seekp( 0, ios::end );	cout << "Please enter first name of person: ";	cin >> temp.first;	cout << "Please enter last name of person: ";	cin >> temp.last;	cout << endl << endl << "Please enter age of person and press enter: ";	cin >> temp.age;	cout << endl << endl << "Please enter account type and press enter: ";	cin >> temp.accountType;	cout << endl << endl << "Please enter the balance of the account and"			  << "press enter: ";	cin >> temp.balance;	temp.active = true;	file.write( ( const char* ) &temp, sizeof( Account ) );	file.seekp( -sizeof( Account ), ios::cur );	timeStamp( file.tellg() / sizeof( Account ) );}//---------------------------------------------------------------------------//void displayAll ()//Purpose:			Displays all the records in the file.//Inputs:			NULL//Outputs:			NULL//---------------------------------------------------------------------------void displayAll(){	clrscr();	int i = 0;	int store;	Account temp;	file.clear();	file.seekp( i, ios::beg );	while ( file.peek() != EOF )	{		store = i / sizeof( Account );		displayRecord( store );		i += sizeof( Account );	}	getch();	clrscr();}//---------------------------------------------------------------------------//int search()//Purpose:			Searches for a particular string in the file.//Inputs:			NULL//Outputs:			int i / size of Account. OR -1//---------------------------------------------------------------------------int search(){	Account temp;	char searchName[ 20 ];	int i;	char c = 0;	char choice;	file.clear();	file.seekp( 0, ios::beg );	while( c != 'n' )	{		cout << "Type the first name of the record and press enter: ";		cin >> searchName;		clrscr();		i = 0;		file.clear();		file.seekp( i, ios::beg );		while ( strcmp( temp.first, searchName ) != 0 && file.peek() != EOF )		{			file.seekp( i, ios::beg );			file.read( ( char* ) &temp, sizeof( Account ) );			i += sizeof( Account );			if ( strcmp( temp.first, searchName ) == 0 )			{				cout << "A record has been found!" << endl					  << "Press enter to continue or 'n' to continue searching."					  << endl << endl << "Name: " << temp.first << " " << temp.last					  << endl << endl;				choice = getch();				if ( choice == 'n' )					strcpy( temp.first, "" );			}		}		if ( strcmp( temp.first, searchName ) != 0 )		{			cout << "There were no records found for " << searchName << "!"				  << endl << "Would you like to search again? y/n" << endl << endl;			c = getch();		}		else		{			file.seekp( -i , ios::cur );			return file.tellg() / sizeof( Account );		}		if( c == 'n' )			return -1;	}}//---------------------------------------------------------------------------//void deleteRecord()//Purpose:			Allows the user to delete a record.//Inputs:			NULL//Outputs:			NULL//---------------------------------------------------------------------------void deleteRecord(){	Account temp;	int i;	char choice;	file.clear();	i = search();	file.seekp( i, ios::beg );	if ( i != -1 )	{		clrscr();		file.seekp( i * sizeof( Account ), ios::beg );		file.read( ( char* ) &temp, sizeof( Account ) );		if ( temp.active == false )		{			cout << "This account is not active!" << endl				  << "Press enter to activate the account or ESC to"				  << "return to the main menu...";			choice = getch();			temp.active = true;		}		else		{			cout << "Press enter to deactivate account or ESC to" << endl				  << "return to the main menu..." << endl;			choice = getch();			temp.active = false;		}		if ( choice != 27 )		{			file.clear();			file.seekp( i * sizeof( Account ), ios::beg );			file.write( ( const char* ) &temp.active, sizeof( Account ) );			cout << endl				  << ( temp.active == false ? "Deactivation" : "Activation" )				  << " complete!" << endl << endl;			system( "pause" );			file.seekp( i * sizeof( Account ), ios::beg );			timeStamp( file.tellg() );		}	}}//---------------------------------------------------------------------------//void updateRecord()//Purpose:			Allows the user to update a particular record.//Inputs:			NULL//Outputs:			NULL//---------------------------------------------------------------------------void updateRecord(){	Account temp;	int i;	char c;	file.clear();	i = search();	displayRecord( i );	timeStamp( i );	cout << endl << endl << "Please enter the new details. . ." << endl << endl;	if ( i != -1 )	{		addRecord();		file.seekp( -sizeof( Account ), ios::cur );		timeStamp( file.tellg() / sizeof( Account ) );		cout << "Your record has been saved!" << endl << endl			  << "Press any key to return to the main menu.";		getch();	}}//---------------------------------------------------------------------------//int displayRecord()//Purpose:			Allows the user to search and display a single record.//Inputs:			NULL//Outputs:			int i//---------------------------------------------------------------------------int displayRecord( int placeHolder ){	Account temp;	file.clear();	if ( placeHolder != -1 )	{		file.seekp( placeHolder * sizeof( Account ), ios::beg );		file.read( ( char* ) &temp, sizeof( Account ) );		cout << setw( 10 ) << left << "Name: " <<  temp.first << " " << temp.last			  << endl << setw( 10 ) << left << "Age: "<< temp.age << endl			  << setw( 10 ) << left << "Type: " << temp.accountType << endl			  << setw( 10 ) << left << "Balance: " << temp.balance << endl			  << setw( 10 ) << left << "Active: "			  << ( temp.active == 1 ? "YES" : "NO" ) << endl << endl;	}	return placeHolder;}//---------------------------------------------------------------------------//int displayRecord()//Purpose:			Allows the user to search and display a single record.//Inputs:			NULL//Outputs:			int i//---------------------------------------------------------------------------void timeStamp( int placeHolder ){	Account temp;	file.clear();	time_t rawTime;	file.seekp( placeHolder, ios::beg );	file.read( ( char* ) &temp, sizeof( Account ) );	time( &rawTime );	strcpy( temp.time, ctime( &rawTime ) );	file.clear();	file.seekp( placeHolder, ios::beg );	file.write( ( const char* ) &temp, sizeof( Account ) );}//---------------------------------------------------------------------------//int displayRecord()//Purpose:			Allows the user to search and display a single record.//Inputs:			NULL//Outputs:			int i//---------------------------------------------------------------------------void sortRecords(){	 Account records[ 10 ];	 Account temp;	 int i = 0;	 file.clear();	 file.seekp( 0, ios::beg );	 while ( file.peek() != EOF )	 {		file.read( ( char* ) &temp, sizeof( Account ) );		records[ i ] = temp;		i++;	 }	 bubbleSort( records, 10 );}//---------------------------------------------------------------------------//int displayRecord()//Purpose:			Allows the user to search and display a single record.//Inputs:			NULL//Outputs:			int i//---------------------------------------------------------------------------void bubbleSort( Account records[], int size ){	int swaps;	int right;	int left;	Account temp;	do	{		swaps = 0;		left = 0;		right = 1;		while ( right < size )		{			if ( strcmp( records[ right ].first, records[ left ].first ) > 0 )			{				temp = records[ left ];				records[ left ] = records[ right ];				records[ right ] = temp;				swaps++;			}		}		left++;		right++;	}	while ( swaps > 0 );	file.close();	fstream sortedFile( sortedFilePath, ios::binary | ios::out );	sortedFile.open( sortedFilePath, ios::binary | ios::in | ios::out );	if ( ! sortedFile )	{		fstream sortedFile( sortedFilePath, ios::binary | ios::out );		sortedFile.open( sortedFilePath, ios::binary | ios::in | ios::out );	}	sortedFile.seekp( 0, ios::beg );	file.write( ( const char* ) &records, sizeof( Account ) );	sortedFile.seekp( 0, ios::beg );	while( file.peek() != EOF )	{		file.read( ( char* ) &temp, sizeof( Account ) );		cout << setw( 10 ) << left << "Name: " <<  temp.first << " " << temp.last			  << endl << setw( 10 ) << left << "Age: "<< temp.age << endl			  << setw( 10 ) << left << "Type: " << temp.accountType << endl			  << setw( 10 ) << left << "Balance: " << temp.balance << endl			  << setw( 10 ) << left << "Last modified on " << temp.time << endl			  << setw( 10 ) << left << "Active: ";		if ( temp.active == true )			cout << "YES" << endl << endl;		else			cout << "NO" << endl << endl;	}	if ( file.tellg() == 0 )		cout << "No records were found!";	cout << endl << endl << "Press any key to return to the main menu...";}//---------------------------------------------------------------------------