c++ - 将值传递给输出函数时,我的程序返回 0
问题描述
我有一个我写的代码,它应该通过每个函数传递变量的值,并使用每个函数中的公式计算分数。在传递函数时,它将每个变量传递给函数,但随后它不会输出正确的值。我不确定这是否与我尝试通过输出函数传递分数的方式有关,或者它是否完全不同。这是我编写的代码,任何帮助都很棒,因为我在基础级别的课程中。我的函数名称应该与我的教授给出的提示相同。
#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;
}
解决方案
要得到答案,您需要将结果保存在变量中,然后打印出来。
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;
该output
功能没有做任何事情。
阅读有关函数的教程可能是个好主意。这是一个很好的。
推荐阅读
- c# - 将 TResult 转换为 Func
在 Xamarin.forms 中 - laravel - 如何让单元测试忽略 Laravel 中的中间件
- typescript - 如何从接口的属性中提取“枚举类型”?
- python - 如何使用 Python 将二元组列表转换为标记列表
- python - 在这个问题中如何在python中实现
- vue.js - 无法在 b-modal 中访问子组件 $refs
- flutter - Flutter - 如何为 BottomNavigationBar 添加分隔符?
- apache-spark - 将 apache spark rdd 写入多个 s3 文件夹
- javascript - 更改嵌套函数中的变量
- python - 如何使用 TextBlob 对整个 pandas 数据框列进行单数化和词形化?