用c ++打印信息到屏幕的解决方案?

时间:2012-02-01 08:39:43

标签: c++ printing screen

我必须创建一个循环来收集员工用户输入的所有信息。正如您所看到的,我已经获得了部分,我向用户询问了多少员工以及他们的信息。现在我只需要将这些信息打印到屏幕上,就像没有每个之间的时间段和每个之间的一些空格一样:

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

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

5 个答案:

答案 0 :(得分: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::stringchar[](或char*),然后您不必限制字符串的长度(或明确管理内存)
  • 使用std::vector代替固定数组以避免限制
  • 利用STL算法(例如std::for_eachstd::copy)对容器(或数组)中的元素执行操作

答案 1 :(得分:0)

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

它应该工作:)

答案 2 :(得分:0)

你可以使用相同的循环插入和显示或任何东西!!因为它贯穿整个循环。

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

查找每个结构对象的净额,税金和总额并打印。

检查您是否希望在标记位置将名称作为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 :(得分:0)

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

答案 4 :(得分:0)

您的代码似乎没问题,虽然它比应该的更复杂,并且输入循环没有错误处理(如果用户输入的内容无效,您的程序将停止工作)。

以下是一些可以帮助您改善计划的一般指示: