Thread: help me debug this program

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    help me debug this program

    I'm using dev-c++ to build my programes.
    when i enter info of "myEmployee" then it is error "don't send"
    Fix and interpret it for me,please!
    Thank so much.
    Code:
    #include <sstream>
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <iostream>
    using namespace std;
    typedef struct {
            int day;
            int month;
            int year;
           
    } Date;
    typedef struct employee{
            string name;
            Date birthDay;
            string role;
            double salary;
    } Employee;
    
    void InitEmployee(Employee *myEmployee,int length);
    Employee searchSalaryMax(Employee *myEmployee,int length);
    Employee searchSalaryMin(Employee *myEmployee,int length);
    void display(Employee myEmployee);
    
    void InitEmployee(Employee *myEmployee,int length){
         myEmployee=new Employee[length];
         for(int i=0;i<length;i++){
                 cout<<"Numble: "<<i<<endl;
                 cout<<"Name: ";
                 fflush(stdin);
                 getline(cin,myEmployee[i].name);
                 cout<<"Day of birth: ";
                 cin>>myEmployee[i].birthDay.day;
                 cout<<"Month of birth: ";
                 cin>>myEmployee[i].birthDay.month;
                 cout<<"Year of birth: ";
                 cin>>myEmployee[i].birthDay.year;
                 cout<<"Role: ";
                 fflush(stdin);
                 getline(cin,myEmployee[i].role);
                 cout<<"Salary: ";
                 cin>>myEmployee[i].salary;
         }
         return;
    }
    Employee searchSalaryMax(Employee *myEmployee,int length){
             int index=0;
             double maxSalary=myEmployee[0].salary;
             for(int i=1;i<length;i++){
                     if(myEmployee[i].salary>maxSalary){
                        maxSalary=myEmployee[i].salary;
                        index=i;                                
                     }
             }                  
             return myEmployee[index];  
    }
    Employee searchSalaryMin(Employee *myEmployee,int length){
             int index=0;
             double minSalary=myEmployee[0].salary;
             for(int i=1;i<length;i++){
                     if(myEmployee[i].salary<minSalary){
                        minSalary=myEmployee[i].salary;
                        index=i;                                
                     }
             }                  
             return myEmployee[index];  
    }
    void display(Employee myEmployee){
         cout<<"Name: "<<myEmployee.name<<endl;    
         cout<<"Day of birth: "<<myEmployee.birthDay.day<<"/"<<myEmployee.birthDay.month<<"/"<<myEmployee.birthDay.year<<endl;
         cout<<"Role: "<<myEmployee.role<<endl;
         cout<<"Salary: "<<myEmployee.salary<<endl;
         return;
    }
    int main(){
         //clrscr();
         Employee *myEmployee,tempEmployee;    
         int length=0;
         cout<<"Mount of employees: ";
         cin>>length;
    
         //khoi tao danh sach nhan vien
         InitEmployee(myEmployee,length);
         //nhan vien co luong cao nhat
         tempEmployee=searchSalaryMax(myEmployee,length);
         display(tempEmployee);
         //nhan vien co luong thap nhat
         tempEmployee=searchSalaryMin(myEmployee,length);
         display(tempEmployee);
         //giai phong vung nho
         delete myEmployee;
         return 0;    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Fix and interpret it for me,please!
    Issuing demands is not the way to get help from people providing free help and advice. Phrase your problem as a question.

    This is a C++ program, so remove these three header files.
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>


    Next, stop using fflush(stdin)
    SourceForge.net: Fflush - cpwiki
    Use cin.ignore() if you want to throw away some input.

    Compile with as many error messages as possible.
    $ g++ -Wall -Wextra bar.cpp
    bar.cpp: In function ‘int main()’:
    bar.cpp:83:37: warning: ‘myEmployee’ may be used uninitialized in this function [-Wuninitialized]

    > InitEmployee(myEmployee,length);
    You need to change this so that myEmployee is modified by the function.
    Look up references in your book.

    > delete myEmployee;
    You must use delete [ ] myEmployee; when deleting memory allocated with new [ ]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    thank salem, my English is not very well,
    anyway, i fixed it. "void InitEmployee(Employee *myEmployee,int length)" change to "void InitEmployee(Employee *&myEmployee,int length)"
    It is run ok, now.
    Code:
    #include <sstream>
    #include <conio.h>
    #include <iostream>
    using namespace std;
    typedef struct {
            int day;
            int month;
            int year;
           
    } Date;
    typedef struct employee{
            string name;
            Date birthDay;
            string role;
            double salary;
    } Employee;
    
    void InitEmployee(Employee *&myEmployee,int length);
    Employee searchSalaryMax(Employee *myEmployee,int length);
    Employee searchSalaryMin(Employee *myEmployee,int length);
    void display(Employee myEmployee);
    
    void InitEmployee(Employee *&myEmployee,int length){
         myEmployee=new Employee[length];
         for(int i=0;i<length;i++){
                 cout<<"Numble: "<<i<<endl;
                 cout<<"Name: ";
                 cin.ignore();
                 getline(cin,myEmployee[i].name);
                 cout<<"Day of birth: ";
                 cin>>myEmployee[i].birthDay.day;
                 cout<<"Month of birth: ";
                 cin>>myEmployee[i].birthDay.month;
                 cout<<"Year of birth: ";
                 cin>>myEmployee[i].birthDay.year;
                 cout<<"Role: ";
                 cin.ignore();
                 getline(cin,myEmployee[i].role);
                 cout<<"Salary: ";
                 cin>>myEmployee[i].salary;
         }
         return;
    }
    Employee searchSalaryMax(Employee *myEmployee,int length){
             int index=0;
             double maxSalary=myEmployee[0].salary;
             for(int i=1;i<length;i++){
                     if(myEmployee[i].salary>maxSalary){
                        maxSalary=myEmployee[i].salary;
                        index=i;                                
                     }
             }                  
             return myEmployee[index];  
    }
    Employee searchSalaryMin(Employee *myEmployee,int length){
             int index=0;
             double minSalary=myEmployee[0].salary;
             for(int i=1;i<length;i++){
                     if(myEmployee[i].salary<minSalary){
                        minSalary=myEmployee[i].salary;
                        index=i;                                
                     }
             }                  
             return myEmployee[index];  
    }
    void display(Employee myEmployee){
         cout<<"Name: "<<myEmployee.name<<endl;    
         cout<<"Day of birth: "<<myEmployee.birthDay.day<<"/"<<myEmployee.birthDay.month<<"/"<<myEmployee.birthDay.year<<endl;
         cout<<"Role: "<<myEmployee.role<<endl;
         cout<<"Salary: "<<myEmployee.salary<<endl;
         return;
    }
    int main(){
         //clrscr();
         Employee *myEmployee,tempEmployee;    
         int length=0;
         cout<<"Mount of employees: ";
         cin>>length;
    
         //khoi tao danh sach nhan vien
         InitEmployee(myEmployee,length);
         //nhan vien co luong cao nhat
         tempEmployee=searchSalaryMax(myEmployee,length);
         display(tempEmployee);
         //nhan vien co luong thap nhat
         tempEmployee=searchSalaryMin(myEmployee,length);
         display(tempEmployee);
         //giai phong vung nho
         delete []myEmployee;
         getch();
         return 0;    
    }

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
    *&myEmployee
    Hi, are you aware of the relation of operators * and & ?They kill each other

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by std10093 View Post
    Code:
    *&myEmployee
    Hi, are you aware of the relation of operators * and & ?They kill each other
    Not in a declaration they dont. They only do so when used in an expession, and even then, in C++ they don't necessarily cancel out if there are operator overloads being called.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    i'm newbie, I do not really understand much of the pointer mechanism, in this case the program has run well with "void InitEmployee(Employee *&myEmployee,int length)"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program to debug
    By panks in forum C Programming
    Replies: 2
    Last Post: 03-28-2011, 09:47 AM
  2. Help with C program debug
    By nikhilbk in forum C Programming
    Replies: 2
    Last Post: 09-18-2010, 09:58 AM
  3. Please debug this program
    By TheDeveloper in forum C Programming
    Replies: 9
    Last Post: 01-07-2009, 11:00 AM
  4. debug program
    By new_c in forum C Programming
    Replies: 3
    Last Post: 03-18-2002, 11:50 PM
  5. How to debug C program?
    By zahid in forum C Programming
    Replies: 4
    Last Post: 12-19-2001, 10:25 PM