【发布时间】:2011-12-08 03:02:24
【问题描述】:
我在运行程序时收到此错误。它编译成功,但给了我一些关于未初始化变量的警告,我认为这些变量已经初始化。我收到错误消息“调试错误!运行时检查失败 #3 - 变量 'sumMaleGPA' 正在使用而未初始化。”
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void openFiles(ifstream& inFile, ofstream& outFile)
{
inFile.open("finalin.dat");
outFile.open("finalout.dat");
outFile << fixed << showpoint << setprecision(2);
inFile >> fixed >> showpoint >> setprecision(2);
if (!inFile||!outFile)
{
cout << "Problem opening file.";
}
}
void initialize(int countFemale,int countMale,float sumFemaleGPA,float sumMaleGPA)
{
countFemale=0;
countMale=0;
sumFemaleGPA=0;
sumMaleGPA=0;
}
void sumGrades(ifstream& inFile, float sumFemaleGPA, float sumMaleGPA,int m,int f)
{
sumFemaleGPA=0;
sumMaleGPA=0;
if (!inFile)
{
inFile.open("finalin.dat");
}
char sex;
float grade;
while(!inFile.eof())
{
inFile >> sex >> grade;
switch (sex)
{
case 'f': (sumFemaleGPA= sumFemaleGPA + grade);
f++;
break;
case 'm': (sumMaleGPA= sumMaleGPA + grade);
m++;
break;
}
}
}
void averageGPA(float avgfGPA, float avgmGPA, int m, int f, float sumFemaleGPA, float sumMaleGPA)
{
avgmGPA=0;
avgfGPA=0;
avgfGPA=sumFemaleGPA/f;
avgmGPA=sumMaleGPA/m;
}
void printResults(float avgfGPA, float avgmGPA, ofstream& outFile)
{
cout <<"The average GPA of the female students is: "<< avgfGPA << endl;
cout <<"The average GPA of the male students is: "<< avgmGPA;
outFile << "The average GPA of the female students is: "<< avgfGPA << endl;
outFile <<"The average GPA of the male students is: "<< avgmGPA;
}
int main()
{
int countFemale;
int countMale;
float sumFemaleGPA;
float sumMaleGPA;
float avgfGPA;
float avgmGPA;
ifstream inFile;
ofstream outFile;
openFiles(inFile,outFile);
initialize(countFemale,countMale,sumFemaleGPA,sumMaleGPA);
sumGrades(inFile,sumFemaleGPA,sumMaleGPA,countMale,countFemale);
averageGPA(avgfGPA,avgmGPA,countMale,countFemale,sumFemaleGPA,sumMaleGPA);
printResults(avgfGPA,avgmGPA, outFile);
}
不确定错误发生在哪里,所以我发布了整个文件。
【问题讨论】:
-
你认为变量为什么会被初始化?