Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

my do while fuctions will not stop giving me an error and i can seem to call the

ID: 3547962 • Letter: M

Question

my do while fuctions will not stop giving me an error and i can seem to call the function search here is my code



//
//  main.cpp
//  final project
//
//  Created by Ahmed Shogar on 12/3/13.
//  Copyright (c) 2013 Ahmed Shogar. All rights reserved.
//

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

fstream custFile;
fstream temFile;    //declaration swap//

const int NAME_LEN=45;
const int ADDR_LEN=45;
const int CITY_LEN=20;
const int STATE_LEN=4;
const int ZIP_LEN=11;
const int PHONE_LEN=14;
const int ARRAY_SIZE=11;

//structure used to represent customer data//

struct Customer
{
    char name[NAME_LEN];
    char address[ADDR_LEN];
    char city[CITY_LEN];
    char state[STATE_LEN];
    char zip[ZIP_LEN];
    char phone[PHONE_LEN];
    double balance;
    char lastPay[ARRAY_SIZE];
    
};

//prototypes//

void setInfo(long);     //writes record to file
void display(long);     //display one record
long search(long);      //find a record and return file position
void deleteRec(long);   //delete one record
void showAll();         //display all records

//flags to signal set setinfo()

bool rnew=0;            //if record is rnew
bool mod=0;             //if records is being modified

int main()
{
    long fpos; //file position
    int choice=6;
    char YorN;
    
    do
    {
        cout<<" ********MAIN MENU************** ";
        cout<<"1. Enter a new Customer Account ";
        cout<<"2. Display a Customer Account ";
        cout<<"3. Delete a Customer Account ";
        cout<<"4. Change a Customer Account ";
        cout<<"5. Display all Customer Account ";
        cout<<"6. Exit the program ";
    }
        //Determine user's Choice
    
    do{
        cout<<"Enter your choice (1-6):";
        cin>>choice;}
    while ((choice < 1 )|| (choice > 6));
        {};
    
    //process user's choice
    
    switch (choice)
    {
        case 1:
            cin.get();
            cout<<" You selected Enter a new Customr Account. ";
            rnew=1;
            setInfo(0);
            rnew=0;
            break;
        case 2:
            cout<<" You selected Display a Customer Account. ";
            fpos= search();
            if (fpos != -1)
            {
                display(fpos);
            }
            else
            {
                cout<<"/nRecord not found.";
                break;
            }
            
        case 3:
            cout<<" You selected Delete a Customer Account. ";
            fpos= search();
            if (fpos != -1)
            {
                display(fpos);
                cout<<"/nARE YOU SURE YOU WANT TO DELETE THIS RECORD? (Y/N)";
                cin>>YorN;
                YorN=toupper(YorN);
                if (YorN== 'Y')
                {
                    deleteRec(fpos);
                    break;
                }
                else
                {
                    cout<<" Record was not deleted. ";
                    break;
                }}
                else
                {
                    cout<<" Record not found. ";
                    break;
                }
        case 4:
            cout<<" You selected Change a Customer Account. ";
            fpos=search();
            mod= 1;
            if (fpos != -1)
            {
                cout<<" RECORD CONTENTS: ";
                display(fpos);
                cout<<" ENTER NEW CONTENTS: ";
                setInfo(fpos);
                mod= 0;
            }
            else
            {
                cout<< " Record not found. ";
                break;
            }
        case 5:
            cout<<" You selected display All Customer Account. ";
            showAll();
            break;
        case 6:
        exit(0);
        break;
            default://anything not between 1-5
        break;
    }}
//funciton defintions

/************************************
setinfo-get info for customerrecord and write to file.
*************************************/
void setinfo(long fp)
{
    Customer c;
    int valid;
    do
    {
        valid=1;
        cout<<" Please enter the following information: ";
        cout<<" Customer name:";
        cin.getline(c.name, 45);
        cout<<"Customer address:";
        cin.getline(c.address, 45);
        cout<<"City:";
        cin.getline(c.city, 20);
        cout<<"State:";
        cin.getline(c.state, 4);
        cout<<"Zip:";
        cin.getline(c.zip,11);
        cout<<"Telephone:";
        cin.getline(c.phone, 14);
        cout<<"Account Balance:";
        cin>>c.balance;
        cin.get();
        cout<<"Date of last payment:";
        cin.getline(c.lastPay, 11);
        if(strlen(c.name) ==0 || strlen(c.address) ==0 || strlen(c.city) ==0 || strlen(c.state) ==0 ||
           strlen(c.zip) ==0 || strlen(c.lastPay) ==0)
        {
         cout<<" you must enter information for each field. ";
            valid=0;
        }
        if(c.balance < 0)
        {
           cout<<"Please enter 0 or greater for account balance. ";
            valid=0;
        }}
    while (!valid);
    
    if(rnew)
    {
        custFile.open("cust.dat", ios::out | ios::app | ios::binary);
    }
    else if(mod)
    {
        custFile.open("cust.dat", ios::in | ios::out | ios::binary);
        custFile.seekp(fp, ios::beg);
    }
    if (custFile.fail())
    {
        cout<<" Error opening file. ";
        return;
    }
    custFile.write(reinterpret_cast<char*>(&c), sizeof(c));
    if (custFile.fail())
    {
        cout<<" Error writing record to file. ";
        custFile.clear();
        custFile.close();
        return;
    }
    else
    {
        cout<<" Record written to file. ";
        custFile.clear();
        custFile.close();
        cout<<" Press Enter to continue...";
        cin.get();
        return;
    }}
/*************************
display record at a given file position.
**************************/
void display(long fp)
{
    Customer c;
    custFile.open("cust.dat", ios::in | ios::binary);
    if (custFile.fail())
    {
        cout<<" Error opening file. ";
        return;
    }
    if (custFile.peek()==EOF)
    {
        cout<<" File is empty. ";
        custFile.clear();
        custFile.close();
        return;
    }
    custFile.seekg(fp, ios::beg);
    custFile.read(reinterpret_cast<char*>(&c), sizeof(Customer));
    cout.precision(2);
    cout<<"Customer name: "<<c.name<<endl;
    cout<<"Customer address: "<<c.address<<endl;
    cout<<"City: "<<c.city<<endl;
    cout<<"State: "<<c.state<<endl;
    cout<<"Zip: "<<c.zip<<endl;
    cout<<"Telephone: "<<c.phone<<endl;
    cout<<"Account balance: $ "<<c.balance<<endl;
    cout<<"Date of last Payment: "<<c.lastPay<<endl;
    custFile.clear();
    custFile.close();
    cout<<" Press Enter to continue. ";
    cin.get();
}

/**************************
search
reaturns file pointer position of a customer record
*****************/

long search()
{
    char name[45];
    Customer c;
    long fp;
    cout<<" Enter all or part of the customer name: ";
    cin.ignore();
    cin.getline(name, 45);
    if (name[0] == '')//if nothing is entered do this
    {
        cout<<" Error opening file. ";
        return -1;
    }
    while (true)
    {
        custFile.read(reinterpret_cast<char*>(&c), sizeof(Customer)); //read one record
        if (custFile.fail())
            break;
        if (strstr(c.name, name) != NULL)//if search value mathees customer name
        {
            fp=custFile.tellg();
            custFile.clear();
            custFile.close();
            return (fp-sizeof(c));
        }}
    cout<< "Record not found ";
    custFile.clear();
    custFile.close();
    return -1;
}
/****************
  showall-show records in file
  *****************/
void showAll()
{
    cin.ignore();
    Customer c;
    int count=0;
    custFile.open("cust.dat", ios::in | ios::binary);
    if (custFile.fail())
    {
        cout<<" Error opening file. ";
        return;
    }
    cout<<" *****Begin Customer Record Listing****** ";
    
    while (custFile.peek()!=EOF)
    {
        custFile.read(reinterpret_cast<char*>(&c), sizeof(Customer));
        cout<<setprecision(2);
        cout<<fixed<<showpoint;
        cout<<" RECORD NUMBER "<<++count <<":"<<endl;
        cout<<" Customer name: "<<c.name<<endl;
        cout<<"Customer Address: "<<c.address<<endl;
        cout<<"City: "<<c.city<<endl;
        cout<<"State: "<<c.state<<endl;
        cout<<"Zip: "<<c.zip<<endl;
        cout<<"Telephone: "<<c.phone<<endl;
        cout<<"Account Balance: $ "<<c.balance<<endl;
        cout<<"Date of Last Payment: "<<c.lastPay<<endl;
        cout<<endl;
        cout<<" Press Enter to Continue...";
        cin.get();
    }
    if (count==0)
    {
        cout<<" File is empty."<<endl;
    }
    cout<<" ***End Of Customer Record Listing*** ";
    custFile.clear();
    custFile.close();
}
/************************
deleteRec
this function marks a record for deletion by placing the null terminator at the beginning of the name member. the contents of the file are then copied to a temorary file, then the temporary file is copied back to the Customer file without the deleted record. ideally this fuction would be expanded to allow multiple record deltions before final file swap takes place
***********/
void deleteRec(long fp)
{
    Customer c;
    int Rec=0;
    custFile.open("cust.dat", ios::in | ios::out | ios::binary);
    if (custFile.fail())
    {
        cout<<" Error opening file. ";
        return;
    }
    
    //mark the file at offset fp for deletion
    strcpy(c.name, '');
    custFile.seekp(fp, ios::beg);
    custFile.write(reinterpret_cast<char*>(&c), sizeof(c));
    custFile.clear();
    custFile.close();
    //copy customer file to temorary file
    custFile.open("cust.dat", ios::in | ios::binary);
    temFile.open("temp.dat", ios::out | ios::binary);
    while (custFile.peek() != EOF)
    {
        custFile.read(reinterpret_cast<char*>(&c), sizeof(c));
        temFile.write(reinterpret_cast<char*>(&c), sizeof(c));
    }
    custFile.clear();
    temFile.clear();
    custFile.close();
    temFile.close();
    //compy temporary file to customer file, skipping the records that re marked for deletion
    temFile.open("tem.dat", ios::in | ios::binary);
    custFile.open("cust.dat", ios::out | ios::binary);
    while (true)
    {
        temFile.read(reinterpret_cast<char*>(&c), sizeof(c));
        if (temFile.fail())
            break;
        if (c.name[0] != '')
        {
            custFile.write(reinterpret_cast<char*>(&c), sizeof(c));
        
        }}
    temFile.clear();
    temFile.close();
    custFile.clear();
    custFile.close();
    cout<<" Deletion Successful. ";
    
}


Explanation / Answer

Have rectified the problem with do - while. Have not compiled and tested the program but just rectified visible problems. Please Check the program and let me know if issue still persists or if other issue pops up. I can compile test and resubmit if you have problems working with below code.




//
// main.cpp
// final project
//
// Created by Ahmed Shogar on 12/3/13.
// Copyright (c) 2013 Ahmed Shogar. All rights reserved.
//

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

fstream custFile;
fstream temFile; //declaration swap//

const int NAME_LEN=45;
const int ADDR_LEN=45;
const int CITY_LEN=20;
const int STATE_LEN=4;
const int ZIP_LEN=11;
const int PHONE_LEN=14;
const int ARRAY_SIZE=11;

//structure used to represent customer data//

struct Customer
{
char name[NAME_LEN];
char address[ADDR_LEN];
char city[CITY_LEN];
char state[STATE_LEN];
char zip[ZIP_LEN];
char phone[PHONE_LEN];
double balance;
char lastPay[ARRAY_SIZE];

};

//prototypes//

void setInfo(long); //writes record to file
void display(long); //display one record
long search(long); //find a record and return file position
void deleteRec(long); //delete one record
void showAll(); //display all records

//flags to signal set setinfo()

bool rnew=0; //if record is rnew
bool mod=0; //if records is being modified

int main()
{
long fpos; //file position
int choice=6;
char YorN;

do
{
cout<<" ********MAIN MENU************** ";
cout<<"1. Enter a new Customer Account ";
cout<<"2. Display a Customer Account ";
cout<<"3. Delete a Customer Account ";
cout<<"4. Change a Customer Account ";
cout<<"5. Display all Customer Account ";
cout<<"6. Exit the program ";

//Determine user's Choice
cout<<"Enter your choice (1-6):";
cin>>choice;}

//process user's choice

switch (choice)
{
case 1:
cin.get();
cout<<" You selected Enter a new Customr Account. ";
rnew=1;
setInfo(0);
rnew=0;
break;
case 2:
cout<<" You selected Display a Customer Account. ";
fpos= search();
if (fpos != -1)
{
display(fpos);
}
else
{
cout<<"/nRecord not found.";
break;
}

case 3:
cout<<" You selected Delete a Customer Account. ";
fpos= search();
if (fpos != -1)
{
display(fpos);
cout<<"/nARE YOU SURE YOU WANT TO DELETE THIS RECORD? (Y/N)";
cin>>YorN;
YorN=toupper(YorN);
if (YorN== 'Y')
{
deleteRec(fpos);
break;
}
else
{
cout<<" Record was not deleted. ";
break;
}}
else
{
cout<<" Record not found. ";
break;
}
case 4:
cout<<" You selected Change a Customer Account. ";
fpos=search();
mod= 1;
if (fpos != -1)
{
cout<<" RECORD CONTENTS: ";
display(fpos);
cout<<" ENTER NEW CONTENTS: ";
setInfo(fpos);
mod= 0;
}
else
{
cout<< " Record not found. ";
break;
}
case 5:
cout<<" You selected display All Customer Account. ";
showAll();
break;
case 6:
exit(0);
break;
default://anything not between 1-5
break;
}
}while ((choice < 1 )|| (choice > 6));
}
//funciton defintions

/************************************
setinfo-get info for customerrecord and write to file.
*************************************/
void setinfo(long fp)
{
Customer c;
int valid;
do
{
valid=1;
cout<<" Please enter the following information: ";
cout<<" Customer name:";
cin.getline(c.name, 45);
cout<<"Customer address:";
cin.getline(c.address, 45);
cout<<"City:";
cin.getline(c.city, 20);
cout<<"State:";
cin.getline(c.state, 4);
cout<<"Zip:";
cin.getline(c.zip,11);
cout<<"Telephone:";
cin.getline(c.phone, 14);
cout<<"Account Balance:";
cin>>c.balance;
cin.get();
cout<<"Date of last payment:";
cin.getline(c.lastPay, 11);
if(strlen(c.name) ==0 || strlen(c.address) ==0 || strlen(c.city) ==0 || strlen(c.state) ==0 ||
strlen(c.zip) ==0 || strlen(c.lastPay) ==0)
{
cout<<" you must enter information for each field. ";
valid=0;
}
if(c.balance < 0)
{
cout<<"Please enter 0 or greater for account balance. ";
valid=0;
}}
while (!valid);

if(rnew)
{
custFile.open("cust.dat", ios::out | ios::app | ios::binary);
}
else if(mod)
{
custFile.open("cust.dat", ios::in | ios::out | ios::binary);
custFile.seekp(fp, ios::beg);
}
if (custFile.fail())
{
cout<<" Error opening file. ";
return;
}
custFile.write(reinterpret_cast<char*>(&c), sizeof(c));
if (custFile.fail())
{
cout<<" Error writing record to file. ";
custFile.clear();
custFile.close();
return;
}
else
{
cout<<" Record written to file. ";
custFile.clear();
custFile.close();
cout<<" Press Enter to continue...";
cin.get();
return;
}}
/*************************
display record at a given file position.
**************************/
void display(long fp)
{
Customer c;
custFile.open("cust.dat", ios::in | ios::binary);
if (custFile.fail())
{
cout<<" Error opening file. ";
return;
}
if (custFile.peek()==EOF)
{
cout<<" File is empty. ";
custFile.clear();
custFile.close();
return;
}
custFile.seekg(fp, ios::beg);
custFile.read(reinterpret_cast<char*>(&c), sizeof(Customer));
cout.precision(2);
cout<<"Customer name: "<<c.name<<endl;
cout<<"Customer address: "<<c.address<<endl;
cout<<"City: "<<c.city<<endl;
cout<<"State: "<<c.state<<endl;
cout<<"Zip: "<<c.zip<<endl;
cout<<"Telephone: "<<c.phone<<endl;
cout<<"Account balance: $ "<<c.balance<<endl;
cout<<"Date of last Payment: "<<c.lastPay<<endl;
custFile.clear();
custFile.close();
cout<<" Press Enter to continue. ";
cin.get();
}

/**************************
search
reaturns file pointer position of a customer record
*****************/

long search()
{
char name[45];
Customer c;
long fp;
cout<<" Enter all or part of the customer name: ";
cin.ignore();
cin.getline(name, 45);
if (name[0] == '')//if nothing is entered do this
{
cout<<" Error opening file. ";
return -1;
}
while (true)
{
custFile.read(reinterpret_cast<char*>(&c), sizeof(Customer)); //read one record
if (custFile.fail())
break;
if (strstr(c.name, name) != NULL)//if search value mathees customer name
{
fp=custFile.tellg();
custFile.clear();
custFile.close();
return (fp-sizeof(c));
}}
cout<< "Record not found ";
custFile.clear();
custFile.close();
return -1;
}
/****************
showall-show records in file
*****************/
void showAll()
{
cin.ignore();
Customer c;
int count=0;
custFile.open("cust.dat", ios::in | ios::binary);
if (custFile.fail())
{
cout<<" Error opening file. ";
return;
}
cout<<" *****Begin Customer Record Listing****** ";

while (custFile.peek()!=EOF)
{
custFile.read(reinterpret_cast<char*>(&c), sizeof(Customer));
cout<<setprecision(2);
cout<<fixed<<showpoint;
cout<<" RECORD NUMBER "<<++count <<":"<<endl;
cout<<" Customer name: "<<c.name<<endl;
cout<<"Customer Address: "<<c.address<<endl;
cout<<"City: "<<c.city<<endl;
cout<<"State: "<<c.state<<endl;
cout<<"Zip: "<<c.zip<<endl;
cout<<"Telephone: "<<c.phone<<endl;
cout<<"Account Balance: $ "<<c.balance<<endl;
cout<<"Date of Last Payment: "<<c.lastPay<<endl;
cout<<endl;
cout<<" Press Enter to Continue...";
cin.get();
}
if (count==0)
{
cout<<" File is empty."<<endl;
}
cout<<" ***End Of Customer Record Listing*** ";
custFile.clear();
custFile.close();
}
/************************
deleteRec
this function marks a record for deletion by placing the null terminator at the beginning of the name member. the contents of the file are then copied to a temorary file, then the temporary file is copied back to the Customer file without the deleted record. ideally this fuction would be expanded to allow multiple record deltions before final file swap takes place
***********/
void deleteRec(long fp)
{
Customer c;
int Rec=0;
custFile.open("cust.dat", ios::in | ios::out | ios::binary);
if (custFile.fail())
{
cout<<" Error opening file. ";
return;
}

//mark the file at offset fp for deletion
strcpy(c.name, '');
custFile.seekp(fp, ios::beg);
custFile.write(reinterpret_cast<char*>(&c), sizeof(c));
custFile.clear();
custFile.close();
//copy customer file to temorary file
custFile.open("cust.dat", ios::in | ios::binary);
temFile.open("temp.dat", ios::out | ios::binary);
while (custFile.peek() != EOF)
{
custFile.read(reinterpret_cast<char*>(&c), sizeof(c));
temFile.write(reinterpret_cast<char*>(&c), sizeof(c));
}
custFile.clear();
temFile.clear();
custFile.close();
temFile.close();
//compy temporary file to customer file, skipping the records that re marked for deletion
temFile.open("tem.dat", ios::in | ios::binary);
custFile.open("cust.dat", ios::out | ios::binary);
while (true)
{
temFile.read(reinterpret_cast<char*>(&c), sizeof(c));
if (temFile.fail())
break;
if (c.name[0] != '')
{
custFile.write(reinterpret_cast<char*>(&c), sizeof(c));

}}
temFile.clear();
temFile.close();
custFile.clear();
custFile.close();
cout<<" Deletion Successful. ";

}