在c++中将信息打印到屏幕的解决方案?
我必须制作一个循环来收集员工用户输入的所有信息。正如您所看到的,我已经获得了询问用户有多少员工以及他们的信息是什么的部分。现在我所要做的就是像这样将该信息打印到屏幕上,只是每个信息之间没有句点,并且每个信息之间有一些空格:
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");
}
好的,我更新了程序。现在我正在尝试进行计算。这就是我正在做的...
计算工资:
总工资是之前为员工输入的周工资。
净工资的计算方式为工资总额减去税额。
计算税款:每项预扣税豁免从工资中扣除 1 美元。这是调整后的收入。如果调整后收入小于0,则以0作为调整后收入。
将调整后的收入乘以税率,您应该假设税率为 25%。
例如,如果 Bob Cratchit 的周收入为 15 美元,有 7 名家属,那么他的调整后收入将为 8 美元。他的税额是 8 美元的 25%,即 2 美元,因此他的净工资是 13 美元。
我已经开始尝试得到这个。我把它放在第二个循环和最后一个循环之间。这是对的吗?
I have to make a loop to gather all the information from the users input of the employees. As you can see, I have gotten the parts where I ask the user how many employees and what their information is. Now all I have to is print that information to the screen like this, just without the periods between each and with a few spaces between each :
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
And this is what I have :
#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");
}
Ok I updated the program. Now I'm trying to do the calculations. This what I'm doing...
To calculate payroll:
Gross pay is the weekly salary which was previously entered for the employee.
Net pay is calculated as the gross pay minus the amount of tax.
To calculate tax: Deduct $1 from the salary for each withholding exemption. This is the adjusted income. If the adjusted income is less than 0, then use 0 as the adjusted income.
Multiply the adjusted income by the tax rate, which you should assume is a flat 25%.
As an example, if Bob Cratchit has a weekly income of $15 and 7 dependents, then his adjusted income would be $8. His tax would be 25% of $8 which is $2, and therefore his net pay is $13.
I have started trying to get this. I put it between the second loop and the last loop. Is this right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 C++ 流打印列的一个小示例:
这将允许您使用以下方法将员工写入标准输出:
对列标题使用类似的方法。
其他要点:
std::string
而不是char[]
(或char*
),这样就不必限制字符串的长度字符串(或显式管理内存)std::vector
而不是固定数组来避免限制std::for_each
,std::copy 例如)用于执行操作容器(或数组)中的元素
A small example for printing columns using C++ streams:
This would allow you to write an employee to standard output using:
Use a similar approach for the column headings.
Other points:
std::string
tochar[]
(orchar*
) then you don't have to limit the length of the string (or explicitly manage the memory)std::vector
instead of a fixed array to avoid a limitstd::for_each
,std::copy
for example) for performing operations on elements in containers (or array)您可以使用相同的循环来插入和显示或任何东西!因为它遍历了整个循环。
查找每个结构对象的净值、税额和毛额并打印。
检查您是否希望在标记位置将 name 作为 int 。
you can use the same loop for insertion and displaying or anything!! because it traverses through the whole loop.
Find net,tax and gross of each structure object and print.
Check if you want name as int at the marked position.
您可以使用“\t”序列,它在 C++ 中表示“制表符空间”(就像您在键盘上按 Tab 键一样)。
至于循环,它应该是这样的:
它应该可以工作:)
You can use the '\t' sequence, which represent in C++ a "Tab space" (like when you press Tab on your Keyboard).
As for the loop, it should be something like:
It should work :)
您可以查看 STL iomanip 标头函数,例如 setw()
使用 iomanip 在 C++ 中格式化 Cout 输出
。有了这个,应该可以获得适当的固定大小的列,而选项卡可能不会。
You could look into the STL iomanip header functions, like setw()
Formatting Cout Output in C++ using iomanip
. With that it should be possible to get decent fix sized columns, which tabs likely wont.
您的代码看起来不错,尽管它比应有的更复杂,并且输入循环没有错误处理(如果用户输入无效的内容,您的程序将停止工作)。
以下是一些可以帮助您改进程序的一般提示:
std::vector
而不是数组 - 这样您就不必限制可以处理的员工数量,因为vector
可以动态增长。运算符<<
-EmployeeT
的重载。std::setw
; 这个问题与您的问题类似,一种解决方案使用操纵器来格式化输出。Your code has seems OK, although it is more complicated than it should be, and the input-loop has no error-handling (if a user enters something invalid, your program will stop working).
Here are some general pointers that can help you make your program better:
std::vector<EmployeeT>
instead of an array - this way you don't have to limit the number of employees you can handle, because thevector
can grow dynamically.operator<<
-overload forEmployeeT
.std::setw
; This question is similar to your problem, and one solution uses manipulators to format the output.