从结构输入信息到数组

时间:2015-05-04 21:30:06

标签: c++ arrays structure

试图弄清楚如何将信息输入到数组中并从结构化变量中输出。我可以在不使用数组的情况下轻松输入和输出信息,但是我正在尝试从键盘输入一组工作人员。我还需要计算每个工人的工资,但如果我能从前三个变量中找出简单信息的格式,我可以自己计算出这个部分。我试图调用数组中的输入函数,但没有成功,所以我只是删除了它并离开了函数。

 #include<iostream>


using namespace std;

struct Worker

{

    int idNumber;
    int hoursWorked;
    double hourlyRate;
    double earned;

};

void input(Worker & theData);
//Postcondition: theData.idNumber, theData.hoursWorked, and theData.hourlyRate are given input values
// the user must input into these values.
void print(const Worker &);

void input(Worker[], int howMany);

void  print(const Worker[], int);



void main()
{
    Worker arr[10];
    Worker Data;
    input(Data);
    print(Data);

    input(arr[10],7);

    system("pause");

}

void input(Worker& theData) {
    cout << "Enter the Employee idNumber ";
    cin >> theData.idNumber;
    cout << "Enter the Hours Worked. ";
    cin >> theData.hoursWorked;
    cout << "Enter the HoutlyRate for under 41 hours. ";
    cin >> theData.hourlyRate;
}

void input(Worker arr[], int howMany)
{
    for (int i = 0; i < howMany; i++)
    {
        input(arr[i]);
    }
}

void print(const Worker& w) {
    cout << w.idNumber << "\n" << w.hoursWorked << "\n" << w.hourlyRate << "\n" << w.earned << endl;
}

void print(const Worker arr[], int howMany)
{
    for (int i = 0; i < howMany; i++)
    {
        print(arr[i]);
    }
}

#include<iostream> using namespace std; struct Worker { int idNumber; int hoursWorked; double hourlyRate; double earned; }; void input(Worker & theData); //Postcondition: theData.idNumber, theData.hoursWorked, and theData.hourlyRate are given input values // the user must input into these values. void print(const Worker &); void input(Worker[], int howMany); void print(const Worker[], int); void main() { Worker arr[10]; Worker Data; input(Data); print(Data); input(arr[10],7); system("pause"); } void input(Worker& theData) { cout << "Enter the Employee idNumber "; cin >> theData.idNumber; cout << "Enter the Hours Worked. "; cin >> theData.hoursWorked; cout << "Enter the HoutlyRate for under 41 hours. "; cin >> theData.hourlyRate; } void input(Worker arr[], int howMany) { for (int i = 0; i < howMany; i++) { input(arr[i]); } } void print(const Worker& w) { cout << w.idNumber << "\n" << w.hoursWorked << "\n" << w.hourlyRate << "\n" << w.earned << endl; } void print(const Worker arr[], int howMany) { for (int i = 0; i < howMany; i++) { print(arr[i]); } }

3 个答案:

答案 0 :(得分:0)

好的,如果我正确理解你,你希望能够阅读有关一堆工人的信息并将其存储到一组结构中。纠正我,如果我错了,但如果这是你想做的事情,我认为你实际上可以使用单一工作人员input函数来欺骗这一点。

struct Worker
{
    int idNumber;
    int hoursWorked;
    double hourlyRate;
    double earned;
};

void input(Worker& theData) {
    cout << "Enter the Employee idNumber ";
    cin >> theData.idNumber;
    cout << "Enter the Hours Worked. ";
    cin >> theData.hoursWorked;
    cout << "Enter the HoutlyRate for under 41 hours. ";
    cin >> theData.hourlyRate;
}

void input(Worker arr[], int howMany)
{
    for (int i = 0; i < howMany; i++)
    {
        input(arr[i]);
    }
}

void print(const Worker& w) {
    cout << w.idNumber << "\n" << w.hoursWorked << "\n" << w.hourlyRate << "\n" << w.earned << endl;
}

void print(const Worker arr[], int howMany)
{
    for (int i = 0; i < howMany; i++)
    {
        print(arr[i]);
    }
}

有些注意事项,您要从数组输入函数中删除const。你要修改它不能成为const的数组。我在&#34;输入等等之后添加了一些空格。 &#34;这样它看起来更漂亮。您可以按照自己的说法自行计算出计算结果,只需按照此处的表格来确定如何为一系列事物做到这一点。一旦你为一个人写了函数,就可以很容易地为数组做这个。

同样对于你的教育,请记住数组本身就是通过引用传递的,这就是为什么我们可以通过引用函数将它们放入传递中而没有任何问题。

我应该补充一点,将来您可以使用以下方法直接访问数组中每个worker信息的各个部分:

arr[i].idNumber;
arr[i].any_struct_variable;

答案 1 :(得分:0)

你快到了。这听起来像课程作业,所以我不打算建议使用std :: vector。顺便提一下,查找std :: vector,并在可能时使用它。它会为你带来很多悲伤。

要使用数组,首先需要知道数组的大小和保留空间。有很多方法可以获得工人数量,但这可能是最简单的方法:

    input(workers, howMany);
    calc(workers, howMany);
    print(workers, howMany);
    system("pause");
    delete[] workers;
}

在那之后,它几乎继续做你正在做的事情,但是调用基于数组的函数并在你完成后进行清理。

select
  co.Name as CountryName,
  ci.Name as CityName,
  ci.Population as CityPopulation
from
  Country co
inner join City ci 
  on ci.CountryCode = co.CountryCode 
where
  co.Region in ('Western Europe')
  and ci.Population > 
    (select sum(ca.Population) / count(*) from City ca 
     where ca.CountryCode = co.CountryCode)

答案 2 :(得分:0)

谢谢大家的帮助!我终于明白了,它运作良好。

    #include<iostream>


using namespace std;

struct Worker

{

    int idNumber;
    int hoursWorked;
    double hourlyRate;
    double earned;

};

void input(Worker & theData);
//Postcondition: theData.idNumber, theData.hoursWorked, and theData.hourlyRate are given input values
// the user must input into these values.
void print(const Worker &);

void calcInput(Worker[], int howMany);

void input(Worker[], int howMany);
void  print(const Worker[], int);
void calc(Worker & theWage);



void main()
{
    Worker Data;
    int howMany;

    cout << "How many?";
    cin >> howMany;

    Worker * workers = new Worker[howMany];
    input(workers, howMany);
    calcInput(workers, howMany);
    print(workers, howMany);

    system("pause");
    delete[] workers;

}

void input(Worker& theData) {
    cout << "Enter the Employee idNumber ";
    cin >> theData.idNumber;
    cout << "Enter the Hours Worked. ";
    cin >> theData.hoursWorked;
    cout << "Enter the HoutlyRate for under 41 hours. ";
    cin >> theData.hourlyRate;
}

void input(Worker arr[], int howMany)
{
    for (int i = 0; i < howMany; i++)
    {
        input(arr[i]);
    }
}

void print(const Worker& w) {
    cout << w.idNumber << "\n" << w.hoursWorked << "\n" << w.hourlyRate << "\n" << w.earned << endl;
}

void print(const Worker arr[], int howMany)
{
    for (int i = 0; i < howMany; i++)
    {
        print(arr[i]);
    }
}

void calc(Worker & theWage)
{
    if (theWage.hoursWorked <= 40)
    {
        theWage.earned = theWage.hoursWorked * theWage.hourlyRate;
    }
    else
    {
        int basePay;
        basePay = theWage.hoursWorked * theWage.hourlyRate;
        theWage.earned = (theWage.hoursWorked - 40) * 1.5 + basePay;
    }
}

void calcInput(Worker arr[], int howMany)
{
    for (int i = 0; i < howMany; i++)
    {
        calc(arr[i]);
    }
}

#include<iostream> using namespace std; struct Worker { int idNumber; int hoursWorked; double hourlyRate; double earned; }; void input(Worker & theData); //Postcondition: theData.idNumber, theData.hoursWorked, and theData.hourlyRate are given input values // the user must input into these values. void print(const Worker &); void calcInput(Worker[], int howMany); void input(Worker[], int howMany); void print(const Worker[], int); void calc(Worker & theWage); void main() { Worker Data; int howMany; cout << "How many?"; cin >> howMany; Worker * workers = new Worker[howMany]; input(workers, howMany); calcInput(workers, howMany); print(workers, howMany); system("pause"); delete[] workers; } void input(Worker& theData) { cout << "Enter the Employee idNumber "; cin >> theData.idNumber; cout << "Enter the Hours Worked. "; cin >> theData.hoursWorked; cout << "Enter the HoutlyRate for under 41 hours. "; cin >> theData.hourlyRate; } void input(Worker arr[], int howMany) { for (int i = 0; i < howMany; i++) { input(arr[i]); } } void print(const Worker& w) { cout << w.idNumber << "\n" << w.hoursWorked << "\n" << w.hourlyRate << "\n" << w.earned << endl; } void print(const Worker arr[], int howMany) { for (int i = 0; i < howMany; i++) { print(arr[i]); } } void calc(Worker & theWage) { if (theWage.hoursWorked <= 40) { theWage.earned = theWage.hoursWorked * theWage.hourlyRate; } else { int basePay; basePay = theWage.hoursWorked * theWage.hourlyRate; theWage.earned = (theWage.hoursWorked - 40) * 1.5 + basePay; } } void calcInput(Worker arr[], int howMany) { for (int i = 0; i < howMany; i++) { calc(arr[i]); } }