-3

This code is a simple exercise for class inheritance and virtual functions. The case class 'person' derives employee, dependent, worker, and manager. The code should display an entry screen, where a user can enter data for 10 people, which differs based on which type of person the user chooses to enter data for. Then, when the user has entered data to his satisfaction, pressing a key other than 1,2,3,4, or 5 will take them to a display menu, where they can choose which information they want to see.

The problem: The first for loop in main() works fine, and the data can be entered up to 10 times. However, the next loop (the display loop) displays the menu, and then immediately exits without reading the user's decision. I can't figure out why this is happening. IT even skips over my little test input at the end, it just exits the program?

Shouldn't it display the menu, then wait for user input, then continue or end the loop?

main.cpp

using namespace std;

#include "person.h"
#include <iostream>

using namespace std;

int main()
{

    worker w;
    employee e;
    dependent d;
    manager m;
    person p;

    int decision;


    for (int i=0; i<10; i++)
    {
        cout << "To enter employee information, press 1." << endl;
        cout << "To enter dependent information, press 2." << endl;
        cout << "To enter manager information, press 3." << endl;
        cout << "To enter worker information, press 4." << endl;
        cout << "To edit information for a person who is none of the above, press 5." << endl;
        cout << "To display entered information, press anything else." << endl;
        cin >> decision;
        if (decision==1)             //employee
        {
            e.obtain();
        }
        else if (decision==2)       //dependent
        {
            d.obtain();
        }
        else if (decision==3)         //manager
        {
            m.obtain();
        }
        else if (decision==4)        //worker
        {
            w.obtain();
        }
        else if (decision==5)        //other person
        {
            p.obtain();
        }
        else
        {
            break;
        }

    };

    for (int i=0; i<10; i++)

    {
        cout << "To display employee information, press 1." << endl;
        cout << "To display dependent information, press 2." << endl;
        cout << "To display manager information, press 3." << endl;
        cout << "To display worker information, press 4." << endl;
        cout << "To display information for a person who is none of the above, press 5." << endl;
        cout << "To exit, press anything else" << endl;
        cin >> decision;
        if (decision==1)             //employee
        {
            e.display();
        }
        else if (decision==2)       //dependent
        {
            d.display();
        }
        else if (decision==3)         //manager
        {
            m.display();
        }
        else if (decision==4)        //worker
        {
            w.display();
        }
        else if (decision==5)        //other person
        {
            p.display();
        }
        else
        {
            break;
        }
    }

    int test;
    cin >> test;
    return 0;
}

person.h

#ifndef PERSON_H
#define PERSON_H

#include <iostream>
#include <string>

using namespace std;

struct date                       //Structures defining date display and SSN display
    {                             //e.g. 3/12/14, XXX-XX-XXXX
        int day;
        int month;
        int year;
    };
struct SSN
    {
        int dig123;
        int dig45;
        int dig6789;
    };


class person                     //Base class
{
public:
    person();
    virtual ~person();
    virtual void obtain ();
    virtual void display();

protected:
    string name;
    char gender;
    date dob;                     //Date of Birth
    SSN ssn;
    string address;
    int phone;
};


class employee: public person   //Employee derived class
{
public:
    employee();
    ~employee ();
    void obtain();
    void display();
private:
    date doh;               //Date of Hire
    int salary;
    string location;
    int workphone;
};

class dependent: public person   //Dependent derived class
{
public:
    dependent();
    ~dependent();
    void obtain();
    void display();
private:
    string address;
    SSN ssn2;                    //SSN of employee of dependent

};

class manager: public person    //Manager derived class
{
public:
    manager();
    ~manager();
    void obtain();
    void display();
private:
    string title;
};

class worker: public person
{
public:
    worker();
    ~worker();
    void obtain();
    void display();
private:
    string project;
};


#endif // PERSON_H

person.cpp

#include "person.h"

person::person()
{

}

person::~person()
{

}

void person::obtain ()
{

    cout<<"Please enter information about the individual:" << endl;
    cout<< "Name:" << endl;
    getline (cin, name);
    getline (cin, name);
    cout<< "Gender (m or f): "<< endl;
    cin>> gender;
    cout<< "Date of Birth: Day? " << endl;
    cin>> dob.day;
    cout<< "Date of Birth: Month? " << endl;
    cin>> dob.month;
    cout<< "Date of Birth: Year? " << endl;
    cin>> dob.year;
    cout<< "Address: "<< endl;
    getline (cin, address);
    getline (cin, address);
    cout<< "Phone number: " << endl;
    cin>> phone;
    cout<< "First three digits of SSN: " << endl;
    cin>> ssn.dig123;
    cout<< "Next 2 digits of SSN: " << endl;
    cin>> ssn.dig45;
    cout<< "Final 4 digits of SSN: " << endl;
    cin>> ssn.dig6789;
}
void person::display()
{
    cout<< "Name: " << name << endl;
    if (gender=='m')
    {
        cout<<"Gender: "<<gender<< endl;
    }
    else if (gender=='f')
    {
        cout<<"Gender: "<< gender<<endl;
    }
    else
    {
        cout<<"You did not enter male(m) or female(f) as a gender."<<endl;
    }
    cout << "Date of Birth: " << dob.month << "/" << dob.day <<"/" << dob.year << endl;
    cout << "Address: " << address << endl;
    cout << "Phone number: " << phone << endl;
    cout << "SSN: " << ssn.dig123 << "-" << ssn.dig45 << "-" << ssn.dig6789 << endl;
}

employee::employee():person()
{

}

employee::~employee()
{

}


void employee::obtain()
{
    person::obtain();
    cout<< "Date of Hire: Day? " << endl;
    cin>> doh.day;
    cout<< "Date of Hire: Month? " << endl;
    cin>> doh.month;
    cout<< "Date of Hire: Year? " << endl;
    cin>> doh.year;
    cout<< "Salary: $" << endl;
    cin >> salary;
    cout<< "Location: "<< endl;
    getline (cin, location);
    getline (cin, location);
    cout<< "Work phone number: " << endl;
    cin>> workphone;
}

void employee::display()
{
    person::display();
    cout << "Date of Hire: " << doh.month << "/" << doh.day <<"/" << doh.year << endl;
    cout << "Location: " << location << endl;
    cout << "Work phone number: " << workphone << endl;
}

dependent::dependent(): person()
{

}

dependent::~dependent()
{

}

void dependent::obtain()
{
    person::obtain();
    cout << "SSN of employee of which this person is a dependent:" << endl;
    cout<< "First three digits of SSN: " << endl;
    cin>> ssn2.dig123;
    cout<< "Next 2 digits of SSN: " << endl;
    cin>> ssn2.dig45;
    cout<< "Final 4 digits of SSN: " << endl;
    cin>> ssn2.dig6789;
    cout << "Address (if different from employee's address): " << endl;
    cout<< "Address: "<< endl;
    getline (cin, address);
    getline (cin, address);

}

void dependent::display()
{
    person::display();
    cout<< "Address: " << address << endl;
    cout << "SSN of employee: " << ssn2.dig123 << "-" << ssn2.dig45 << "-" << ssn2.dig6789 << endl;
}

worker::worker():person()
{

}
worker::~worker()
{

}

void worker::obtain()
{
    person::obtain();
    cout << "Project: " << endl;
    getline (cin, project);
}

void worker::display()
{
    person::display();
    cout << "Project: " << project << endl;
}

manager::manager():person()
{

}

manager::~manager()
{

}

void manager::obtain()
{
    person::obtain();
    cout << "Title: " << endl;
    getline (cin, title);
    getline (cin, title);
}

void manager::display()
{
    person::display();
    cout << "Title: " << title << endl;
}

I would be very grateful is someone could give me some help about why this is happening.

ardunn
  • 79
  • 2
  • 9

2 Answers2

4

cin >> decision; only reads up to a \n or a space, once it takes your string out of the input buffer a \n is still left. So on the second iteration when you get to cin >> decision; again it reads up to the \n and exits. You need to clear the buffer by calling getline to consume the left over \n.

Fsmv
  • 1,106
  • 2
  • 12
  • 28
  • 1
    Beat me to it :) Was in progress of typing this. – Adam Aug 07 '14 at 19:00
  • thank you so much! so do i just use cin.getline() to get decision? – ardunn Aug 07 '14 at 19:07
  • @user3919624 **no.** `getline(cin, decision);` will do the trick. – scohe001 Aug 07 '14 at 19:07
  • @Josh should I do that for the first iteration as well? – ardunn Aug 07 '14 at 19:10
  • now i get the error, 'ERROR: no matching function call to getline(std::istream&, int&) – ardunn Aug 07 '14 at 19:12
  • Its supposed to be a `char *` in the second argument you need a temporary string variable to store the result in. – Fsmv Aug 07 '14 at 19:18
  • and then I need to convert it? – ardunn Aug 07 '14 at 19:29
  • No call `getline` after you've read the int. like `cin >> decision; char throwaway[2]; getline(cin, throwaway);` I made the size two because it needs room for the new line and the null terminator. – Fsmv Aug 07 '14 at 19:29
  • there's no function for getline(cin, char[2])? it gives me the error 'error: no matching function for call to getline(istream&, char[2])' – ardunn Aug 07 '14 at 19:40
  • Oh sorry, I got confused with another question and thought you were using C for some reason. Use `std::string` instead of a char array – Fsmv Aug 07 '14 at 19:45
1

1) There is no need for a semicolon at the end of your first for loop.

2) If the input is being skipped, more than likely you have junk in the input buffer that's being read. Try this before the second loop:

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

See this link: My cin is being ignored inside a while loop

Community
  • 1
  • 1
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45