Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

C / C++ Forum


You are currently viewing our C / C++ Forum as a guest. Please register to participate.
Login



Closed Thread
Comment each and every line. And How to convert this code in C++
Old 01-03-2011, 12:02 PM Comment each and every line. And How to convert this code in C++
Junior Talker

Posts: 1
Name: Shayaan Mustafa
Trades: 0
Code:
#include<iostream>
#include<fstream>
#include<string>
struct emp{
    int eno;
    char name[50],address[50];
    float basic,allowence,deduction;
};
void addEmp(); //adds new record to data file
void disEmp(); //displays all records from file
void delEmp(); //asks empNo and removes that record from file
void updEmp(); //asks empno and lets you reenter that data
void cls(); //just clear screen
int main(){
    string dummy;
    int choice;
    cout<<"===== Main Menu ====="<<endl;
    cout<<endl<<endl;
    cout<<"1 -> Add Record "<<endl;
    cout<<"2 -> Display Record"<<endl;
    cout<<"3 -> Delete Record"<<endl;
    cout<<"4 -> Update Record"<<endl;
    cout<<endl;
    cout<<"0 -> Exit"<<endl;
    cout<<endl<<endl<<"Selection ";
    cin>>choice;
    if(choice<0 || choice>4){
        cls();
        main();
    }
    switch(choice){
        case 0:    break;
        case 1:    addEmp();    break;
        case 2:    disEmp();    break;
        case 3:    delEmp();    break;
        case 4:    updEmp();    break;
    }
}
void addEmp(){
    fstream data;
    string dummy;
    emp e;
    int choice;
    do{
        cls();
        cout<<"===== Enter New Record ====="<<endl;
        cout<<endl<<endl;
        cout<<"Eno :\t\t"; cin>>e.eno;
        getline(cin,dummy); //just to clear input buffer
        cout<<"\nName :\t\t"; gets_s(e.name,50);
        cout<<"\nAddress :\t"; gets_s(e.address,50);
        cout<<"\nBasic Salary :\t";cin>>e.basic;
        cout<<"\nAllowence :\t";cin>>e.allowence;
        cout<<"\nDeductions :\t";cin>>e.deduction;
        cout<<endl<<endl;
        cout<<"1 -> Save  | 2 -> Cancel"<<endl;
        cin>>choice;
    }while(!(choice==1 || choice==2));
    if(choice==1){
        data.open("data.dat",ios::out|ios::app);
        data.write((char*)&e,sizeof(e));
        data.close();
        cout<<"\nRecord Saved "<<endl<<endl;
    }else{
        cout<<"\n\nRecord Cancelled"<<endl<<endl;
    }
    cout<<"Enter another record?"<<endl;
    cout<<"1 -> Yes  | 2 -> No"<<endl;
    cin>>choice;
    if(choice==1)
        addEmp();
    else
        return;
    main();
}
void disEmp(){
    fstream data;
    emp e;
    data.open("data.dat",ios::in);
    cls();
    while(data.read((char*) &e, sizeof(e))){
        cout<<"\n\n-----Displaying record for ENO " <<e.eno<<endl;
        cout<<"\nName :\t\t"<<e.name;
        cout<<"\nAddress :\t"<<e.address;
        cout<<"\nBasic Salary :\t"<<e.basic;
        cout<<"\nAllowence :\t"<<e.allowence;
        cout<<"\nDeductions :\t"<<e.deduction<<endl;
        cout<<endl<<"\nTotal Salary :\t"<<e.basic+e.allowence;
        cout<<"\nNet Paid :\t"<<e.basic+e.allowence-e.deduction;
        cout<<endl;
    }
    data.close();
    main();
}
void delEmp(){
    fstream data, temp;
    emp e;
    int eno;
    cls();
    cout<<"===== Delete Record ====="<<endl;
    cout<<endl<<endl<<endl;
    cout<<"Enter ENO to delete record ";cin>>eno;
    rename("data.dat","temp.dat");
    data.open("data.dat",ios::app);
    temp.open("temp.dat",ios::in);
    temp.seekg(0,ios::beg);
    while(temp.read((char*)&e,sizeof(e))){
        if(!(e.eno==eno)){
            cout<<"Record to delete "<<e.eno;
            data.write((char*) &e,sizeof(e));
        }
    }
    data.close();
    temp.close();
    system("Del temp.dat");
    main();
}
void updEmp(){
    fstream data, temp;
    emp e;
    string dummy;
    int eno;
    cls();
    cout<<"===== Update Record ====="<<endl;
    cout<<endl<<endl<<endl;
    cout<<"Enter ENO :_";cin>>eno;
    rename("data.dat","temp.dat");
    data.open("data.dat",ios::app);
    temp.open("temp.dat",ios::in);
    temp.seekg(0,ios::beg);
    while(temp.read((char*)&e,sizeof(e))){
        if(e.eno==eno){
            cout<<"-----Current data for ENO: "<<e.eno;
            cout<<"\nName :\t\t"<<e.name;
            cout<<"\nAddress :\t"<<e.address;
            cout<<"\nBasic Salary :\t"<<e.basic;
            cout<<"\nAllowence :\t"<<e.allowence;
            cout<<"\nDeductions :\t"<<e.deduction<<endl;
            cout<<endl<<"\nTotal Salary :\t"<<e.basic+e.allowence;
            cout<<"\nNet Paid :\t"<<e.basic+e.allowence-e.deduction;
            cout<<endl<<endl;
            cout<<"-----Enter New Values-----"<<endl;
            cout<<"\nEno :\t\t"; cin>>e.eno;
            getline(cin,dummy); //just to clear input buffer
            cout<<"\nName :\t\t"; gets_s(e.name,50);
            cout<<"\nAddress :\t"; gets_s(e.address,50);
            cout<<"\nBasic Salary :\t";cin>>e.basic;
            cout<<"\nAllowence :\t";cin>>e.allowence;
            cout<<"\nDeductions :\t";cin>>e.deduction;
        }
        data.write((char*) &e,sizeof(e));
    }
    data.close();
    temp.close();
    system("Del temp.dat");
    main();
}
void cls(){
    for (int i=0;i<50;i++)
        cout<<"\n";
}
At least must answer one. Please
Comment each and every line in this program.
Also how to convert this C++ object-oriented code in just C++ so that it could be complied on "Turbo C++ IDE" i.e. it can be able to use header files of .h extesion.
Thanks in advance. Thanks a lot.
Shayaan_Mustafa is offline
View Public Profile
 
 
Register now for full access!
Old 01-03-2011, 12:17 PM Re: Comment each and every line. And How to convert this code in C++
chrishirst's Avatar
Defies a Status

Posts: 44,046
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
We don't do homework or coursework for you and that looks suspiciously like coursework or a mock exam question.

The idea of YOU commenting each line is to see if YOU understand what each line of code is meant to do.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
View Public Profile Visit chrishirst's homepage!
 
Old 01-05-2011, 09:51 AM Re: Comment each and every line. And How to convert this code in C++
Novice Talker

Posts: 8
Trades: 0
any C code can be compiled using C++,
C++ is C plus extra features (OOP, easier memory management, ..)
Helper_24 is offline
View Public Profile
 
Closed Thread     « Reply to Comment each and every line. And How to convert this code in C++
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13672 seconds with 11 queries