C++ Help with File Pointer and search Functions

I am new to programming and on our current assignment we have to effectively create a database to store various pieces of information in a struct.

I have created so far an add file function(works well)
main()
print all to screen(works well) and I am now working on the display record function which utilizes a search function to display the records.

The problems I am having are my search Function prints the information I want twice to the console instead of just once. I also need to pass the information when found over to my display record function to be able to display it on the console screen.....

I am pretty desperate and have read as much as I can but the way our tutor makes us program is quite different from most of the resources I can find on the net and my understanding is still very limited.

My Code is as follows

#include <iostream>
#include<fstream>
#include <iomanip>
#include <conio.h>
#include <tchar.h>
#pragma hdrstop
#include <cstdlib>
#include <ctime>
using namespace std;

struct Record
{
char firstName [ 8 ];
int age;
};

void createFileOfRecords();
void addRecord();
void printAllRecords();
void search( char );
void displayRecord();
void deleteUndeleteRecord();
void updateRecord();
void switchFunction();
void printMenu();

const char FILE_PATH[] = "E:\\Parts";

fstream MyFile( FILE_PATH, ios :: binary | ios :: in | ios :: out );

//---------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
if ( ! MyFile )
createFileOfRecords();

MyFile.close();
switchFunction();

MyFile.close();
return 0;
}
//---------------------------------------------------------------------------
//void switchFunction();
//Purpose:
//Intput:
//Output:
//----------------------------------------------------------------------------
void switchFunction()
{

cout << endl << endl << "*** Welcome to the NZ Car Parts Filing Cabinet ***"
<< endl << endl;

char choice;

printMenu();
choice = toupper( getch() );

while ( choice != 27 )
{
switch (choice)
{
case 'A':
clrscr();
addRecord();

break;

case 'U' :
clrscr();
updateRecord();

break;

case 'X' :
clrscr();
deleteUndeleteRecord();

break;

case 'D' :
clrscr();
displayRecord();

break;

case 'P' :
clrscr();
printAllRecords();

break;

}
clrscr();
printMenu();
choice = toupper( getch() );

}

}
//----------------------------------------------------------------------------
//---------------------------------------------------------------------------
//void printMenu();
//Purpose:
//Intput:
//Output:
//---------------------------------------------------------------------------
void printMenu()
{
cout << "Please select 'A' to add a record"<< endl
<< "Please select 'U' to update a record"<< endl
<< "Please select 'X' to Delete or Undelete a record"<< endl
<< "Please select 'D' to display all records"<< endl
<< "Please select 'P' to print all records"<< endl
<< "or <ESC> key to quit";
}
//---------------------------------------------------------------------------
//void CreateFileOfRecords();
//Purpose:
//Intput:
//Output:
//----------------------------------------------------------------------------
void createFileOfRecords()
{
if ( ! MyFile )
fstream MyFile( FILE_PATH, ios :: binary | ios :: out );

MyFile.close();
}
//---------------------------------------------------------------------------
//void addRecord();
//Purpose:
//Intput:
//Output:
//----------------------------------------------------------------------------
void addRecord()
{
Record temp;

cout << endl<< endl << "*****Add A New Record*****" << endl << endl;

cout << "Please enter first name: ";
cin >> temp.firstName;
cout << "Please enter An Age: ";
cin >> temp.age;

MyFile.open( FILE_PATH, ios::binary | ios :: app );
MyFile.clear();
MyFile.write( ( const char* ) &temp, sizeof( Record ) );
MyFile.close();

clrscr();
cout << "*** Record entered succesfully ***" << endl
<< " Press enter to continue";

getch();
}
//---------------------------------------------------------------------------
//void search();

//Purpose:
//Intput:
//Output:
//----------------------------------------------------------------------------
int search( char itemToFind [])
{
Record temp;

MyFile.open( FILE_PATH, ios :: binary | ios :: in );
MyFile.clear();
MyFile.seekg( 0, ios :: beg );
MyFile.read(( char* ) &temp, sizeof( Record ) );

int i = 0;
while ( ! MyFile.eof() )
{
MyFile.read((char*) &temp, sizeof(Record));

if (strcmp( temp.firstName, itemToFind) != 0)
{
i++;
}
else
cout << temp.firstName << "\t" << temp.age << endl;
}

MyFile.close();
getch();
return -1;
}
//---------------------------------------------------------------------------
//void printAllRecords();

//Purpose:
//Intput:
//Output:
//----------------------------------------------------------------------------
void printAllRecords()
{
cout << endl<< endl << "*****All Records*****" << endl << endl;

Record temp;

MyFile.open( FILE_PATH, ios :: binary | ios :: in );
MyFile.clear();
MyFile.seekg( 0, ios :: beg );
MyFile.read(( char* ) &temp, sizeof( Record ) );


while ( ! MyFile.eof() )
{
cout << temp.firstName << "\t" << temp.age << endl;
MyFile.read((char*) &temp, sizeof(Record));
}
MyFile.close();
getch();
}
//---------------------------------------------------------------------------
//void deleteUndeleteRecord();

//Purpose:
//Intput:
//Output:
//----------------------------------------------------------------------------
void deleteUndeleteRecord()
{

clrscr();
cout << "You have selected the delete/Undelete Record Function " << endl;
switchFunction();
getch();
}
//---------------------------------------------------------------------------
//void updateRecord();
//Purpose:
//Intput:
//Output:
//----------------------------------------------------------------------------
void updateRecord()
{
clrscr();
cout << " You have selected the update Record Function update" << endl;
switchFunction();
getch();
}
//---------------------------------------------------------------------------
//void displayRecord()
//Purpose:
//Intput:
//Output:
//----------------------------------------------------------------------------
void displayRecord()
{
int returnFromFunction;
char itemToFind[ 12 ];
Record temp;
cout << "Please enter the record you would like to search: ";
cin >> itemToFind;

search( itemToFind );

MyFile.open( FILE_PATH, ios :: binary | ios :: in );
MyFile.read(( char* ) &temp, sizeof( Record ) );

returnFromFunction = search( itemToFind );

if ( returnFromFunction == -1 )
{
MyFile.seekp(0, ios :: beg);
cout << temp.firstName << temp.age;
cout << "boo";
}
else
cout << "No Records found press any key to continue";
getch();
}


//---------------------------------------------------------------------------
Last edited on
Rather than have each operation manipulate data in the file, it is better to read the file into memory and manipulate the memory representation, then write the file at the end.

You can then use conventional data structures to manage records.

Also, that will allow you to get rid of that global file thing. Never use global variables unless there's a very good reason to or there's no alternative.
Topic archived. No new replies allowed.