PDA

View Full Version : Help Help!!


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;
}

DrDeThuong
10-11-2002, 12:58 AM
Oh, I also attact all the files in zip so you can easy compile and see for yourself.

Thanks alot

Trung Quốc
10-11-2002, 05:08 PM
use fflush( stdin ); right after you get the input (getline) and before the next geline to clear the buffer that store in the momory so that your program wouldn't read in junk. :)

QT

DrDeThuong
10-11-2002, 06:39 PM
I followed your instruction...
but I got this error
" 'stdin' undeclared, first use in this function "

Trung Quốc
10-11-2002, 06:50 PM
look like your compiler didn't have standard input, weird. It should work for all compiler.

Don't know if you use it correct ly. Here is what it should look like'

void CommunityMember::setValues() {
cout << "Enter Name: ";
getline(cin, name);
fflush( stdin );
cout << "Enter Address: ";
getline(cin, address);
fflush( stdin );
cout << "Enter Age: ";
cin >> age;
}


and

void Faculty::setValues() {
Employee::setValues();
cout << "Enter Department: ";
fflush (stdin);
getline(cin, department);
}

DrDeThuong
10-11-2002, 07:13 PM
followed exactly the instruction...
still get

" 'stdin' undeclared, first use in this function "
implicit declaration of function 'int fflush(...)'

Im using Dev-C++ 4

Trung Quốc
10-11-2002, 07:54 PM
I think that compiler doesn't have some lib file. Try to use Ms C++ or borlan c++

ffffff
10-11-2002, 09:51 PM
fflush( stdin ); <= ca'i na`y cho C language
C++ hi`nh nhu+ du`ng cin.flush() thi` pha?i

chubuha
10-11-2002, 10:58 PM
maybe you should include

#include <iostream>

to use stdin

DrDeThuong
10-12-2002, 02:33 AM
I already included <iostream>

I installed Dev-C++ 5 (not 4) and test the program w/ it

cin.flush() => generate error. "no matching function for call to '_IO_istream_withassign::flush ()' "

tried to #include <iostream_withassign> => No such file or directory

ffffff
10-12-2002, 10:58 AM
cin >> flush; // thu+? ca'i na`y coi

Trung Quốc
10-12-2002, 11:45 AM
#include<stdio.h>

DrDeThuong
10-16-2002, 03:07 AM
thanks alot everyone...
I asked my professor, he said: use cin.ignore(20, '\n')
and it works fine