-
September 24th, 2012, 12:11 AM
#1
C++ search, delete/undelete, update functions
Code:
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <fstream>
using namespace std;
const char FILE_PATH[] = "E:\\iofile";
//------------------------------------------------------------------------------
struct Student
{
*** char names [ 16 ];
*** int other;
};
fstream MyFile ( FILE_PATH , ios :: binary | ios :: in | ios :: out );
void createFile();
void mainMenu();
void funcSwitch();
void addRecord();
int displayRecord();
void updateRecord();
void deleteRecord();
void printRecords();
int searchRecord();
//------------------------------------------------------------------------------
int main ( int argc, char* argv [] )
{
*** createFile();
*** mainMenu();
}
//------------------------------------------------------------------------------
void mainMenu()
{
*** cout << "Welcome to School Enrolment System" << endl << endl
*** *** * << "Please enter your selection: " << endl
*** *** * << "1.** Register - New Student (Add record)" << endl
*** *** * << "2.** Look Up - Existing Student (Display record)" << endl
*** *** * << "3.** Change Details (Update record)" << endl
*** *** * << "4.** Remove Student (Delete record)" << endl
*** *** * << "5.** Show all Students (Print all)" << endl
*** *** * << "6.** Exit " << endl << endl
*** *** * << "Selection: ";
*** funcSwitch();
}
//------------------------------------------------------------------------------
void funcSwitch()
{
** char choice;
*** choice = getch();
*** clrscr();
*** while ( choice != 54 )
** {
***** switch ( choice )
*** *** {
******** case '1' :
*********** addRecord();
*********** getch();
*** *** *** *** clrscr();
************** break;
******** case '2' :
*********** displayRecord();
*********** getch();
************** break;
******** case '3' :
*********** updateRecord();
*********** getch();
************** break;
******** case '4' :
*********** deleteRecord();
*********** getch();
************** break;
******** case '5' :
*********** printRecords();
*********** getch();
************** break;
******** case '6' :
*********** break;
******** default :
*** *** *** *** cout << choice << " is not a vaild choice" << endl << endl
*** *** *** *** *** * << "Press any key to choose again... ";
*********** getch();
*** *** *** *** clrscr();
***** }
***** mainMenu();
*** *** choice = getch();
*** *** clrscr();
** }
}
//------------------------------------------------------------------------------
void addRecord()
{
*** Student temp;
*** cout << "Welcome new Student!" << endl
*** *** * << "We're going to set you up with a new Enrolment..." << endl
*** *** * << "All we need is a few details, let's begin: " << endl;
*** cout << endl << "Please enter first name : ";
*** cin >> temp.names;
*** cout << endl << "Please enter your age : ";
*** cin >> temp.other;
*** MyFile.open ( FILE_PATH , ios :: binary | ios :: app | ios :: out );
*** MyFile.clear();
*** MyFile.write ( ( const char* ) &temp, sizeof ( Student ) );
*** MyFile.clear();
*** MyFile.close();
}
//------------------------------------------------------------------------------
int searchRecord()
{
*** return 0;
}
//------------------------------------------------------------------------------
int displayRecord()
{
*** Student temp;
*** int age;
*** int position;
*** cout << "Enter age of person you are seeking: ";
*** cin >> age;
*** clrscr();
*** MyFile.open ( FILE_PATH , ios :: binary | ios :: in );
*** MyFile.clear();
*** MyFile.seekp ( position * sizeof ( Student ), ios :: beg );
*** MyFile.read ( ( char* ) &temp, sizeof ( Student ) );
*** if ( age != temp.other )
*** {
*** *** cout << endl << "No record found. Press any key to continue...";
*** }
*** else
*** {
*** *** cout << "We found your record: " << endl << endl
*** *** *** * << "Name: " << temp.names << endl
*** *** *** * << "Age:* " << temp.other << endl;
*** }
*** *** *cout << endl << "Press any key to continue..." << endl;
*** *** *getch();
*** *** *clrscr();
*** *** *mainMenu();
*** *** *MyFile.close();
*** *** *return 0;
}
//------------------------------------------------------------------------------
void updateRecord()
{
*** Student temp;
*** time_t t;
*** char choice;
*** int position;
*** position = displayRecord ();
*** MyFile.open( FILE_PATH, ios :: binary | ios :: out );
*** MyFile.clear();
*** if( position != -1 )
*** {
*** *** cout << endl << "Do you want to Update this Record( y or n )";
*** *** choice = getch();
*** }
*** if( choice == 'y' )
*** {
*** *** cout << endl << "Enter name : ";
*** *** cin >> temp.names;
*** *** cout << endl << "And your age : ";
*** *** cin >> temp.other;
*** *** MyFile.open( FILE_PATH, ios :: binary | ios :: out );
*** *** MyFile.clear();
*** *** MyFile.seekp( position * sizeof( Student ), ios :: beg );
***** MyFile.clear();
*** *** MyFile.write( ( const char* ) &temp, sizeof( Student ) );
***** MyFile.close();
*** }
}
//------------------------------------------------------------------------------
void deleteRecord()
{
*** Student temp;
*** char choice;
*** int position;
*** -MyFile.open( FILE_PATH, ios :: binary | ios :: in | ios :: out );
*** MyFile.clear();
*** MyFile.seekp( position * sizeof( Student ), ios :: beg );
*** MyFile.read( ( char* )&temp, sizeof( Student ) );
*** cout << endl << endl;
*** cout << "Would you like to delete this record? Yes (y) or No (n): ";
*** cin >> choice;
*** choice = getch();
*** MyFile.seekp( position * sizeof( Student ), ios :: beg );
*** MyFile.write( ( const char* )&temp, sizeof( Student ) );
*** getch();
*** clrscr();
*** mainMenu();
*** MyFile.close();
}
//-----------------------------------------------------------------------------
void printRecords()
{
*** Student temp;
*** MyFile.open ( FILE_PATH , ios :: binary | ios :: in );
*** MyFile.clear();
*** MyFile.read ( ( char* ) &temp, sizeof ( Student ) );
*** while ( ! MyFile.eof() )
*** {
*** *** cout << "Name: " << temp.names << endl
*** *** *** * << "Age:* " << temp.other << endl << endl;
*** *** MyFile.read ( ( char* ) &temp, sizeof ( Student ) );
*** }
*** cout << endl << "Press any key to continue back to main menu" << endl;
*** getch();
*** clrscr();
*** mainMenu();
*** MyFile.close();
}
//------------------------------------------------------------------------------
void createFile()
{
*** if( ! MyFile )
*** {
*** *** MyFile.close();
*** *** MyFile.open(FILE_PATH, ios::binary | ios::in | ios::out);
*** }
*** if ( ! MyFile )
*** {
*** *** cout << "File Error: File does not exist. Creating new file..."
*** *** *** * << endl << "Click to continue to main menu...";
*** *** getch();
*** *** clrscr();
*** }
}
Basically the program is a database of student enrollments.
I cannot get the the search delete/undelete and update functions to work and need help asap. Need to implement a bool field to the delete / undelete
Please help
-
September 24th, 2012, 12:45 AM
#2
Re: C++ search, delete/undelete, update functions
First, indent your code properly. This will make your code more readable, helping you to trace the flow of control through your program.
Then, you should tell us exactly how do the functions fail to work.
-
September 24th, 2012, 03:15 AM
#3
Re: C++ search, delete/undelete, update functions
Code:
//------------------------------------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <fstream>
using namespace std;
const char FILE_PATH[] = "E:\\iofile";
//------------------------------------------------------------------------------
struct Student
{
char names [ 16 ];
int other;
};
fstream MyFile ( FILE_PATH , ios :: binary | ios :: in | ios :: out );
void createFile();
void mainMenu();
void funcSwitch();
void addRecord();
int displayRecord();
void updateRecord();
void deleteRecord();
void printRecords();
int searchRecord();
//------------------------------------------------------------------------------
int main ( int argc, char* argv [] )
{
createFile();
mainMenu();
}
//------------------------------------------------------------------------------
void mainMenu()
{
cout << "Welcome to School Enrolment System" << endl << endl
<< "Please enter your selection: " << endl
<< "1. Register - New Student (Add record)" << endl
<< "2. Look Up - Existing Student (Display record)" << endl
<< "3. Change Details (Update record)" << endl
<< "4. Remove Student (Delete record)" << endl
<< "5. Show all Students (Print all)" << endl
<< "6. Exit " << endl << endl
<< "Selection: ";
funcSwitch();
}
//------------------------------------------------------------------------------
void funcSwitch()
{
char choice;
choice = getch();
clrscr();
while ( choice != 54 )
{
switch ( choice )
{
case '1' :
addRecord();
getch();
clrscr();
break;
case '2' :
displayRecord();
getch();
break;
case '3' :
updateRecord();
getch();
break;
case '4' :
deleteRecord();
getch();
break;
case '5' :
printRecords();
getch();
break;
case '6' :
break;
default :
cout << choice << " is not a vaild choice" << endl << endl
<< "Press any key to choose again... ";
getch();
clrscr();
}
mainMenu();
choice = getch();
clrscr();
}
}
//------------------------------------------------------------------------------
void addRecord()
{
Student temp;
cout << "Welcome new Student!" << endl
<< "We're going to set you up with a new Enrolment..." << endl
<< "All we need is a few details, let's begin: " << endl;
cout << endl << "Please enter first name : ";
cin >> temp.names;
cout << endl << "Please enter your age : ";
cin >> temp.other;
MyFile.open ( FILE_PATH , ios :: binary | ios :: app | ios :: out );
MyFile.clear();
MyFile.write ( ( const char* ) &temp, sizeof ( Student ) );
MyFile.clear();
MyFile.close();
}
//------------------------------------------------------------------------------
int searchRecord()
{
return 0;
}
//------------------------------------------------------------------------------
int displayRecord()
{
Student temp;
int age;
int position;
cout << "Enter age of person you are seeking: ";
cin >> age;
clrscr();
MyFile.open ( FILE_PATH , ios :: binary | ios :: in );
MyFile.clear();
MyFile.seekp ( position * sizeof ( Student ), ios :: beg );
MyFile.read ( ( char* ) &temp, sizeof ( Student ) );
if ( age != temp.other )
{
cout << endl << "No record found. Press any key to continue...";
}
else
{
cout << "We found your record: " << endl << endl
<< "Name: " << temp.names << endl
<< "Age: " << temp.other << endl;
}
cout << endl << "Press any key to continue..." << endl;
getch();
clrscr();
mainMenu();
MyFile.close();
return 0;
}
//------------------------------------------------------------------------------
void updateRecord()
{
Student temp;
time_t t;
char choice;
int position;
position = displayRecord ();
MyFile.open( FILE_PATH, ios :: binary | ios :: out );
MyFile.clear();
if( position != -1 )
{
cout << endl << "Do you want to Update this Record( y or n )";
choice = getch();
}
if( choice == 'y' )
{
cout << endl << "Enter name : ";
cin >> temp.names;
cout << endl << "And your age : ";
cin >> temp.other;
MyFile.open( FILE_PATH, ios :: binary | ios :: out );
MyFile.clear();
MyFile.seekp( position * sizeof( Student ), ios :: beg );
MyFile.clear();
MyFile.write( ( const char* ) &temp, sizeof( Student ) );
MyFile.close();
}
}
//------------------------------------------------------------------------------
void deleteRecord()
{
Student temp;
char choice;
int position;
MyFile.open( FILE_PATH, ios :: binary | ios :: in | ios :: out );
MyFile.clear();
MyFile.seekp( position * sizeof( Student ), ios :: beg );
MyFile.read( ( char* )&temp, sizeof( Student ) );
cout << endl << endl;
cout << "Would you like to delete this record? Yes (y) or No (n): ";
cin >> choice;
choice = getch();
MyFile.seekp( position * sizeof( Student ), ios :: beg );
MyFile.write( ( const char* )&temp, sizeof( Student ) );
getch();
clrscr();
mainMenu();
MyFile.close();
}
//-----------------------------------------------------------------------------
void printRecords()
{
Student temp;
MyFile.open ( FILE_PATH , ios :: binary | ios :: in );
MyFile.clear();
MyFile.read ( ( char* ) &temp, sizeof ( Student ) );
while ( ! MyFile.eof() )
{
cout << "Name: " << temp.names << endl
<< "Age: " << temp.other << endl << endl;
MyFile.read ( ( char* ) &temp, sizeof ( Student ) );
}
cout << endl << "Press any key to continue back to main menu" << endl;
getch();
clrscr();
mainMenu();
MyFile.close();
}
//------------------------------------------------------------------------------
void createFile()
{
if( ! MyFile )
{
MyFile.close();
MyFile.open(FILE_PATH, ios::binary | ios::in | ios::out);
}
if ( ! MyFile )
{
cout << "File Error: File does not exist. Creating new file..."
<< endl << "Click to continue to main menu...";
getch();
clrscr();
}
}
//------------------------------------------------------------------------------
Last edited by stevensonnz; September 24th, 2012 at 07:40 PM.
-
September 24th, 2012, 04:09 AM
#4
Re: C++ search, delete/undelete, update functions
Originally Posted by stevensonnz
I cannot get the the search delete/undelete and update functions to work
If you wrote the program, then you had some sort of logic in mind to get the program to work. If that logic fails, then it's your job to debug your code using the debugger, and seeing where the flow of the program fails to follow the logic you had in mind. Then you fix the code so that the logic follows your original plans.
The bottom line is that you have to put the effort in debugging your own code -- if something doesn't work, then identify for yourself exactly what is not working. If you have done that and you still are having trouble, then tell us what line of code, what library function, etc. is doing something that you didn't expect. Posting code verbatim and basically asking "go debug this for me" isn't (or shouldn't be) the way it's done here.
Having said that, one thing that doesn't make sense:
Code:
MyFile.open( FILE_PATH, ios :: binary | ios :: out );
How can you update a record if you erase the file? Opening a file for output erases the original file. If you debugged your code, you would see that this will not work. From there, you would then go to your book and see how to open a file for r/w access, and change the code accordingly.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; September 24th, 2012 at 04:16 AM.
-
September 24th, 2012, 04:33 AM
#5
Re: C++ search, delete/undelete, update functions
Your code does not compile. So how could do something like
Originally Posted by stevensonnz
I cannot get the the search delete/undelete and update functions to work and need help asap. Need to implement a bool field to the delete / undelete
Victor Nijegorodov
-
September 24th, 2012, 07:40 PM
#6
Re: C++ search, delete/undelete, update functions
Try now it should compile
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|