gpt4 book ai didi

c++ - 在 C++ 中将信息打印到屏幕的解决方案?

转载 作者:行者123 更新时间:2023-11-28 08:10:56 26 4
gpt4 key购买 nike

我必须做一个循环来收集员工用户输入的所有信息。如您所见,我已经得到了询问用户有多少员工以及他们的信息是什么的部分。现在我所要做的就是像这样将信息打印到屏幕上,只是每个之间没有句点,每个之间有几个空格:

Weekly Payroll:
Name...............Title........Gross.......Tax.........Net

----------------------------------------

Ebenezer Scrooge.....................Partner...250.00......62.25.....187.75

Bob Cratchit...............................Clerk.......15.00........2.00.......13.00

这就是我所拥有的:

#include <iostream>
using namespace std;


const int MAXSIZE = 20;


struct EmployeeT
{
char name[MAXSIZE];
char title;
double SSNum;
double Salary;
double Withholding_Exemptions;
};

EmployeeT employees[MAXSIZE];

int main()
{

cout << "How many Employees? ";
int numberOfEmployees;
cin >> numberOfEmployees;



while(numberOfEmployees > MAXSIZE)
{

cout << "Error: Maximum number of employees is 20\n" ;
cout << "How many Employees? ";
cin >> numberOfEmployees;
}


char name[MAXSIZE];
int title;
double SSNum;
double Salary;
double Withholding_Exemptions;


for (int count=0; count < numberOfEmployees; count++)
{
cout << "Name: ";
cin >> employees[ count ].name;

cout << "Title: ";
cin >> employees[ count ].title;

cout << "SSNum: \n";
cin >> employees[ count ].SSNum;

cout << "Salary: \n";
cin >> employees[ count ].Salary;

cout << "Withholding Exemptions: \n";
cin >> employees[ count ].Withholding_Exemptions;
}

double gross;
double tax;
double net;
double adjusted_income;

gross = employees[ count ].Salary;
adjusted_income = employees[ count ].Salary - 1.00;
tax = adjusted_income * .25;
net = gross - tax;


cout << "Weekly Payroll:\t Name \t Title \t Gross \t Tax \t Net \n";

for (int count=0; count < numberOfEmployees; count++)
{
cout << employees[count].name << " \t" << employees[count].title << " \t" <<
gross << "\t" << tax << "\t" << net << "\n";
}
system("pause");

}

好的,我更新了程序。现在我正在尝试进行计算。这就是我正在做的...

To calculate payroll:

总工资是之前为员工输入的周薪。

净工资的计算方法是总工资减去税额。

要计算税款:从工资中扣除每项预扣税豁免 1 美元。这是调整后的收入。如果调整后收入小于 0,则将 0 作为调整后收入。

将调整后的收入乘以税率,您应该假设税率为 25%。

例如,如果 Bob Cratchit 的周收入为 15 美元,有 7 个受抚养人,那么他的调整后收入将为 8 美元。他的税是 8 美元的 25%,即 2 美元,因此他的净工资是 13 美元。

我已经开始尝试得到这个。我把它放在第二个循环和最后一个循环之间。这样对吗?

最佳答案

一个使用 C++ 流打印列的小例子:

#include <iomanip> 
#include <ios>

std::ostream& operator<<(std::ostream& a_out, const EmployeeT& a_e)
{
a_out << std::setfill(' ')
<< std::left
<< std::setw(sizeof(a_e.name))
<< a_e.name
<< std::setw(0)
<< a_e.title
<< " "
<< std::setw(10)
<< a_e.SSNum
<< a_e.Salary
<< a_e.Withholding_Exemptions;

return a_out;
}

这将允许您使用以下方式将员工写入标准输出:

std::cout << employees[n] << "\n";

对列标题使用类似的方法。

其他要点:

  • 首选 std::string 而不是 char[](或 char*),那么您不必限制字符串的长度字符串(或显式管理内存)
  • 使用 std::vector 而不是固定数组以避免限制
  • 利用 STL 算法(例如 std::for_eachstd::copy)对容器(或数组)中的元素执行操作

关于c++ - 在 C++ 中将信息打印到屏幕的解决方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9092946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com