C++ 我的程序在将值传递到输出函数时返回0
我有一个我写的代码,它应该通过每个函数传递变量的值,并使用每个函数中的公式计算分数。在传递函数时,它会将每个变量传递到函数中,但随后不会输出正确的值。我不确定这是否与我试图通过输出函数传递分数的方式有关,或者是完全不同。这是我写的代码,任何帮助都是很棒的,因为我在一个基础级别的类中。我的函数名应该与我的教授给出的提示相同C++ 我的程序在将值传递到输出函数时返回0,c++,C++,我有一个我写的代码,它应该通过每个函数传递变量的值,并使用每个函数中的公式计算分数。在传递函数时,它会将每个变量传递到函数中,但随后不会输出正确的值。我不确定这是否与我试图通过输出函数传递分数的方式有关,或者是完全不同。这是我写的代码,任何帮助都是很棒的,因为我在一个基础级别的类中。我的函数名应该与我的教授给出的提示相同 #include <QCoreApplication> #include <QCoreApplication> #include <iostrea
#include <QCoreApplication>
#include <QCoreApplication>
#include <iostream>
using namespace std;
double touchdown;
double extraPoint;
double fieldGoal;
double twoPoints;
double score;
double points;
double footballScore(double touchdown, double extraPoint, double points);
double footballScore(double touchdown, double fieldGoal, double extraPoint, double points);
double footballScore(double touchdown, double fieldGoal, double extraPoint, double points, double twoPoints);
double output(double score, double points);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
touchdown=5;
extraPoint=3;
fieldGoal=4;
twoPoints=2;
footballScore(touchdown,extraPoint,points);
footballScore(touchdown,extraPoint,points,fieldGoal);
footballScore(touchdown,extraPoint,points,fieldGoal,twoPoints);
output(score,score);
cout<<"The score for the football team is: "<<score<<endl;
return a.exec();
}
//Function Definitions
double footballScore(double touchdown, double extraPoint, double points)
{
points=(touchdown*6)+extraPoint;
return points;
}
double footballScore(double touchdown, double fieldGoal, double extraPoint, double points)
{
points=(touchdown*6)+extraPoint+(fieldGoal*3);
return points;
}
double footballScore(double touchdown, double fieldGoal, double extraPoint, double points, double twoPoints)
{
points=(touchdown*6)+extraPoint+(fieldGoal*3)+(twoPoints*2);
return points;
}
double output(double score, double points)
{
score=points;
return score;
}
#包括
#包括
#包括
使用名称空间std;
双触地;
双外点;
双目标;
双重两点;
双倍得分;
双点;
足球双倍得分(双触地、双加分、双加分);
足球双倍得分(双触地得分、双外场得分、双加分、双积分);
足球双倍得分(双触地得分、双界外球得分、双加分、双二分);
双倍输出(双倍分数,双倍分数);
int main(int argc,char*argv[])
{
qcorea应用程序(argc、argv);
着陆=5;
外点=3;
fieldGoal=4;
两点=2;
足球得分(触地得分、附加分、得分);
足球得分(触地得分、界外得分、得分、场上进球);
足球得分(触地得分、加分、得分、射门得分、两分);
输出(分数,分数);
cout要得到答案,需要将结果保存在变量中,然后打印出来
score = footballScore(touchdown,extraPoint,points);
cout << "The score using formula 1 is" << score << endl;
score = footballScore(touchdown,extraPoint,points,fieldGoal);
cout << "The score using formula 2 is" << score << endl;
score = footballScore(touchdown,extraPoint,points,fieldGoal,twoPoints);
cout << "The score using formula 3 is" << score << endl;
score=足球得分(触地得分、附加得分、得分);
cout要得到答案,需要将结果保存在变量中,然后打印出来
score = footballScore(touchdown,extraPoint,points);
cout << "The score using formula 1 is" << score << endl;
score = footballScore(touchdown,extraPoint,points,fieldGoal);
cout << "The score using formula 2 is" << score << endl;
score = footballScore(touchdown,extraPoint,points,fieldGoal,twoPoints);
cout << "The score using formula 3 is" << score << endl;
score=足球得分(触地得分、附加得分、得分);
cout您应该使用返回的值
score=足球得分(触地得分、附加得分、得分);
输出(分数,分数);
得分=足球得分(触地得分、加分、得分、场上进球);
输出(分数,分数);
得分=足球得分(触地得分、加分、得分、场上进球、两分);
输出(分数,分数);
output
函数中score
的赋值不起任何作用,因为参数score
隐藏了全局score
变量。您应该使用返回的值
score=足球得分(触地得分、附加得分、得分);
输出(分数,分数);
得分=足球得分(触地得分、加分、得分、场上进球);
输出(分数,分数);
得分=足球得分(触地得分、加分、得分、场上进球、两分);
输出(分数,分数);
output
函数中score
的赋值没有任何作用,因为参数score
会对全局score
变量进行阴影处理。在main
中,您没有使用从函数返回的值。您希望在哪里看到任何更改?请尝试score=footballScore(触地得分、外点得分);
然后找出需要从其他函数调用中添加更多分数的内容。并且,所有变量都不应该是全局变量。在main
中,您没有使用从函数返回的值。您希望在何处看到任何更改?请尝试score=footballScore(触地得分、外点得分);
然后找出需要什么才能从其他函数调用中添加更多分数。此外,所有变量都不应是全局变量。