【问题标题】:Solution for printing information to the screen in c++?在 C++ 中将信息打印到屏幕的解决方案?
【发布时间】:2012-02-01 08:39:42
【问题描述】:

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

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 美元。

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

【问题讨论】:

  • 请更具体。究竟是什么不起作用。你试过什么?如果您发布代码,请将其减少到证明您的问题所需的最低限度。例如,由于这个问题是关于程序的输出的,所以用一些硬编码数据替换从用户那里获取输入的部分,因为它不相关。有关如何编写好的代码示例的更多信息,请参阅sscce.org

标签: c++ printing screen


【解决方案1】:

一个使用 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)对容器(或数组)中的元素执行操作

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 我更新了我的程序。帮我检查一下,看看我添加的问题。谢谢
    • 写下你想为一个(单个)员工做的事情的代码。把它放在一个带下标的循环中。这是基本的想法!
    【解决方案3】:

    您可以使用 '\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"; 
    } 
    

    它应该工作:)

    【讨论】:

      【解决方案4】:

      您可以查看 STL iomanip 标头函数,例如 setw() Formatting Cout Output in C++ using iomanip .有了它,应该可以获得合适的固定大小的列,而这些选项卡可能不会。

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          猜你喜欢
          • 2011-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-07
          • 2011-03-09
          • 1970-01-01
          • 1970-01-01
          • 2018-10-14
          相关资源
          最近更新 更多