0

Here I have tried to write a program write, update, display, delete, and print record. Display a menu of option and select an option to execute it. Problem is that, "Neither default switch statement is executed nor 3 to 6 option executed."

Can someone point out the error/mistakes?

 #include <iostream>
#include <fstream>
#include <windows.h>
#include <iomanip>

using namespace std;

void gotoxy(short x, short y) {
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

struct data{

char mark;
char name[15],city[15];

};

class person{
public:
    void addrec(void);
    void update(void);
    void disp_all(void);
    void display(void);
    void delrec(void);
    void pack(void);

};
int main(){
person obj;
int ch;
while(1){
    system("cls");
    gotoxy(28,5);
    cout<<"Person Records";
    gotoxy(25,6);
    cout<<"===================="; //20
    gotoxy(25,7);
    cout<<"1:   Add Records ";
    gotoxy(25,8);
    cout<<"2:   Update Records ";
    gotoxy(25,9);
    cout<<"3:   Display All Records ";
    gotoxy(25,10);
    cout<<"4:   Display Single Records ";
    gotoxy(25,11);
    cout<<"5:   Mark  Record for Deletion ";
    gotoxy(25,12);
    cout<<"6:   Delete Records Permanently";
    gotoxy(25,13);
    cout<<"7:   Exit ";
    gotoxy(25,15);
    cout<<"Enter Your choice  [1-7]  ";
    cin>>ch;

    switch(ch){
    case 1: obj.addrec(); break;
    case 2: obj.update(); break;
    case 3: obj.disp_all(); break;
    case 4: obj.display(); break;
    case 5: obj.delrec(); break;
    case 6: obj.pack(); break;
    case 7: exit(1); break;
    default:
        cout<<"invalid choice ";
    }
}
}

void person::addrec(void){
    data rec;
    char op;
    ofstream add("employee.dat",ios::binary);
    do{
        system("cls");
        cout<<"Enter Name of person: ";
        cin>>rec.name;
        cout<<"Enter City of person: ";
        cin>>rec.city;
        rec.mark=' ';

        add.write((char*)&rec,sizeof(rec));
        cout<<"More Records [Y/N]";
        cin>>op;
    }
    while(op=='y'||op=='Y');

    add.close();
}



void person::update(){
data rec;
char nm[15];
long int rn=0, t=0;

 fstream update_Rec("employee.dat", ios::binary|ios::in|ios::out);

 if(!update_Rec){
    cerr<<"File opening error "<<endl;
    exit(1);
 }

 cout<<"Enter Name of person ";
 cin>>nm;

 while(update_Rec.read((char*)&rec,sizeof(rec))){
    if(strcmp(nm,rec.name)==0){
            cout<<rec.mark<<rec.name<<"\t"<<rec.city<<endl;
        rn=(rn)&sizeof(rec);
    update_Rec.seekp(rn);
    cout<<"Enter New name of person";
    cin>>rec.name;
    cout<<"Enter new city of Person";
    cin>>rec.city;

    update_Rec.write((char*)&rec,sizeof(rec));

    update_Rec.close();

    t=1;
    }
rn++;
 }
if(t==0)
    cout<<"Record not found"<<endl;

}

void person::disp_all(void){

data rec;
char op;
ifstream disp_all_Rec("employee.dat",ios::binary);

if(!disp_all_Rec){
    cerr<<"File opening error";
    exit(1);
}
while(!disp_all_Rec.eof()){
    disp_all_Rec.read((char*)&rec,sizeof(rec));
    cout<<setw(15)<<bodyrec.mark<<rec.name<<setw(15)<<rec.city<<endl;
}
disp_all_Rec.close();
}


void person::display(void){

data rec;
char nm[15];

long int rn=0, t=0;

ifstream display_Rec("employee.dat",ios::binary);
if(!display_Rec){
    cerr<<"File opening error";
    exit(1);
}
cout<<"Enter name of person: ";
cin>>nm;

while(display_Rec.read((char*)&rec,sizeof(rec))){
    if(strcmp(nm,rec.name)==0){
        rn=(rn-1)*sizeof(rec);
        display_Rec.seekg(rn);
        cout<<rec.mark<<"\t"<<rec.name<<"\t"<<rec.city<<endl;

        display_Rec.close();
        t=1;
    }
rn++;
}
if(t==0)
cout<<"Record not found"<<endl;
}


void person::delrec(){
data rec;
char nm[15];
long int rn=0,t=0;

fstream del("employee.dat",ios::in|ios::out);
if(!del){

    cerr<<"File opening error"<<endl;
    exit(1);
}

cout<<"Enter Name of person";
cin>>nm;

while(del.read((char*)&rec,sizeof(rec))){
    if(strcmp(nm,rec.name)==0){
        cout<<"Record is : "<<rec.mark<<"\t"<<rec.name<<"\t"<<rec.city<<endl;

        if(rec.mark='*'){
            cout<<"Record already Marked \n";
            t=1;
        }
        else
        {
            rn=(rn)*sizeof(rec);
            del.seekp(rn);
            rec.mark='*';
            del.write((char*)&rec,sizeof(rec));

            cout<<"\n\n Record is marked for deletion \n";
            del.close();
            t=1;
        }
rn++;
    }
if(t==0)
    cout<<"Record not found";

}
}

void person::pack(void){
data rec;
char nm[15];
long int rn=0;

ifstream pack_Rec("employee.dat",ios::binary);
ofstream pack_Rec_open("temp.dat",ios::binary);

if(!pack_Rec){
    cerr<<"File opening error";
    exit(1);
}

while(pack_Rec.read((char*)&rec,sizeof(rec))){

    if(rec.mark!='*')
        pack_Rec_open.write((char*)&rec,sizeof(rec));
        rn++;
}
pack_Rec_open.close();
pack_Rec.close();

remove("employee.dat");
rename("temp.dat","employee.dat");
cout<<"Records are copies";

}

add records and update records are working properly but other functions are not properly?

Waqar
  • 8,558
  • 4
  • 35
  • 43
  • Did you try to debug in single step? – Scheff's Cat Jul 02 '20 at 06:22
  • I just compile and run the program , programm run successfully but not execute properly all functions – Hanzalah Anees Jul 02 '20 at 06:23
  • How exactly are most functions not executed properly? What are the symptoms? – Yunnosch Jul 02 '20 at 06:26
  • 1
    Ca you focus the problem by making a [mre] for the menu issue, which only calls empty output-only functions ? – Yunnosch Jul 02 '20 at 06:27
  • 1
    Sorry, debugging is an essential part of S/W development and worth to be learnt: [Get started with Visual Studio 2017 - Debugging](https://tutorials.visualstudio.com/vs-get-started/debugging). Just a hint: If you `cin >> ch;` with `char ch;` and you type in `1` then you will get `'1'` instead of `1`. The single quotes make a significant difference because `'1' == 49` ([ASCII](https://en.wikipedia.org/wiki/ASCII) encoding assumed). Additionally, you have to confirm your input `1`. Hence, you get two characters: first the `'1'` and second the `'\n'` (for the ENTER key). – Scheff's Cat Jul 02 '20 at 06:35
  • How do you know that `addrec` and `update` are working when you have no way to view the records that have been added or updated? I can see a bug in `addrec` so that definitely isn't working. The bug is that `ofstream add("employee.dat",ios::binary);` should be `ofstream add("employee.dat",ios::app|ios::binary);` because you want to append to existing data, not replace existing data. – john Jul 02 '20 at 07:11
  • `update` is bugged to `rn=(rn)&sizeof(rec); update_Rec.seekp(rn);` just makes no sense. It should be just `update_Rec.seekp(rn*sizeof(rec));` – john Jul 02 '20 at 07:15
  • 1
    Unfirtunately none of the functions you have written are working correctly. This is a problem when dealing with binary data, there is no easy way to check of the data has been written correctly. As has already been stated, you are really going to have to learn how to use a debugger to get this program completed successfully. You should also not be so quick to assume that code which has completed successfully without any apparaent error, is actually working, because it isn't necessarily true. – john Jul 02 '20 at 07:19

0 Answers0