Question

Code in C++: Please help me fix the error in function: void write_account(); //function to write...

Code in C++:

Please help me fix the error in function:

void write_account(); //function to write record in binary file (NOTE: I able to store data to a txt file but however the data get error when I try to add on data the second time)

void display_all(); //function to display all account details (NOTE: This function is to display all the info that save account.txt which is ID, Name, and Type)
void modify_account(int); //function to modify record of file (NOTE: This function is to modify and change the data in account.txt. Before user can access to the modify the program will check if the input ID match with any ID that had register in account.txt)
void delete_account(int); //function to delete record of file (NOTE: This function is to delete account from the account.txt)

//my code below:

//***************************************************************
// HEADER FILE USED IN PROJECT
//****************************************************************
#include<iostream>
#include <cstdlib>
#include<fstream>
#include<cctype>
#include<iomanip>
using namespace std;

//***************************************************************
// CLASS USED IN PROJECT
//****************************************************************


class account
{
public:
char id[10];
char name[10];
char type;

void create_account(); //function to get data from user
void show_account() const; //function to show data on screen
void modify(); //function to add new data
void report() const; //function to show data in tabular format
const char* retid() const; //function to return account number
int retid() const;   //function to return account number
char rettype() const; //function to return type of account
}; //class ends here

void account::create_account()
{
cout<<"\nEnter The Student ID No. :";
cin>>id;
cout<<"\n\nEnter Student Name (First and Last : ";
cin.ignore();
cin.getline(name,50);
cout<<"\nEnter Type of The account (T/S) : ";
cin>>type;
type=toupper(type);
cout<<"\n\n\nAccount Created..";
}

void account::show_account() const
{
cout<<"\nStudent ID No. : "<<id;
cout<<"\nStudent Name : ";
cout<<name;
cout<<"\nType of Account : "<<type;
}

const char* account::retid() const
{
return this->id;
}


void account::modify()
{
cout<<"\nStudent ID No. : "<<id;
cout<<"\nModify Student Name : ";
cin.ignore();
cin.getline(name,50);
cout<<"\nModify Type of Account : ";
cin>>type;
type=toupper(type);
}

void account::report() const
{
   cout<<id<<setw(10)<<" "<<name<<setw(10)<<" "<<type<<setw(6)<<endl;
}


int account::retid() const
{
   return id;
}

//***************************************************************
// function declaration
//****************************************************************
void write_account(); //function to write record in binary file
void display_all(); //function to display all account details
void modify_account(int); //function to modify record of file
void delete_account(int); //function to delete record of file
void intro(); //introductory screen function

//****************************************************************
// THE MAIN FUNCTION OF PROGRAM
//****************************************************************
int main()
{
char ch;
int num;
intro();
do
{
system("cls");
cout<<"\n\n\n\tMAIN MENU";
cout<<"\n\n\t01. NEW ACCOUNT";
cout<<"\n\n\t02. ALL ACCOUNT HOLDER LIST";
cout<<"\n\n\t03. CLOSE AN ACCOUNT";
cout<<"\n\n\t04. MODIFY AN ACCOUNT";
cout<<"\n\n\t05. EXIT";
cout<<"\n\n\tSelect Your Option (1-5) ";
cin>>ch;
system("cls");
switch(ch)
{
case '1':
write_account();
break;
case '2':
display_all();
break;
case '3':
cout<<"\n\n\tEnter The account No. : "; cin>>num;
delete_account(num);
break;
case '4':
cout<<"\n\n\tEnter The account No. : "; cin>>num;
modify_account(num);
break;
case '5':
cout<<"\n\n\tThanks for using Schedule management system";
break;
default :cout<<"\a";
}
cin.ignore();
cin.get();
}while(ch!='5');
return 0;
}

//***************************************************************
// INTRODUCTION FUNCTION
//****************************************************************


void intro()
{
cout<<"\n\n\n\t Schedule";
cout<<"\n\n\tMANAGEMENT";
cout<<"\n\n\t SYSTEM";
cout<<"\n\n\n\nMADE BY : your name";
cout<<"\n\nSCHOOL : your school name";
cin.get();
}

//***************************************************************
// function to write in file
//****************************************************************

void write_account(){

account ac;
ofstream outFile;
outFile.open("account.txt",ios::binary|ios::app);
ac.create_account();
outFile.write(reinterpret_cast<char *> (&ac), sizeof(account));
outFile.close();
}

//***************************************************************
//     function to display all accounts deposit list
//****************************************************************

void display_all()
{
   account ac;
   ifstream inFile;
   inFile.open("account.dat",ios::binary);
   if(!inFile)
   {
       cout<<"File could not be open !! Press any Key...";
       return;
   }
   cout<<"\n\n\t\tACCOUNT HOLDER LIST\n\n";
   cout<<"====================================================\n";
   cout<<"A/c no. NAME Type \n";
   cout<<"====================================================\n";
   while(inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
   {
       ac.report();
   }
   inFile.close();
}

//***************************************************************
//     function to delete record of file
//****************************************************************


void delete_account(int n)
{
   account ac;
   ifstream inFile;
   ofstream outFile;
   inFile.open("account.txt",ios::binary);
   if(!inFile)
   {
       cout<<"File could not be open !! Press any Key...";
       return;
   }
   outFile.open("Temp.dat",ios::binary);
   inFile.seekg(0,ios::beg);
   while(inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
   {
       if(ac.retid()!=n)
       {
           outFile.write(reinterpret_cast<char *> (&ac), sizeof(account));
       }
   }
   inFile.close();
   outFile.close();
   remove("account.txt");
   rename("Temp.dat","account.txt");
   cout<<"\n\n\tRecord Deleted ..";
}

//***************************************************************
//     function to modify record of file
//****************************************************************

void modify_account(int n)
{
   bool found=false;
   account ac;
   fstream File;
   File.open("account.dat",ios::binary|ios::in|ios::out);
   if(!File)
   {
       cout<<"File could not be open !! Press any Key...";
       return;
   }
   while(!File.eof() && found==false)
   {
       File.read(reinterpret_cast<char *> (&ac), sizeof(account));
       if(ac.retid()==n)
       {
           ac.show_account();
           cout<<"\n\nEnter The New Details of account"<<endl;
           ac.modify();
           int pos=(-1)*static_cast<int>(sizeof(account));
           File.seekp(pos,ios::cur);
           File.write(reinterpret_cast<char *> (&ac), sizeof(account));
           cout<<"\n\n\t Record Updated";
           found=true;
       }
   }
   File.close();
   if(found==false)
       cout<<"\n\n Record Not Found ";
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Following program is compiling fine. Output is shown below . For complete output you need files account.txt and temp.txt in your working directory as mentioned in code for data

#include<iostream>
#include <cstdlib>
#include<fstream>
#include<cctype>
#include<iomanip>
using namespace std;

//***************************************************************
// CLASS USED IN PROJECT
//****************************************************************


class account
{
public:
int id;
char name[10];
char type;

void create_account(); //function to get data from user
void show_account() const; //function to show data on screen
void modify(); //function to add new data
void report() const; //function to show data in tabular format
int retid() const; //function to return account number
char rettype() const; //function to return type of account
}; //class ends here

void account::create_account()
{
cout<<"\nEnter The Student ID No. :";
cin>>id;
cout<<"\n\nEnter Student Name (First and Last : ";
cin.ignore();
cin.getline(name,50);
cout<<"\nEnter Type of The account (T/S) : ";
cin>>type;
type=toupper(type);
cout<<"\n\n\nAccount Created..";
}

void account::show_account() const
{
cout<<"\nStudent ID No. : "<<id;
cout<<"\nStudent Name : ";
cout<<name;
cout<<"\nType of Account : "<<type;
}

void account::modify()
{
cout<<"\nStudent ID No. : "<<id;
cout<<"\nModify Student Name : ";
cin.ignore();
cin.getline(name,50);
cout<<"\nModify Type of Account : ";
cin>>type;
type=toupper(type);
}

void account::report() const
{
cout<<id<<setw(10)<<" "<<name<<setw(10)<<" "<<type<<setw(6)<<endl;
}


int account::retid() const
{
return id;
}

//***************************************************************
// function declaration
//****************************************************************
void write_account(); //function to write record in binary file
void display_all(); //function to display all account details
void modify_account(int); //function to modify record of file
void delete_account(int); //function to delete record of file
void intro(); //introductory screen function

//****************************************************************
// THE MAIN FUNCTION OF PROGRAM
//****************************************************************
int main()
{
char ch;
int num;
intro();
do
{
system("cls");
cout<<"\n\n\n\tMAIN MENU";
cout<<"\n\n\t01. NEW ACCOUNT";
cout<<"\n\n\t02. ALL ACCOUNT HOLDER LIST";
cout<<"\n\n\t03. CLOSE AN ACCOUNT";
cout<<"\n\n\t04. MODIFY AN ACCOUNT";
cout<<"\n\n\t05. EXIT";
cout<<"\n\n\tSelect Your Option (1-5) ";
cin>>ch;
system("cls");
switch(ch)
{
case '1':
write_account();
break;
case '2':
display_all();
break;
case '3':
cout<<"\n\n\tEnter The account No. : "; cin>>num;
delete_account(num);
break;
case '4':
cout<<"\n\n\tEnter The account No. : "; cin>>num;
modify_account(num);
break;
case '5':
cout<<"\n\n\tThanks for using Schedule management system";
break;
default :cout<<"\a";
}
cin.ignore();
cin.get();
}while(ch!='5');
return 0;
}

//***************************************************************
// INTRODUCTION FUNCTION
//****************************************************************


void intro()
{
cout<<"\n\n\n\t Schedule";
cout<<"\n\n\tMANAGEMENT";
cout<<"\n\n\t SYSTEM";
cout<<"\n\n\n\nMADE BY : your name";
cout<<"\n\nSCHOOL : your school name";
cin.get();
}

//***************************************************************
// function to write in file
//****************************************************************

void write_account(){

account ac;
ofstream outFile;
outFile.open("account.txt",ios::binary|ios::app);
ac.create_account();
outFile.write(reinterpret_cast<char *> (&ac), sizeof(account));
outFile.close();
}

//***************************************************************
// function to display all accounts deposit list
//****************************************************************

void display_all()
{
account ac;
ifstream inFile;
inFile.open("account.txt",ios::binary);
if(!inFile)
{
cout<<"File could not be open !! Press any Key...";
return;
}
cout<<"\n\n\t\tACCOUNT HOLDER LIST\n\n";
cout<<"====================================================\n";
cout<<"A/c no. NAME Type \n";
cout<<"====================================================\n";
while(inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
{
ac.report();
}
inFile.close();
}

//***************************************************************
// function to delete record of file
//****************************************************************


void delete_account(int n)
{
account ac;
ifstream inFile;
ofstream outFile;
inFile.open("account.txt",ios::binary);
if(!inFile)
{
cout<<"File could not be open !! Press any Key...";
return;
}
outFile.open("Temp.txt",ios::binary);
inFile.seekg(0,ios::beg);
while(inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
{
if(ac.retid()!=n)
{
outFile.write(reinterpret_cast<char *> (&ac), sizeof(account));
}
}
inFile.close();
outFile.close();
remove("account.txt");
rename("Temp.txt","account.txt");
cout<<"\n\n\tRecord Deleted ..";
}

//***************************************************************
// function to modify record of file
//****************************************************************

void modify_account(int n)
{
bool found=false;
account ac;
fstream File;
File.open("account.txt",ios::binary|ios::in|ios::out);
if(!File)
{
cout<<"File could not be open !! Press any Key...";
return;
}
while(!File.eof() && found==false)
{
File.read(reinterpret_cast<char *> (&ac), sizeof(account));
if(ac.retid()==n)
{
ac.show_account();
cout<<"\n\nEnter The New Details of account"<<endl;
ac.modify();
int pos=(-1)*static_cast<int>(sizeof(account));
File.seekp(pos,ios::cur);
File.write(reinterpret_cast<char *> (&ac), sizeof(account));
cout<<"\n\n\t Record Updated";
found=true;
}
}
File.close();
if(found==false)
cout<<"\n\n Record Not Found ";
}

// Output

Add a comment
Know the answer?
Add Answer to:
Code in C++: Please help me fix the error in function: void write_account(); //function to write...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program that uses a structure to store the following inventory information in a...

    Write a C++ program that uses a structure to store the following inventory information in a file: ⦁   Item description, Quantity on hand, Wholesale cost, Retail cost, and Date added to inventory. ⦁   Use a char array for item description and date. ⦁   The program should have a menu that allows the user to perform the following tasks: i.   Add a new record at the end of the file. ii.   Display any record in the file. iii.   Change any record...

  • Hi there! I need to fix the errors that this code is giving me and also...

    Hi there! I need to fix the errors that this code is giving me and also I neet to make it look better. thank you! #include <iostream> #include <windows.h> #include <ctime> #include <cstdio> #include <fstream> // file stream #include <string> #include <cstring> using namespace std; const int N=10000; int *freq =new int [N]; int *duration=new int [N]; char const*filename="filename.txt"; int songLength(140); char MENU(); // SHOWS USER CHOICE void execute(const char command); void About(); void Save( int freq[],int duration[],int songLength); void...

  • please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName();...

    please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName(); double getNumberExams(); double getScoresAndCalculateTotal(double E); double calculateAverage(double n, double t); char determineLetterGrade(); void displayAverageGrade(); int main() { string StudentName; double NumberExam, Average, ScoresAndCalculateTotal; char LetterGrade; StudentName = getStudentName(); NumberExam = getNumberExams(); ScoresAndCalculateTotal= getScoresAndCalculateTotal(NumberExam); Average = calculateAverage(NumberExam, ScoresAndCalculateTotal); return 0; } string getStudentName() { string StudentName; cout << "\n\nEnter Student Name:"; getline(cin, StudentName); return StudentName; } double getNumberExams() { double NumberExam; cout << "\n\n Enter...

  • Can some help me with my code I'm not sure why its not working. Thanks In...

    Can some help me with my code I'm not sure why its not working. Thanks In this exercise, you are to modify the Classify Numbers programming example in this chapter. As written, the program inputs the data from the standard input device (keyboard) and outputs the results on the standard output device (screen). The program can process only 20 numbers. Rewrite the program to incorporate the following requirements: a. Data to the program is input from a file of an...

  • The code will not run and I get the following errors in Visual Studio. Please fix the errors. err...

    The code will not run and I get the following errors in Visual Studio. Please fix the errors. error C2079: 'inputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' cpp(32): error C2228: left of '.open' must have class/struct/union (32): note: type is 'int' ): error C2065: 'cout': undeclared identifier error C2065: 'cout': undeclared identifier error C2079: 'girlInputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' error C2440: 'initializing': cannot convert from 'const char [68]' to 'int' note: There is no context in which this conversion is possible error...

  • Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string;...

    Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string; class Account { public: Account(); Account(string, double); void deposit(double); bool withdraw(double); string getName() const; double getBalance() const; private: string name; double balance; }; #endif ////////////////////////////////////////////// //AccountManager.hpp #ifndef _ACCOUNT_MANAGER_HPP_ #define _ACCOUNT_MANAGER_HPP_ #include "Account.hpp" #include <string> using std::string; class AccountManager { public: AccountManager(); AccountManager(const AccountManager&); //copy constructor void open(string); void close(string); void depositByName(string,double); bool withdrawByName(string,double); double getBalanceByName(string); Account getAccountByName(string); void openSuperVipAccount(Account&); void closeSuperVipAccount(); bool getBalanceOfSuperVipAccount(double&) const;...

  • what would be the pseudocode for my source code? Tools Plugins DoxyBlocks Settings Help program.cpp Code...

    what would be the pseudocode for my source code? Tools Plugins DoxyBlocks Settings Help program.cpp Code Blocks 16.01 e Edt View Search Project Build Debug Fortran wwSmith Tools Start here WOODSC program.cpp x #include <cstdlib> #include <iostream> include <string> Pinelude <stream include <string> using namespace std: int main(int argc, char argv11) 10 el itatre in stream: de lared to streamchele detasan input- string filein, fileout, key: Stringa tes are created to hold the nates and key Val of stream out...

  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT