更新: 所以我能够编译并运行该程序,它似乎工作!然而,判断得分值2.3的变量 - 以奇怪的方式存储并且放弃计算。当我输入2.3时,商店价值是2.299999999998 - 这是为什么? calcScore或最低和最高函数可能会出错,因为最终得分在输入分数时会减少0.3,2.3 - 5.5 - 5.5 - 5.5 - 8.5 - 任何想法?
// file name starSearch.cpp
// author: SK
// last modified: 03/19/2012
#include <iostream>
#include <iomanip>
using namespace std;
//function prototypes
void getJudgeData(double &);
void calcScore(double, double, double, double, double);
int findLowest(double, double, double, double, double);
int findHighest(double, double, double, double, double);
//declare variables
double score1;
double score2;
double score3;
double score4;
double score5;
int main()
{
getJudgeData(score1); //Call getJudgeData for first judge score
getJudgeData(score2); //Call getJudgeData for second judge score
getJudgeData(score3); //Call getJudgeData for third judge score
getJudgeData(score4); //Call getJudgeData for fourth judge score
getJudgeData(score5); //Call getJudgeData for fifth judge score
calcScore(score1, score2, score3, score4, score5); //calculate contestant score
return 0;
}
//
//Begin function prototypes definitions
//
//defines calcScore.
void calcScore(double score1, double score2, double score3, double score4, double score5)
{
double total = (score1 + score2 + score3 + score4 + score5 - findLowest(score1, score2, score3, score4, score5) - findHighest(score1, score2, score3, score4, score5)) / 3;
cout << setprecision(2);
cout << "The contestant's final score is: " << total << endl;
}
//defines getJudgeData. this function asks each judge for their score.
void getJudgeData(double &score)
{
cout << "Judge, enter the contestant's score: ";
cin >> score;
if (score < 0 || score > 10) //validate score between 0-10, else display error msg
{
cout << "ERROR: Enter a value in the range 0-10.\n";
return getJudgeData(score);
}
return;
}
//defines findLowest. this function returns the lowest score entered.
int findLowest(double score1, double score2, double score3, double score4, double score5)
{
if (score1 < score2 && score1 < score3 && score1 < score4 && score1 < score5)
{
return score1;
}
else if (score2 < score1 && score2 < score3 && score2 < score4 && score2 < score5)
{
return score2;
}
else if (score3 < score1 && score3 < score2 && score3 < score4 && score3 < score5)
{
return score3;
}
else if (score4 < score1 && score4 < score2 && score4 < score3 && score4 < score5)
{
return score4;
}
else if (score5 < score1 && score5 < score2 && score5 < score3 && score5 < score4)
{
return score5;
}
}
//defines findHighest. this functio returns the highest score entered.
int findHighest(double score1, double score2, double score3, double score4, double score5)
{
if (score1 > score2 && score1 > score3 && score1 > score4 && score1 > score5)
{
return score1;
}
else if (score2 > score1 && score2 > score3 && score3 > score4 && score3 > score5)
{
return score2;
}
else if (score3 > score1 && score3 > score2 && score3 > score4 && score3 > score5)
{
return score3;
}
else if (score4 > score1 && score4 > score2 && score4 > score3 && score4 > score5)
{
return score4;
}
else if (score5 > score1 && score5 > score2 && score5 > score3 && score5 > score4)
{
return score5;
}
}
ORIGINAL:
我在第39行得到编译器错误,其中我的代数表达式用整数3写。我只是想弄清楚如何平均五个分数并删除最低和最高。确定哪个最高和最低的功能似乎是有序的..请帮助。
谢谢。
*// file name starSearch.cpp
// author: SK
// last modified: 03/19/2012
#include <iostream>
using namespace std;
//function prototypes
void getJudgeData(double);
void calcScore();
int findLowest();
int findHighest();
//declare variables
double score1;
double score2;
double score3;
double score4;
double score5;
int main()
{
getJudgeData(score1); //Call getJudgeData for first judge score
getJudgeData(score2); //Call getJudgeData for second judge score
getJudgeData(score3); //Call getJudgeData for third judge score
getJudgeData(score4); //Call getJudgeData for fourth judge score
getJudgeData(score5); //Call getJudgeData for fifth judge score
calcScore(); //calculate contestant score
return 0;
}
//
//Begin funtion prototypes definitions
//
//defines calcScore.
void calcScore(double score1, double score2, double score3, double score4, double score5)
{
double total = score1 + score2 + score3 + score4 + score5 - int findLowest(a, b, c, d, e) - intfindHighest(a, b, c, d, e) / 3;
cout << "The contestant's final score is: " << total << endl;
}
//defines getJudgeData. this function asks each judge for their score.
void getJudgeData(double score)
{
cout << "Judge, enter the contestant's score: ";
cin >> score;
if (score < 0 || score > 10) //validate score between 0-10, else display error msg
{
cout << "ERROR: Enter a value in the range 0-10: ";
cin >> score;
}
else
return;
}
//defines findLowest. this function returns the lowest score entered.
int findLowest(double score1, double score2, double score3, double score4, double score5)
{
if (score1 < score2, score3, score4, score5)
{
return score1;
}
else if (score2 < score1, score3, score4, score5)
{
return score2;
}
else if (score3 < score1, score2, score4, score5)
{
return score3;
}
else if (score4 < score1, score2, score3, score5)
{
return score4;
}
else if (score5 < score1, score2, score3, score4)
{
return score5;
}
}
//defines findHighest. this functio returns the highest score entered.
int findHighest(double score1, double score2, double score3, double score4, double score5)
{
if (score1 > score2, score3, score4, score5)
{
return score1;
}
else if (score2 > score1, score3, score4, score5)
{
return score2;
}
else if (score3 > score1, score2, score4, score5)
{
return score3;
}
else if (score4 > score1, score2, score3, score5)
{
return score4;
}
else if (score5 > score1, score2, score3, score4)
{
return score5;
}
}*
答案 0 :(得分:3)
这些功能分别称为findLowest
和findHighest
。 int
是它们的返回类型,在调用它们时不应指定它们。
如果这引起你的困惑,我建议你找一本好的初学者级C ++书,并阅读函数声明和调用。
答案 1 :(得分:2)
除了之前的答案,您的getJudgeData
功能非常糟糕。它需要一个双引用,否则你永远不会修改原始变量,并且该函数毫无价值。此外,你无法防止法官输入错误分数两次。
void getJudgeData(double& score)
{
cout << "Judge, enter the contestant's score: ";
cin >> score;
if (score < 0 || score > 10) //validate score between 0-10, else display error msg
{
cout << "ERROR: Enter a value in the range 0-10: ";
return getJudgeData(score);
}
return;
}
这个简单的递归可以保护你免受每次输入错误数据的判断,并且它也会正确地修改原始变量,因为你的代码似乎写得很好。
答案 2 :(得分:1)
问题在于您的界限:
double total = score1 + score2 + score3 + score4 + score5 - int findLowest(a, b, c, d, e) - intfindHighest(a, b, c, d, e) / 3;
您不应该在内嵌int
标识符。编译器已经知道你的函数返回一个int。改为写
double total = score1 + score2 + score3 + score4 + score5 - findLowest(a, b, c, d, e) - findHighest(a, b, c, d, e) / 3;
正如其他人指出的那样,你可能想把它强制转换为双倍。在这种情况下,你会这样做。
double total = score1 + score2 + score3 + score4 + score5 - (double)findLowest(a, b, c, d, e) - (double)findHighest(a, b, c, d, e) / 3;
尽管如此,这实际上不会计算平均值,因此您应该使用括号来强制执行订单或操作。
double total = (score1 + score2 + score3 + score4 + score5 - (double)findLowest(a, b, c, d, e) - (double)findHighest(a, b, c, d, e)) / 3;
答案 3 :(得分:1)
除了Chowlett提供的答案之外,您还需要重新考虑在findLowest和findHighest中使用逗号运算符。
例如,您在findHighest中的行
if (score1 > score2, score3, score4, score5)
基本上与
完全相同if (score5)
这不是你想要的。
答案 4 :(得分:0)
你想在那里演出吗?这是用括号(double)完成的,我认为double也是你想要施展的,虽然我不确定那里是否需要它。