Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing, ediiting from and to a file

Status
Not open for further replies.

TechMC

MIS
Dec 9, 2003
122
US
Im trying to edit and write to a file. I need some help. This is what i have so far. I want to be able to write to master.file and edit the records to master.file.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

void main_menu ();
void master_file_menu ();
void MF_input_menu ();
void MF_view_menu ();
void MF_edit_menu ();
void MF_delete_menu ();
void transaction_file_menu ();
void TF_input_menu ();
void TF_view_menu ();
void TF_edit_menu ();
void TF_delete_menu ();

char firstn;

void main (void)
{

main_menu ();

}

void main_menu ()
{
int choice;
cout <<endl;
cout <<&quot;\t\tMain Menu&quot;<<endl;
cout <<endl;
cout <<&quot;\tPress 1 (Master File)&quot;<<endl;
cout <<&quot;\tPress 2 (Transaction File)&quot;<<endl;
cout <<&quot;\tPress 3 (End Program)&quot;<<endl;
cout <<&quot;\tEnter choice: &quot;;
cin >>choice;
if (choice == 1)
master_file_menu ();
else if (choice == 2)
transaction_file_menu ();
else if (choice == 3)
return;
else
{
cout <<&quot;INVALID ENTRY&quot;<<endl;
main_menu ();
}

}

void master_file_menu ()
{
int choice;
cout <<endl;
cout <<&quot;\t\tMaster File Menu&quot;<<endl;
cout <<endl;
cout <<&quot;\tPress 1 (Input)&quot;<<endl;
cout <<&quot;\tPress 2 (View)&quot;<<endl;
cout <<&quot;\tPress 3 (Edit)&quot;<<endl;
cout <<&quot;\tPress 4 (Delete)&quot;<<endl;
cout <<&quot;\tPress 5 (Main Menu)&quot;<<endl;
cout <<&quot;\tPress 6 (End Program)&quot;<<endl;
cout <<&quot;\tEnter choice: &quot;;
cin >>choice;
if (choice == 1)
MF_input_menu ();
else if (choice == 2)
MF_view_menu ();
else if (choice == 3)
MF_edit_menu ();
else if (choice == 4)
MF_delete_menu ();
else if (choice == 5)
main_menu ();
else if (choice == 6)
return;
else
{
cout <<&quot;INVALID ENTRY&quot;<<endl;
main_menu ();
}
}

void transaction_file_menu ()
{
int choice;
cout <<endl;
cout <<&quot;\t\tTransaction File Menu&quot;<<endl;
cout <<endl;
cout <<&quot;\tPress 1 (Input)&quot;<<endl;
cout <<&quot;\tPress 2 (View)&quot;<<endl;
cout <<&quot;\tPress 3 (Edit)&quot;<<endl;
cout <<&quot;\tPress 4 (Delete)&quot;<<endl;
cout <<&quot;\tPress 5 (Main Menu)&quot;<<endl;
cout <<&quot;\tPress 6 (End Program)&quot;<<endl;
cout <<&quot;\tEnter choice: &quot;;
cin >>choice;
if (choice == 1)
TF_input_menu ();
else if (choice == 2)
TF_view_menu ();
else if (choice == 3)
TF_edit_menu ();
else if (choice == 4)
TF_delete_menu ();
else if (choice == 5)
main_menu ();
else if (choice == 6)
return;
else
{
cout <<&quot;INVALID ENTRY&quot;<<endl;
main_menu ();
}
}
void MF_input_menu ()
{
char firstn[16];
char lastn[16];
int ssn;
char address1[20];
char address2[20];
int phone;

fstream infile;
fstream outfile;
infile.open (&quot;master.file&quot;,ios::in);
outfile.open (&quot;master.file&quot;,ios::eek:ut);


cout << &quot;Enter First Name: &quot;;
outfile << firstn;
cin >> firstn;

cout <<&quot;Enter Last Name: &quot;;
outfile <<&quot;Enter Last Name: &quot;;
cin >> lastn;

cout <<&quot;Enter Social Security Number: &quot;;
outfile << ssn;
cin >> ssn;

cout <<&quot;Enter Address Line 1: &quot;;
outfile <<&quot;Enter Address Line 1: &quot;;
cin >> address1;

cout <<&quot;Enter Address Line 2: &quot;;
outfile <<&quot;Enter Address Line 2: &quot;;
cin >> address2;

cout <<&quot;Enter Phone Number: &quot;;
outfile <<&quot;Enter Phone Number: &quot;;
cin >> phone;
outfile << endl;





}

void MF_view_menu ()
{

ifstream infile (&quot;master.file&quot;);



}

void MF_edit_menu ()
{
cout <<&quot;EDIT&quot;<<endl;
//want to be able to edit master.file from here!
}

void MF_delete_menu ()
{
fstream infile;
infile.open (&quot;master.file&quot;,ios::in);

}
void TF_input_menu ()
{
cout <<&quot;INPUT&quot;<<endl;
}

void TF_view_menu ()
{
cout <<&quot;VIEW&quot;<<endl;
}

void TF_edit_menu ()
{
cout <<&quot;EDIT&quot;<<endl;
}

void TF_delete_menu ()
{
cout <<&quot;DELETE&quot;<<endl;
}
 
whats the problem?

/Srikanth

What happens if you’re scared half to death twice?
 
The same problem:
I have a bug in me program... please help me....!!!!

Please learn how to use debuggers, and if you have a problem, put in the forum just the lines of code where the problem resides. They are usualy no more than five lines.

Ion Filipski
1c.bmp
 
Wow, what an interesting little program! Here are some problems I see that you should address:

1. Unintended recursion in main_menu() calling main_menu().
2. Buffer-overrun exploits in MF_input_menu() with unguarded input to fixed-size buffers.
3. Runaway if-else-if-else-if-else logic -- consider using &quot;switch&quot; or better yet look-up tables.
4. Total lack of comments -- write some before you forget what the code is supposed to do.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top