首页 > 解决方案 > 将值传递给输出函数时,我的程序返回 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;
    }

标签: c++

解决方案


要得到答案,您需要将结果保存在变量中,然后打印出来。

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功能没有做任何事情。

阅读有关函数的教程可能是个好主意。是一个很好的。


推荐阅读