Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++错误C47_C++ - Fatal编程技术网

C++错误C47

C++错误C47,c++,C++,我是编程新手,需要一些帮助!对于变量Cat1、Cat2、Cat3、EmpNum1、Pay1、EmpNum2、Pay2、EmpNum3和Pay3,我在第23-26行得到错误C4700未初始化的局部变量,如果有关系的话 #include <iostream> #include <fstream> #include <iomanip> using namespace std; int main() { ifstream PayFileIn; ofs

我是编程新手,需要一些帮助!对于变量Cat1、Cat2、Cat3、EmpNum1、Pay1、EmpNum2、Pay2、EmpNum3和Pay3,我在第23-26行得到错误C4700未初始化的局部变量,如果有关系的话

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

编译器的信息非常清楚。您正在使用未初始化的变量

// 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

你知道它说的那些局部变量吗

他们还没有入门

您正在计算中使用它们,但尚未给它们一个值

尝试在计算上方向上移动以下行。。。这样,当代码按顺序执行时,将值放入变量的工作实际上首先发生:

PayFileIn >> Cat1 >> Pay1 >> EmpNum1 >>
     Cat2 >> Pay2 >> EmpNum2 >>
     Cat3 >> Pay3 >> EmpNum3;

是的。我想我已经试过了,但我在程序中试过了。谢谢你的帮助!就像我说的…我是编程高手