我需要输入信息并为几个员工计算一些东西,并将每个员工信息输出到单个控制台,我需要使用数组。
问题是我真的不知道如何将循环中的信息存储到数组中。运动的屏幕截图是here
我问用户有多少工人,并且值进入“workers”变量,然后我创建int employees [workers]数组,因此循环中的迭代次数由用户的输入决定。
我的循环问题在于,无论有多少员工,它都不会重复问题。
我使用do while循环和“Count”变量来控制重复次数,但在输入信息一次后,它只显示结果,而不是再次询问问题。
我也尝试了while循环和“count”变量,但这次它只询问有多少员工,它只显示空输出。
int main()
{
//************************DECLARATIONS**********************
typedef char INFO;
INFO f_name[30];
INFO m_name[10];
INFO l_name[30];
int count; // tracks the number of iterations in do loop
int workers;
double rate;
double hrs_worked;
double gross_inc;
double overtime;
double tax_total;
float net;
float STATE_TAX;
float FED_TAX;
float UNI_FEE;
const double OVERTIME_R = 1.5;
//*****************INPUT FROM USER***************************
cout << "Enter amount of workers" << endl;
cin >> workers;
int employees[workers];
while(count < workers)
{
cout << "Enter worker's First name: " << endl;
cin.getline(f_name, (sizeof(f_name)-1));
cout << "Enter worker's middle name initial: " << endl;
cin.getline(m_name, (sizeof(m_name)-1));
cout << "Enter worker's last name: " << endl;
cin.getline(l_name, (sizeof(l_name)-1));
cout << "Enter number of hours worked: " << endl;
cin >> hrs_worked;
// If statement activates if user enters incorrect values
// and asks to reenter the correct value.
if(hrs_worked < 0 || hrs_worked > 60)
{
while(hrs_worked < 0 || hrs_worked > 60)
{
cout << "Must be between 0 and 60: " << endl;
cin >> hrs_worked;
}
}
cout << "Enter Rate Per Hour: " << endl;
cin >> rate;
// If statement activates if user enters incorrect values
// and asks to reenter the correct value.
if(rate < 0 || rate > 50)
{
while(rate < 0 || rate > 50)
{
cout << "Must be between 0 and 50: " << endl;
cin >> rate;
}
}
count++;
}
system("clear");
//**********************************CALCULATIONS*****************
// Calculates overtime if employee worked more than 40 hrs
if(hrs_worked > 40)
{
overtime = (hrs_worked - 40.0) * rate * OVERTIME_R;
}
gross_inc = (rate * hrs_worked) + overtime;
STATE_TAX = gross_inc * 0.06;
FED_TAX = gross_inc * 0.12;
UNI_FEE = gross_inc * 0.02;
tax_total = STATE_TAX + FED_TAX + UNI_FEE;
net = gross_inc - (tax_total)
return 0;
}
目前,优先级是设置正确的循环并将信息从循环存储到数组中。此时输出不是主要关注点。
第一个问题是你需要了解这一行中发生的事情:
int count; // tracks the number of iterations in do loop
您只需输出此变量的值:
cout << "count: " << count << endl;
之后你应该阅读一些关于在C ++中定义和声明变量的内容(它与“操作”不同)。
如果您只需要一个简单的解决方案,我可以告诉您,您的count
变量将具有一些“垃圾”值。解决它的最简单方法是将其初始化为起始值。在分析了上下文后,我可以假设int count = 0;
将是正确的起始值。
我想可能还有其他问题,但这个问题与你的问题直接相关。
祝你好运C ++!我还建议你深入研究C ++基础知识,首先要了解变量的定义和声明。
[UPDATE]
我想在您发布更新后添加一些内容:
int i; // will be used in for loop
请不要这样做。如果你想在“for循环”中使用它,那么在那里初始化它。
int workers = 0;
如果此变量表示“工人数量”,则应根据其含义对其进行命名,例如:
int numberOfWorkers = 0;
你的问题来自这个li(n)e(s):
// typedef for string data types
typedef char INFO;
不幸的是,你在这个评论中撒谎。这不是string
类型。你的typedef
是char
的别名所以它只存储一个字符,而不是string
。
它应该是
// typedef for string data types
typedef char* INFO;
但是在我的拙见中,由于INFO
的类型名称没有说什么,所以它是不情愿的。此外,你必须记住,如果你想解决这个问题,你还必须为这些c-string成员(f_name
,m_name
,l_name
)设置一些固定的大小,因为c-strings意味着具有恒定的大小。
还有另外一种解决方案 - 如果你想用C ++编写代码,更喜欢std::string
而不是c-strings。简而言之,std::string
的工作方式类似于char
类型元素的动态大小数组/容器。
你也可以简单地替换c风格的“动态”数组:
struct employee *emp = new employee[workers];
与...的std ::向量:
std::vector<employee> emp;
emp.reserve(workers);
(此行中也不需要使用struct关键字)。
所有输入检查都会发生另一个错误,例如:
while(emp[i].hrs_worked < 0 || emp[i].hrs_worked > 60)
{
cout << "Must be between 0 and 60: " << endl;
cin >> emp[i].hrs_worked;
}
它导致无限循环(我检查了它)。为什么?因为你没有清理输入缓冲区,例如:How do I flush the cin buffer?
我认为您也可以考虑将此结构更改为类,并考虑为您的类重载i / o流运算符(operator <<和operator >>),这将简化employee
对象的i / o操作。
最后一句话 - 请通过编辑上一个问题而不是将其作为答案来发布您的更新。
再一次 - 祝你学习C ++好运!
[更新]基本上,代码是垃圾,但它让我记住并学习了很多东西。所以这就是我重新编写代码的方式:
我使用了struct,pointer和“new”运算符。
唯一有问题的部分是这个输入部分,因为如果我输入多个字符,程序基本上会跳过所有内容,只显示一个模板:
cout << "Enter first name of employee "<<i+1<<" : " << endl;
cin >> emp[i].f_name;
cout << "Enter middle name of employee "<<i+1<<" : " << endl;
cin >> emp[i].m_name;
cout << "Enter last name of employee "<<i+1<<" : " << endl;
cin >> emp[i].l_name;
cout << "Hours of work by employee " << i+1 << ": " << endl;
cin >> emp[i].hrs_worked;
这是输出的样子:output
//*********************** Preprocessor Directives *********************
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string.h>
#include <cmath>
#include <stdlib.h>
#include <string.h>
using namespace std;
// Constants
const double OVERTIME_R = 1.5;
#define STATE_TAX 0.06
#define FED_TAX 0.12
#define UNION_FEE 0.02
// typedef for string data types
typedef char INFO;
// using struct to store employee info
struct employee
{
INFO f_name;
INFO m_name;
INFO l_name;
float rate;
float hrs_worked;
float gross;
float overtime;
float state_tax;
float fed_tax;
float uni_fee;
float net;
};
//******************************* main ********************************
int main()
{
int workers = 0;
int i; // will be used in for loop
float total_gross = 0;
float avg_gross = 0;
//*****************Process***************************
// asking number of employees
cout << "Enter the number of employees: " << endl;
cin >> workers;
// array of employees
struct employee *emp = new employee[workers];
for(i = 0; i < workers; i++)
{
cout << "Enter first name of employee "<<i+1<<" : " << endl;
cin >> emp[i].f_name;
cout << "Enter middle name of employee "<<i+1<<" : " << endl;
cin >> emp[i].m_name;
cout << "Enter last name of employee "<<i+1<<" : " << endl;
cin >> emp[i].l_name;
cout << "Hours of work by employee " << i+1 << ": " << endl;
cin >> emp[i].hrs_worked;
// If statement activates if user enters incorrect values
// and asks to reenter the correct value.
if(emp[i].hrs_worked < 0 || emp[i].hrs_worked > 60)
{
while(emp[i].hrs_worked < 0 || emp[i].hrs_worked > 60)
{
cout << "Must be between 0 and 60: " << endl;
cin >> emp[i].hrs_worked;
}
}
cout << "Rate Per Hour of employee " << i+1 << ": " << endl;
cin >> emp[i].rate;
// If statement activates if user enters incorrect >> values
// and asks to reenter the correct value.
if(emp[i].rate < 0 || emp[i].rate > 50)
{
while(emp[i].rate < 0 || emp[i].rate > 50)
{
cout << "Must be between 0 and 50: " << endl;
cin >> emp[i].rate;
}
}
// if employee has worked over 40 hrs. this statement activates.
if(emp[i].hrs_worked > 40)
{
emp[i].overtime = (emp[i].hrs_worked - 40.0) * emp[i].rate *
OVERTIME_R;
}
// Calculating the taxes.
emp[i].state_tax = emp[i].gross * STATE_TAX;
emp[i].fed_tax = emp[i].gross * FED_TAX;
emp[i].uni_fee = emp[i].gross * UNION_FEE;
emp[i].net= emp[i].gross - (emp[i].state_tax + emp[i].fed_tax +
emp[i].uni_fee);
// Total Gross
total_gross += emp[i].gross;
}
//**********************************OUTPUT****************************
cout << endl;
cout << endl;
cout << "\t\t\t\t" <<"Data Housing Corp. Weekly Payroll" << endl;
cout << "\t\t\t\t" <<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << "First Name" << setw(5) << "MI" << setw(13) << "Last Name" << setw(18)
<< "Rate per hour" << setw(18)<< "Hours worked" << setw(12)<< "Overtime"
<< setw(10)<< "Gross" << setw(15)<< "State tax"
<< setw(10)<< "Fed tax" << setw(15)<< "Union fee" << setw(10)<< "Net" << endl;
cout << "==========" << setw(5) << "==" << setw(13) << "=========" << setw(18)
<< "=============" << setw(18)<< "============" << setw(12)<< "========"
<< setw(10)<< "=====" << setw(15)<< "========="
<< setw(10)<< "=======" << setw(15)<< "=========" << setw(10)<< "===" << endl;
for ( i = 0 ; i < workers ; i++)
{
cout << setw(10) << emp[i].f_name << setw(5) << emp[i].m_name << setw(13)
<< emp[i].l_name << setw(18) << emp[i].rate << setw(18)<< emp[i].hrs_worked
<< setw(12)<< emp[i].overtime << setw(10)<< emp[i].gross << setw(15)
<< emp[i].state_tax << setw(10)<< emp[i].fed_tax << setw(15)<< emp[i].uni_fee
<< setw(10) << fixed << showpoint <<setprecision(2)<< emp[i].net << endl;
}
return 0;
}