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

发布于 2025-01-01 06:50:41 字数 2947 浏览 2 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

二货你真萌 2025-01-08 06:50:41

使用 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_each, std::copy 例如)用于执行操作容器(或数组)中的元素

A small example for printing columns using C++ streams:

#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;
}

This would allow you to write an employee to standard output using:

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

Use a similar approach for the column headings.

Other points:

  • Prefer std::string to char[] (or char*) then you don't have to limit the length of the string (or explicitly manage the memory)
  • Use std::vector instead of a fixed array to avoid a limit
  • Make use of STL algorithms (std::for_each, std::copy for example) for performing operations on elements in containers (or array)
累赘 2025-01-08 06:50:41

您可以使用相同的循环来插入和显示或任何东西!因为它遍历了整个循环。

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";
}

查找每个结构对象的净值、税额和毛额并打印。

检查您是否希望在标记位置将 name 作为 int 。

#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" <<
            "How many Employees? ";
        cin >> numberOfEmployees;
    }

    int name;         // name is char[] or string
    int title;
    double SSNum;
    double Salary;
    double Withholding_Exemptions;

    for (int count = 0; count < numberOfEmployees; count++) {

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

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

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

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

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

you can use the same loop for insertion and displaying or anything!! because it traverses through the whole loop.

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";
}

Find net,tax and gross of each structure object and print.

Check if you want name as int at the marked position.

#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" <<
            "How many Employees? ";
        cin >> numberOfEmployees;
    }

    int name;         // name is char[] or string
    int title;
    double SSNum;
    double Salary;
    double Withholding_Exemptions;

    for (int count = 0; count < numberOfEmployees; count++) {

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

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

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

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

        cout << "Withholding Exemptions: ";
        cin >> employees[ count ].Withholding_Exemptions;
    }
}
情丝乱 2025-01-08 06:50:41

您可以使用“\t”序列,它在 C++ 中表示“制表符空间”(就像您在键盘上按 Tab 键一样)。
至于循环,它应该是这样的:

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

for(int n=0; n < employees; n++)
{
cout << employees[n].name << " \t" << employees[n].title << " \t" ...... << "\n"; 
} 

它应该可以工作:)

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:

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

for(int n=0; n < employees; n++)
{
cout << employees[n].name << " \t" << employees[n].title << " \t" ...... << "\n"; 
} 

It should work :)

原谅我要高飞 2025-01-08 06:50:41

您可以查看 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.

蹲墙角沉默 2025-01-08 06:50:41

您的代码看起来不错,尽管它比应有的更复杂,并且输入循环没有错误处理(如果用户输入无效的内容,您的程序将停止工作)。

以下是一些可以帮助您改进程序的一般提示:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文