结构数组 - 未引用的局部变量

时间:2017-07-24 16:32:40

标签: c++ arrays structure

我试图编译它,它说 (43):警告C4101:'work':未引用的局部变量

我正在尝试使用Worker结构数组                                                 写4个功能 1.输入一组Worker结构 2.计算每个工人的收入 数组中的结构 3.一个输出Worker结构数组 4.计算加班工人的数量。通过函数调用

返回答案

我无法删除int main()中的工作,否则它根本就不会编译,这是我到目前为止所做的:

#include <iostream>
using namespace std;


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



// function prototypes
void input(Worker [], const int);
void earnings(Worker [], const int);
void output(Worker [], const int);
int over(Worker [], const int);


int main() {



int overtime;



//const int numWorkers = number;
//work[];
//work[numWorkers];




Worker work;
int numWorkers;

cout << "How many workers are there?";
cin >> numWorkers;

Worker * workers = new Worker[numWorkers];


input(workers, numWorkers);
earnings(workers, numWorkers);
output(workers, numWorkers);
overtime = over(workers, numWorkers);

cout << "The number of workers who worked overtime is " << overtime << endl;

delete[] workers;

return 0;
}
void input(Worker f[], const int numPeople) {

for (int i = 0; i < numPeople; i++) {

    cout << "Worker # " << i << endl;
    cout << "Enter the id number: ";
    cin >> f[i].idNumber;
    cout << "Enter the hourly rate: ";
    cin >> f[i].hourlyRate;
    cout << "Enter the hours worked: ";
    cin >> f[i].hoursWorked;
}
}

void earnings(Worker e[], const int numPeople) {

for (int i = 0; i < numPeople; i++) {

    if (e[i].hoursWorked <= 40) {

        e[i].earned = e[i].hourlyRate * e[i].hoursWorked;

    }
    else {
        e[i].earned = (40 * e[i].hourlyRate) + ((e[i].hoursWorked - 40 
((e[i].hourlyRate / 2) + e[i].hourlyRate));
    }
}

}

void output(Worker end[], const int numPeople) {

for (int i = 0; i < numPeople; i++) {

    cout << "-------------------------" << endl;
    cout << "Worker # " << i << endl;
    cout << "Id number: " << end[i].idNumber << endl;
    cout << "Hours worked: " << end[i].hoursWorked << endl;
    cout << "Hourly rate: " << end[i].hourlyRate << endl;
    cout << "Earned: " << end[i].earned << endl;

}

}

int over(Worker o[], const int numPeople) {
int overWorked = 0;

for (int i = 0; i < numPeople; i++) {
    if (o[i].hoursWorked > 40) {
        overWorked += 1;
    }
}

return overWorked;
}

1 个答案:

答案 0 :(得分:2)

警告unreferenced local variable表示您定义了变量并且从不使用它。并且,由于这只是警告,它不会阻止编译或您的代码。您只需删除行Worker work;即可。

顺便说一句,你在这行中有编译错误:

e[i].earned = (40 * e[i].hourlyRate) + ((e[i].hoursWorked - 40 ((e[i].hourlyRate / 2) + e[i].hourlyRate));

当你发布你的问题时,可能是格式错误?可能这条线看起来像:

e[i].earned = (40 * e[i].hourlyRate) + ((e[i].hoursWorked - 40) * ((e[i].hourlyRate / 2) + e[i].hourlyRate));