我是编程新手,需要一些帮助!我得到"错误C4700未初始化的局部变量"在第23-26行,变量Cat1,Cat2,Cat3,EmpNum1,Pay1,EmpNum2,Pay2,EmpNum3和Pay3(如果重要的话,按顺序排列)
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream PayFileIn;
ofstream PayFileOut;
PayFileIn.open("G:\\PayInFile.txt"); //change input file location per device
PayFileOut.open("G:\\PayOutFile.txt"); //change input file location per device
char Cat1, Cat2, Cat3;
double Pay1, Pay2, Pay3;
int EmpNum1, EmpNum2, EmpNum3;
int AvgEmp = (Cat1 + Cat2 + Cat3) / 3; //line 23 //average number of total employees
double AvgPay1 = Pay1 / EmpNum1; //line 24 //average pay for category 1
double AvgPay2 = Pay2 / EmpNum2; //line 25 //average pay for category 2
double AvgPay3 = Pay3 / EmpNum3; //line 26 //average pay for category 3
double AvgPay4 = (Pay1 + Pay2 + Pay3) / (EmpNum1 + EmpNum2 + EmpNum3); //average pay for total employees
PayFileIn >> Cat1 >> Pay1 >> EmpNum1 >>
Cat2 >> Pay2 >> EmpNum2 >>
Cat3 >> Pay3 >> EmpNum3;
cout << setprecision(2);
cout << "The average number of employees is: " << AvgEmp << endl <<
"The average pay for Category 1 is: " << AvgPay1 << endl <<
"The average pay for Category 2 is: " << AvgPay2 << endl <<
"The average pay for Category 3 is: " << AvgPay3 << endl <<
"The average pay for all employees is: " << AvgPay4 << endl << endl;
}
答案 0 :(得分:0)
编译器消息非常清楚。您正在使用未初始化的变量。
// All uninitialized variables.
char Cat1, Cat2, Cat3;
double Pay1, Pay2, Pay3;
int EmpNum1, EmpNum2, EmpNum3;
您需要做的是移动线
int AvgEmp = (Cat1 + Cat2 + Cat3) / 3; //line 23 //average number of total employees
double AvgPay1 = Pay1 / EmpNum1; //line 24 //average pay for category 1
double AvgPay2 = Pay2 / EmpNum2; //line 25 //average pay for category 2
double AvgPay3 = Pay3 / EmpNum3; //line 26 //average pay for category 3
double AvgPay4 = (Pay1 + Pay2 + Pay3) / (EmpNum1 + EmpNum2 + EmpNum3); //average pay for total employees
通过从PayFileIn
读取变量初始化变量的行。
换句话说,使用:
char Cat1, Cat2, Cat3;
double Pay1, Pay2, Pay3;
int EmpNum1, EmpNum2, EmpNum3;
// Initialize the variables by reading into them from PayFileIn.
PayFileIn >> Cat1 >> Pay1 >> EmpNum1 >>
Cat2 >> Pay2 >> EmpNum2 >>
Cat3 >> Pay3 >> EmpNum3;
Now use them to compute other numbers.
int AvgEmp = (Cat1 + Cat2 + Cat3) / 3; //line 23 //average number of total employees
double AvgPay1 = Pay1 / EmpNum1; //line 24 //average pay for category 1
double AvgPay2 = Pay2 / EmpNum2; //line 25 //average pay for category 2
double AvgPay3 = Pay3 / EmpNum3; //line 26 //average pay for category 3
double AvgPay4 = (Pay1 + Pay2 + Pay3) / (EmpNum1 + EmpNum2 + EmpNum3); //average pay for total employees
答案 1 :(得分:0)
你知道它谈到的那些局部变量吗?
他们没有被初始化!
您正在计算中使用它们,但尚未给它们一个值。
尝试在计算之上移动以下行...这样,当代码按顺序执行时,将值放入变量的东西实际上会首先发生:
PayFileIn >> Cat1 >> Pay1 >> EmpNum1 >>
Cat2 >> Pay2 >> EmpNum2 >>
Cat3 >> Pay3 >> EmpNum3;