我正在尝试为 3 个团队生成 3 个随机分数并确定哪个是最大的。我的做法是使用Predfined
函数,一个程序员定义的函数,然后声明和定义函数。我对此很陌生,我买的这本书并没有真正帮助我。
以下是代码端的目标概述:
调用预定义函数生成随机数序列
声明并定义一个返回值的函数
调用程序员定义的函数。
最终目标(取自书中):
编写一个名为的函数
max
,它接受三个类型int
的参数并返回参数中的最大值。您的程序必须同时具有此函数的声明和定义。函数声明必须放在main
函数上方。编写执行
main()
以下操作的函数:一种。生成一个 10 到 40 之间的随机整数作为 Hoosier、Boilermakers 和 Fighting Irish 三支球队的分数,并打印出这些分数。您的程序必须能够在不同时间运行时生成不同的分数序列。
湾。调用
max
任务 1 中定义的函数来查找所有团队中的最大分数,并打印出找到的最大分数。C。将最大的分数与 Hoosier 的分数进行比较,并打印出“Go Hoosier!!!” 如果 Hoosier 队的得分等于所有队的最大得分。
这是代码
/*
Author: Dan Wingeart
Assignment: Lab 9
*/
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int max(int Hscore, int Pscore, int Fscore);
int main()
{
int Fscore, Pscore, Hscore, highestScore;
Fscore = 10 + rand() % 40;
Pscore = 10 + rand() % 40;
Hscore = 10 + rand() % 40;
cout << "Prediction performance of sport teams:" << endl;
cout << "Team Hoosier's score is " << Hscore << endl;
cout << "Team Boilermakers' score is " << Pscore << endl;
cout << "Team Fighting Irish's score is " << Fscore << endl;
highestScore = max(Hscore, Pscore, Fscore)
if (max>Pscore&&max>Fscore){
cout << "The largest score is " << max << endl;
cout << "GO HOOSIER!!!" << endl;}
else
cout << "The largest score is " << max << endl;
return 0;
}
int max(int Hscore, int Pscore, int Fscore)
{
if (Hscore>Pscore&&Hscore>Fscore){
cout << Hscore;}
else if (Pscore>Hscore&&Pscore>Fscore){
cout << Pscore;}
else{
cout << Fscore;}
return 0;
}
产生的错误:
ClCompile:
1> Lab9.cpp
1>c:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.cpp(34): error C2143: syntax error : missing ';' before 'if'
1>c:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.cpp(34): error C2563: mismatch in formal parameter list
1>c:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.cpp(34): error C2563: mismatch in formal parameter list
1>c:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.cpp(35): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1> with
1>c:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.cpp(38): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1> with
1