错误C4700:使用未初始化的局部变量“最低”

时间:2013-09-05 04:01:44

标签: c++

我有一个问题,我无法弄清楚。我对编程很陌生。这是我写的代码。这是我得到的错误“错误C4700:未初始化的局部变量'最低'使用”我不知道如何解决它。我也确定有更好的方法来编写这个程序,但这是我必须要做的要求。我该如何修复错误?

#include "stdafx.h"
#include "iostream"
#include <iomanip>

using namespace std;

//Function prototypes
void getScore(int &score);
int findLowest(int score1, int score2, int score3, int score4, int score5, int &lowest);
void calcAverage(int score1, int score2, int score3, int score4, int score5);


int main ()
{
cout<<"Average with Lowest Score Drop by John\n"<<endl; 
int score1, 
    score2, 
    score3, 
    score4, 
    score5,
    lowest;

    getScore(score1);
    getScore(score2);
    getScore(score3);
    getScore(score4);
    getScore(score5);

    calcAverage(score1, score2, score3, score4, score5);


    cin.get();
    cin.get();
return 0;
}

void getScore(int &score)
{

cout << "Please enter 5 test scores between 1 and 100): ";
cin >> score;

while (score <= 0 || score >= 100)
{
    cout << "Enter a score values of 0 to 100";
    cin >> score;
}
}

int findLowest(int score1, int score2, int score3, int score4, int score5, int &lowest)
{
lowest = score1;
if (score2 < lowest)
    lowest = score2;
else if (score3 < lowest)
    lowest = score3;
else if (score4 < lowest)
    lowest = score4;
else if (score5 < lowest)
    lowest = score5;

cout << "The lowest test score is " << lowest << endl;

return lowest;
}

void calcAverage (int score1, int score2, int score3, int score4, int score5)

{
int findLowest(int, int, int, int, int, int);
int lowest;
double average;
 findLowest(score1, score2, score3, score4, score5, lowest);


average = (((float)score1 + score2 + score3 + score4 + score5) - lowest) / 4.0;
//cout << setw(4);
cout << fixed << showpoint << setprecision(2);
cout <<" With the grade "<<lowest<< " dropped"<<"The average of test scores entered is: " << average << endl;
} 

2 个答案:

答案 0 :(得分:1)

findLowest内的

calcAverage原型缺少&

void calcAverage (int score1, int score2, int score3, int score4, int score5)
{
    int findLowest(int, int, int, int, int, int&);
    //                                         ^
    ....
}

实际上,你不需要那个原型,因为在main函数之前已有相同的原型。

答案 1 :(得分:0)

我只是偶然发现了你的问题。答案似乎很明显。看看你的最后几句:`     void calcAverage(int score1,int score2,int score3,int score4,int score5)

{
int findLowest(int, int, int, int, int, int);
int lowest;
double average;
 findLowest(score1, score2, score3, score4, score5, lowest);


average = (((float)score1 + score2 + score3 + score4 + score5) - lowest) / 4.0;
//cout << setw(4);
cout << fixed << showpoint << setprecision(2);
cout <<" With the grade "<<lowest<< " dropped"<<"The average of test scores entered is: " << average << endl;
} `

此处int lowest未初始化。 尝试编写int lowest(0);或类似的初始值,以满足您的需求。