如何将循环中的信息存储到数组中

How to store information from loop into an array

本文关键字:存储 数组 信息 循环      更新时间:2023-10-16

我需要为几个员工输入信息和计算一些东西,并将每个员工的信息输出到单个控制台中,我需要使用数组。

问题是我真的不知道如何将循环中的信息存储到数组中。演习截图在这里

我问用户有多少workers,值进入"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++的基本原理,从了解变量的定义和声明开始。

[更新]

我想在你发布更新后添加一点:

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类型。您的typedefchar的别名,因此它只存储一个字符,而不是string

应该是

// typedef for string data types    
typedef char* INFO;

但在我看来,这是多余的,因为字体名称INFO什么也没说。此外,您必须记住,如果要修复此问题,还必须为这些c字符串成员(f_namem_namel_name)设置一些固定大小,因为c字符串的大小是恒定的。

还有另一种解决方案——如果您想用C++进行编码,那么更喜欢std::string而不是C字符串。简而言之,std::string的工作方式类似于char类型元素的动态大小数组/容器。

您还可以简单地替换c样式的"动态"数组:

struct employee *emp = new employee[workers];

例如std::vector:

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

它会导致无限循环(我检查过了)。为什么?因为你没有清理输入缓冲区,这里的例子是:我如何刷新cin缓冲区?

我认为您还可以考虑将此结构更改为类,并考虑为类重载I/o流运算符(运算符<<和运算符>>),这将简化employee对象上的I/o操作。

最后一句话——请通过编辑前一个问题而不是将其作为答案来发布您的更新。

我再次祝你学习C++好运!

[UPDATE]基本上,代码是垃圾,但它让我记住并学到了很多东西。这就是我重新制作代码的方式:

我使用了已使用的结构、指针和"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;

这就是输出的样子:输出

//*********************** 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 << "tttt" <<"Data Housing Corp. Weekly Payroll" << endl;
cout << "tttt" <<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << 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;
}