DrDeThuong
10-11-2002, 12:52 AM
Hi, there is something wrong with this program... I tried everything but still couldnt figure it out whats wrong. Please help!
First, the compiler generate exec fine, no bug or anything.
As you can see the program first call f1.setValues(); to let me input name, address, age, employee#, department. However something seem wrong with this function
void Faculty::setValues() {
Employee::setValues();
cout << "Enter Department: ";
getline(cin, department);
}
The problem is that I can only input name, address, age, employee#, but not department. Because after I enter the employee#, it won't let me input department, instead it exec the f1.viewAll() and f2.viewAll() as you can see in the output window.
To make the matter clear, after I inputed the employee# and hit enter. The program ended. ( It should let me input the department info, then end the program)
----------
I tried to change the setVaules as follow
void Faculty::setValues() {
cout << "Enter Department: ";
getline(cin, department);
Employee::setValues();
}
and everything worked fine. Well, it does fix the problem, however the output is not in the right order (department,name,address,age,employee#)
The correct output should be name,address,age,employee#,department
Is there something wrong with the getline(cin, department); in the original setValues function?
The output window is below (something is wrong with it..)
Please help me fix this!
Thanks alot
*************************************
FILE CommunityMember.h
*************************************
#include <iostream>
#include <string>
using namespace std;
class CommunityMember {
private:
string name;
string address;
unsigned int age;
public:
CommunityMember();
CommunityMember(string n, string ad, unsigned int a);
void setValues();
void viewAll() const;
protected:
};
*************************************
FILE CommunityMember.cpp
*************************************
#include "CommunityMember.h"
CommunityMember::CommunityMember():name(""), address(""), age(0)
{}
CommunityMember::CommunityMember(string n, string ad, unsigned int a): name(n), address(ad), age(a)
{}
void CommunityMember::setValues() {
cout << "---------------------" << endl;
cout << "Enter Name: ";
getline(cin, name);
cout << "Enter Address: ";
getline(cin, address);
cout << "Enter Age: ";
cin >> age;
}
void CommunityMember::viewAll() const{
cout << "Name: " << name << endl;
cout << "Address: " << address << endl;
cout << "Age: " << age << endl;
}
*************************************
FILE Employee.h
*************************************
#include "CommunityMember.h"
class Employee: public CommunityMember {
private:
unsigned int employeeNumber;
public:
Employee();
Employee(string n, string ad, unsigned int a, unsigned int en);
void setValues();
void viewAll() const;
protected:
};
*************************************
FILE Employee.cpp
*************************************
#include "Employee.h"
Employee::Employee(): CommunityMember(), employeeNumber(0)
{}
Employee::Employee(string n, string ad, unsigned int a, unsigned int en): CommunityMember(n,ad,a), employeeNumber(en)
{}
void Employee::setValues() {
CommunityMember::setValues();
cout << "Enter Employee Number: ";
cin >> employeeNumber;
}
void Employee::viewAll() const{
CommunityMember::viewAll();
cout << "Employee Number: " << employeeNumber << endl;
}
*************************************
FILE Faculty.h
*************************************
#include "Employee.h"
class Faculty: public Employee {
private:
string department;
public:
Faculty();
Faculty(string n, string ad, unsigned int a, unsigned int en, string de);
void setValues();
void viewAll() const;
protected:
};
*************************************
FILE Faculty.cpp
*************************************
#include "Faculty.h"
Faculty::Faculty(): Employee(), department("")
{}
Faculty::Faculty(string n, string ad, unsigned int a, unsigned int en, string de): Employee(n,ad,a,en), department(de)
{}
void Faculty::setValues() {
Employee::setValues();
cout << "Enter Department: ";
getline(cin, department);
}
void Faculty::viewAll() const {
Employee::viewAll();
cout << "Department: " << department << endl;
}
*************************************
FILE driver.cpp
*************************************
#include "Faculty.h"
int main() {
Faculty f1,f2("Cuong", "111 Somewhere", 23, 12345, "Science");
f1.setValues();
f1.viewAll();
f2.viewAll();
return 0;
}
First, the compiler generate exec fine, no bug or anything.
As you can see the program first call f1.setValues(); to let me input name, address, age, employee#, department. However something seem wrong with this function
void Faculty::setValues() {
Employee::setValues();
cout << "Enter Department: ";
getline(cin, department);
}
The problem is that I can only input name, address, age, employee#, but not department. Because after I enter the employee#, it won't let me input department, instead it exec the f1.viewAll() and f2.viewAll() as you can see in the output window.
To make the matter clear, after I inputed the employee# and hit enter. The program ended. ( It should let me input the department info, then end the program)
----------
I tried to change the setVaules as follow
void Faculty::setValues() {
cout << "Enter Department: ";
getline(cin, department);
Employee::setValues();
}
and everything worked fine. Well, it does fix the problem, however the output is not in the right order (department,name,address,age,employee#)
The correct output should be name,address,age,employee#,department
Is there something wrong with the getline(cin, department); in the original setValues function?
The output window is below (something is wrong with it..)
Please help me fix this!
Thanks alot
*************************************
FILE CommunityMember.h
*************************************
#include <iostream>
#include <string>
using namespace std;
class CommunityMember {
private:
string name;
string address;
unsigned int age;
public:
CommunityMember();
CommunityMember(string n, string ad, unsigned int a);
void setValues();
void viewAll() const;
protected:
};
*************************************
FILE CommunityMember.cpp
*************************************
#include "CommunityMember.h"
CommunityMember::CommunityMember():name(""), address(""), age(0)
{}
CommunityMember::CommunityMember(string n, string ad, unsigned int a): name(n), address(ad), age(a)
{}
void CommunityMember::setValues() {
cout << "---------------------" << endl;
cout << "Enter Name: ";
getline(cin, name);
cout << "Enter Address: ";
getline(cin, address);
cout << "Enter Age: ";
cin >> age;
}
void CommunityMember::viewAll() const{
cout << "Name: " << name << endl;
cout << "Address: " << address << endl;
cout << "Age: " << age << endl;
}
*************************************
FILE Employee.h
*************************************
#include "CommunityMember.h"
class Employee: public CommunityMember {
private:
unsigned int employeeNumber;
public:
Employee();
Employee(string n, string ad, unsigned int a, unsigned int en);
void setValues();
void viewAll() const;
protected:
};
*************************************
FILE Employee.cpp
*************************************
#include "Employee.h"
Employee::Employee(): CommunityMember(), employeeNumber(0)
{}
Employee::Employee(string n, string ad, unsigned int a, unsigned int en): CommunityMember(n,ad,a), employeeNumber(en)
{}
void Employee::setValues() {
CommunityMember::setValues();
cout << "Enter Employee Number: ";
cin >> employeeNumber;
}
void Employee::viewAll() const{
CommunityMember::viewAll();
cout << "Employee Number: " << employeeNumber << endl;
}
*************************************
FILE Faculty.h
*************************************
#include "Employee.h"
class Faculty: public Employee {
private:
string department;
public:
Faculty();
Faculty(string n, string ad, unsigned int a, unsigned int en, string de);
void setValues();
void viewAll() const;
protected:
};
*************************************
FILE Faculty.cpp
*************************************
#include "Faculty.h"
Faculty::Faculty(): Employee(), department("")
{}
Faculty::Faculty(string n, string ad, unsigned int a, unsigned int en, string de): Employee(n,ad,a,en), department(de)
{}
void Faculty::setValues() {
Employee::setValues();
cout << "Enter Department: ";
getline(cin, department);
}
void Faculty::viewAll() const {
Employee::viewAll();
cout << "Department: " << department << endl;
}
*************************************
FILE driver.cpp
*************************************
#include "Faculty.h"
int main() {
Faculty f1,f2("Cuong", "111 Somewhere", 23, 12345, "Science");
f1.setValues();
f1.viewAll();
f2.viewAll();
return 0;
}