I've been getting the hang of this project my teacher gave me finally!
I was confused about how to open and close the iofile, but now I get that.
I'm only building the program with 2 things in the struct at the moment but later on I will update this...
The main problem I'm having is with the delete and update functions... I don't really understand the whole concept behind it... If anyone could help me with how these work that would be much appreciated
#include <iostream>
#include <conio.h>
#include <fstream>
using namespace std;
const char FILE_PATH[] = "D:\\iofile";
//------------------------------------------------------------------------------
struct Customer
{
char names [ 16 ];
int other;
};
void switchFoo();
void menu();
void addRec();
void updateRec();
void deleteRec();
void printAll();
void createFile();
int displayRec();
int searchRec( int );
fstream MyFile ( FILE_PATH, ios :: binary | ios :: out | ios :: in );
//------------------------------------------------------------------------------
int main ( int argc, char* argv [] )
{
createFile();
switchFoo();
return 0;
}
//------------------------------------------------------------------------------
void switchFoo()
{
char choice;
menu();
choice = getch();
while ( choice != 54 )
{
switch ( choice )
{
case '1' :
addRec();
break;
case '2' :
displayRec();
break;
case '3' :
updateRec();
break;
case '4' :
deleteRec();
break;
case '5' :
printAll();
break;
case '6' :
break;
default :
cout << choice << " is not a vaild choice" << endl
<< "\t\t Please try again: ";
getch();
}
menu();
choice = getch();
}
}
//------------------------------------------------------------------------------
void menu()
{
system("cls");
cout << "\t\t|--------------------------------------------------|" << endl
<< "\t\t| .::MAIN MENU::. |" << endl
<< "\t\t|--------------------------------------------------|" << endl
<< "\t\t| 1. Register New Account |" << endl
<< "\t\t| 2. Look Up Existing Customer |" << endl
<< "\t\t| 3. Deposit / Withdraw |" << endl
<< "\t\t| 4. Remove Account |" << endl
<< "\t\t| 5. Show All Accounts |" << endl
<< "\t\t| 6. Exit |" << endl
<< "\t\t|--------------------------------------------------|" << endl
<< "\t\t Selection: ";
}
//------------------------------------------------------------------------------
void addRec()
{
Customer temp;
system("cls");
cout << "\t\t|--------------------------------------------------|" << endl
<< "\t\t| .::NEW ACCOUNT MENU::. |" << endl
<< "\t\t|--------------------------------------------------|" << endl
<< "\t\t Welcome new customer! " << endl
<< "\t\t We're going to set you up with a new account... " << endl
<< "\t\t All we need is a few details, let's begin: " << endl;
cout << endl << "\t\t Enter first name : ";
cin >> temp.names;
cout << "\t\t And your age : ";
cin >> temp.other;
cout << endl << "\t\t Hello " << temp.names << ". " << endl
<< "\t\t We've now set up that account for you. " << endl
<< "\t\t Press any key to continue back to main menu...";
getch();
MyFile.open ( FILE_PATH , ios :: binary | ios :: app | ios :: out );
MyFile.clear();
MyFile.write ( ( const char* ) &temp, sizeof ( Customer ) );
MyFile.clear();
MyFile.close();
}
//------------------------------------------------------------------------------
void printAll()
{
Customer temp;
MyFile.open ( FILE_PATH , ios :: binary | ios :: in );
MyFile.clear();
MyFile.read ( ( char* ) &temp, sizeof ( Customer ) );
system("cls");
cout << "\t\t|--------------------------------------------------|" << endl
<< "\t\t| .::ALL ACCOUNTS MENU::. |" << endl
<< "\t\t|--------------------------------------------------|" << endl;
while ( ! MyFile.eof() )
{
cout << "\t\t| Name: " << temp.names << "\t\t\t\t\t |" << endl
<< "\t\t| Age: " << temp.other << "\t\t\t\t\t |" << endl
<< "\t\t --------------------------------------------------" << endl;
MyFile.read ( ( char* ) &temp, sizeof ( Customer ) );
}
if ( ! MyFile )
{
cout << "\t\t No records to show yet... ";
}
cout << endl << "\t\t Press any key to continue back to main menu...";
getch();
MyFile.close();
}
//------------------------------------------------------------------------------
int displayRec()
{
Customer temp;
int age;
int position;
system("cls");
cout << "\t\t|--------------------------------------------------|" << endl
<< "\t\t| .::ACCOUNT RECORDS::. |" << endl
<< "\t\t|--------------------------------------------------|" << endl;
cout << "\t\t Enter age of person you are seeking: ";
cin >> age;
position = searchRec ( age );
MyFile.open ( FILE_PATH , ios :: binary | ios :: in );
MyFile.clear();
MyFile.seekp ( position * sizeof ( Customer ), ios :: beg );
MyFile.read ( ( char* ) &temp, sizeof ( Customer ) );
if ( age != temp.other )
{
cout << endl << "\t\t No record found. Press any key to continue...";
}
else
{
cout << endl << "\t\t We found your record: " << endl << endl
<< "\t\t --------------------------------------------------" << endl
<< "\t\t| Name: " << temp.names << "\t\t\t\t\t |" << endl
<< "\t\t| Age: " << temp.other << "\t\t\t\t\t |" << endl
<< "\t\t --------------------------------------------------" << endl;
}
cout << endl << "\t\t Press any key to continue...";
getch();
MyFile.close();
}
//------------------------------------------------------------------------------
void deleteRec()
{
system("cls");
cout << "deleteRecord() called" << endl;
}
//------------------------------------------------------------------------------
void updateRec()
{
system("cls");
cout << "updateRecord() called" << endl;
}
//------------------------------------------------------------------------------
int searchRec( int age )
{
Customer temp;
int position;
int i = 0;
MyFile.open ( FILE_PATH , ios :: binary | ios :: in );
MyFile.clear();
MyFile.seekp ( 0 , ios :: beg );
MyFile.read ( ( char* ) &temp, sizeof ( Customer ) );
while ( ! MyFile.eof() )
{
if ( age == temp.other )
{
position = i;
}
i++;
MyFile.read ( ( char* ) &temp, sizeof ( Customer ) );
}
return position;
}
//------------------------------------------------------------------------------
//void createFile()
//Purpose: 0
void createFile()
{
if ( ! MyFile )
{
fstream MyFile ( FILE_PATH, ios :: binary | ios :: out );
}
MyFile.close();
MyFile.open ( FILE_PATH, ios :: binary | ios :: in | ios :: out );
MyFile.close();
if ( ! MyFile )
{
cout << "File Error: File does not exist. Creating new file..."
<< endl << "Click to continue to main menu...";
getch();
}
}
//-------------------------------------------------------------------------------
So far...

New Topic/Question
Reply



MultiQuote





|