简体   繁体   English

使用结构体的错误输出

[英]Incorrect output using structures

I am having some trouble displaying data that is in a structure.我在显示结构中的数据时遇到了一些问题。 After the user enters all their information, my program is supposed to display what was entered in the form of ... FirstName LastName TotalPay in the same line.在用户输入他们的所有信息后,我的程序应该在同一行中以 ... FirstName LastName TotalPay 的形式显示输入的内容。 I am testing it out with the hourly workers and their pay, Say there is 3 employees, what is displaying is the last employees information for 3 times.我正在用小时工和他们的工资进行测试,假设有 3 名员工,显示的是 3 次的最后员工信息。 How can I fix this or what am I doing wrong?我该如何解决这个问题或者我做错了什么?

This is my code:这是我的代码:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <cctype>
#include <sstream>
#include <set>
#include <iomanip> 

using namespace std;


//Create enumeration that contains bonus information 
enum BonusAvailability{NO_BONUS, AVG_BONUS, HIGH_BONUS};


//Create a struct that contains the employees personal information
struct PersonalInfo
{
    string fName,
           lName,
           title;
};

//Create a struct for hourly workers, calls personal info struct 
struct HourlyW
{
    PersonalInfo pData ; 
    double hoursWorked; 
    double hourlyRate;
};

//Create a struct for salary workers, calls personal info struct
struct SalaryW
{
    PersonalInfo pData;
    double salary;
    double bonus;
    BonusAvailability bData;

};



int main()
{


    int numInfo; // Holds number of employess to be entered 
    int numHour; // Holds number of hourly workers 
    int numSalary; // Holds number of salary workers
    char selection; // If user wants to continue 
    char selection2; // If user wants to continue 
    double tHour; // Total hourly 
    double tSalary; // Total salary
    HourlyW employee; // Create employee under Hourly W
    SalaryW emp; // Create emp under Salary W
    BonusAvailability b; // Create b under Bonus Availability 

                  // Get number of employees to be entered 
                  cout << "Enter the number of employees you will be entering information for: ";
                  cin >> numInfo;

                  // If number is negative, show error message  
                  while (numInfo < 0)
                   {
                       cout << "Invalid number or negative number! Try again!" << endl;
                       exit(1);
                   }
                  // Get number of hourly workers     
                  cout << "How many are hourly workers?" << endl;
                  cin >>numHour;

                  // If number is negative, show error message
                  while (numHour < 0)
                   {
                       cout << "Invalid number or negative number! Try again!" << endl;
                       exit(1);
                   }

                  // Get number of salary workers 
                  cout << "How many are salary workers?" << endl;
                  cin >> numSalary;

                  // If number is negative, show error message 
                  while (numSalary < 0)
                   {
                       cout << "Invalid number or negative number! Try again! " << endl;
                       exit(1);
                   }

                   // If sum of hourly and salary is less than or greater than the total number of employees entered show error message 
                   while ((numHour + numSalary) > numInfo || (numHour + numSalary < numInfo))
                   {
                       cout << "Hourly workers plus Salary workers does not equal " << numInfo << endl;
                       cout << "Try again " << endl;
                       cout << "" << endl;
                       cout << "How many hourly workers?" << endl;
                       cin >>numHour;
                       while (numHour < 0)
                        {
                            cout << "Invalid number or negative number!" << endl;
                            exit(1);
                        }
                        cout << "How many are salary workers?" << endl;
                        cin >> numSalary;
                        while (numSalary < 0)
                            {
                                cout << "Invalid number or negative number!" << endl;
                                exit(1);
                            }
                        if (numHour + numSalary == numInfo)
                        {
                            break;
                        }
                   }


                  // If hourly workers is greater than 0, run this to get the information
                  if (numHour > 0)
                  {
                    cout << "Hourly Workers!" << endl;
                    cout << "---------------" << endl;

                    for (int i = 0; i < numHour; i++)
                    {


                      cout << "Enter first name: ";
                      cin >> employee.pData.fName;
                      cout << "" << endl;
                      cout << "Enter last name: ";
                      cin >> employee.pData.lName;
                      cout << "" << endl;
                      cout << "Enter their title: ";
                      cin >> employee.pData.title;
                      cout << "" << endl;
                      cout << "Enter the hours worked: ";
                      cin >> employee.hoursWorked; 

                      // If hours worked is less than 0 or greater than 80, show error message 
                      while (employee.hoursWorked < 0 || employee.hoursWorked > 80)
                      {
                          cout << "Incorrect input!" << endl;
                          cout << "" << endl;
                          cout << "Enter the hours worked: ";
                          cin >> employee.hoursWorked;
                          if (employee.hoursWorked > 0 || employee.hoursWorked < 80)
                              break;
                      }
                      cout << "" << endl;
                      cout << "Enter the hourly rate: ";
                      cin >> employee.hourlyRate;
                      cout << "" << endl;
                      tHour = employee.hoursWorked * employee.hourlyRate; // Get total hourly pay 

                      //Ask if they want to continue 
                      cout << "Would you like to continue? Y or N" << endl;
                      cin >> selection;
                      if (selection == 'y' || selection == 'Y')
                      {
                          continue;
                      }
                      else 
                      {
                          break;
                      }
                     // cout << "Total " << tHour << endl;  //TEST 


                    } 
                  }


                 // If number of salary workers is greater than 0, run this to get the information  
                 if (numSalary > 0)
                 {
                    cout << "Salary Workers!" << endl;
                    cout << "---------------" << endl;
                    for (int i = 0; i < numSalary; i++)
                    {

                        cout << "Enter first name: ";
                        cin >> emp.pData.fName;
                        cout << "" << endl;
                        cout << "Enter last name: ";
                        cin >> emp.pData.lName;
                        cout << "" << endl;
                        cout << "Enter their title: ";
                        cin >> emp.pData.title;
                        cout << "" << endl;
                        cout << "Enter salary: ";
                        cin >> emp.salary;

                        // If salary is less that 0, show error message 
                        while (emp.salary < 0)
                        {
                          cout << "Incorrect input!" << endl;
                          cout << "" << endl;
                          cout << "Enter the salary: ";
                          cin >> emp.salary;
                          if (emp.salary > 0)
                              break;
                        }

                        cout <<"" << endl;
                        cout << "Enter bonus: ";
                        cin >> emp.bonus;

                        // If bonus is less than 0, show error message 
                        while (emp.bonus < 0)
                        {
                          cout << "Incorrect input!" << endl;
                          cout << "" << endl;
                          cout << "Enter bonus: ";
                          cin >> emp.bonus;
                          if (emp.bonus > 0)
                          {
                            break;
                          }
                        }


                        tSalary = emp.salary + emp.bonus; // Hold total pay for salary workers 
                        cout << "Salary is " << tSalary << endl; //TEST 

                        cout << "" << endl;

                        //See if user wants to continue 
                        cout << "Would you like to continue? Y or N" << endl;
                        cin >> selection2;
                        if (selection2 == 'y' || selection2 == 'Y')
                            {
                                continue;
                            }
                        else 
                            {
                                break;
                            }

                      // See what category the salary workers fall under 
                      if (emp.bonus == 0)
                      {
                          b = static_cast<BonusAvailability> (0);

                      }
                      else if  (emp.bonus > 0 && emp.bonus <= 5000)
                      {
                          b = static_cast<BonusAvailability> (1);
                      }
                      else if (emp.bonus > 5000)
                      {
                          b = static_cast<BonusAvailability>(2);
                      }
                        cout << "" << endl;
                        cout << "Bonus is " << b << endl; //TEST 
                    }

                 }

                // Begin displaying information 
                for (int i = 0; i < numInfo; i++)
                {
                    cout << "List of employees" << endl;
                    cout << "------------------" << endl;
                    cout << employee.pData.fName << " " <<  employee.pData.lName << " "    << tHour << endl;
                }

return 0;                

}

You have a semicolon on else (emp.bonus > 5000);您在else (emp.bonus > 5000);上有一个分号else (emp.bonus > 5000);

Sorry, that I missed the second part of your question.抱歉,我错过了您问题的第二部分。 After they enter the hourly and salaried employees add the two numbers and make sure they equal the total number of employees.在他们输入时薪和受薪员工后,将这两个数字相加并确保它们等于员工总数。 If not, make them re-enter the numbers.如果没有,让他们重新输入数字。

since you are NOT using scope enum, and you want print them on screen因为您没有使用范围枚举,并且您想在屏幕上打印它们

if (emp.bonus == 0)
{
    b = NO_BONUS;

}
else if (emp.bonus > 0 && emp.bonus <= 5000)
{
    b = AVG_BONUS;
}
else if (emp.bonus > 5000)
{
    b = HIGH_BONUS;
}
cout << "\nBonus is " << b << endl; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM