Using a struct to hold information entered by the

2019-10-02 09:28发布

问题:

Okay, so I am writing a C++ program to declare a struct data type that holds the following information on an employee (First Name, Last Name, ID, Pay Rate, and Hours). My problem is that the user can only enter in the ID and First Name, then the whole program runs without letting the user enter the rest of the data.

Heres my code:

 #include <iostream> 
 #include <iomanip> 


    using namespace std;

    struct Employee
{

int employeeID;
char firstName;
char lastName;
float payRate;
int hours;

};



int main()
{
    int i, j;

    cout << "How Many Employees Do You Wish To Enter?:\n\n";
    cin >> j;

   Employee info;

   for (i = 0; i < j; i++)
   {
        cout << "Enter in the Data for Employee number " << i + 1 << endl;

        cout << setw(5) << "\n Please Enter The Employee ID Number: ";
        cin >> info.employeeID;

        cout << setw(5) << "\n Please Enter Employees First Name: ";
        cin >> info.firstName;

        cout << setw(5) << "\n Please Enter Employees Last Name: ";
        cin >> info.lastName;

        cout << setw(5) << "\n Please Enter Employees Pay Rate: ";
        cin >> info.payRate;

        cout << setw(5) << "\n  Please Enter The Hours The Employee Worked: 
";
        cin >> info.hours;


    }

    cout << "\n\n                                   \n";

    cout << "ID" << setw(15) << "First Name" << setw(10) << "Last Name" << 
setw(10) << "Pay Rate" << setw(10) << "Hours";
    cout << endl;

    for (i = 0; i < j; i++)
    {

    cout << "\n" << info.employeeID << setw(15) << info.firstName << setw(10) << info.lastName << setw(10) << info.payRate << setw(10) << info.hours;

}

cout << "\n\n                                    \n";



system("pause");
return 0;



};

回答1:

#include <iostream> 
#include <iomanip> 
#include <string> //Allows you to use strings, which are way more handy for text manipulation
#include <vector> //Allows you to use vector which are meant to be rezied dynamicaly, which is your case


using namespace std;

struct Employee
{

    int employeeID;
    string firstName; //HERE : use string instead of char (string are array of char)
    string lastName; //HERE : use string instead of char
    float payRate;
    int hours;

};



int main()
{
    int j; 

    cout << "How Many Employees Do You Wish To Enter?:\n\n";
    cin >> j;

    vector<struct Employee> info; //creation of the vector (dynamic array) to store the employee info the user is going to give you

    for (int i = 0; i < j; i++) //declare your looping iterator "i" here, you will avoid many error 
    {
        struct Employee employee_i; // create an employee at each iteration to associate the current info
        cout << "Enter in the Data for Employee number " << i + 1 << endl;

        cout << "\n Please Enter The Employee ID Number: ";
        cin >> employee_i.employeeID; 

        cout <<  "\n Please Enter Employees First Name: ";
        cin >> employee_i.firstName;

        cout << "\n Please Enter Employees Last Name: ";
        cin >> employee_i.lastName;

        cout << "\n Please Enter Employees Pay Rate: ";
        cin >> employee_i.payRate;

        cout << "\n  Please Enter The Hours The Employee Worked: ";
        cin >> employee_i.hours;

        info.push_back(employee_i); //store that employee info into your vector. Push_back() methods expands the vector size by 1 each time, to be able to put your item in it
    } // because you employee variable was create IN the loop, he will be destruct here, but not the vector which was created outside

    cout << "\n\n                                   \n";

    for (int i = 0; i < j; i++) //the loop to get back all the info from the vector
    {
        cout << "ID :" << info[i].employeeID << "  First Name :" << info[i].firstName << "  Last Name :" <<
            info[i].lastName << "  Pay Rate :" << info[i].payRate << "  Hours :"<< info[i].hours;
        cout << endl;
//notice the info[i], which leads you to the employee you need and the ".hours" which leads to the hours info of that specific employee
        }

        system("pause");
        return 0;
}


回答2:

First, please read Tips and tricks for using C++ I/O (input/output). It might be helpful to understand C++ I/O.

Here are some comments on your code:

First

Use string instead of char.

struct Employee
{
    int employeeID;
    string firstName;
    string lastName;
    float payRate;
    int hours;
};

Second

Use array of Employee object to store multiple employees.

Employee info[100]; 

Third

Use cin carefully depending data types. In your case, it would be something like this:

cout << "Enter in the Data for Employee number " << i + 1 << endl;
cout << setw(5) << "\n Please Enter The Employee ID Number: ";
cin >>  info[i].employeeID;
cin.ignore(); //It is placed to ignore new line character.
cout << setw(5) << "\n Please Enter Employees First Name: ";
getline (cin,  info[i].firstName);
cout << setw(5) << "\n Please Enter Employees Last Name: ";
getline (cin,  info[i].lastName);
cout << setw(5) << "\n Please Enter Employees Pay Rate: ";
cin >>  info[i].payRate;
cout << setw(5) << "\n  Please Enter The Hours The Employee Worked: ";
cin >>  info[i].hours;

Fourth

std::getline() can run into problems when used before std::cin >> var. So, std::cin.ignore() can be used in this case to solve the problem.

I hope it helps.



标签: c++ struct