uninitialized?,tuitionCost,local Variable,

时间:2016-04-11 17:29:01

标签: c++ variables local

int main()

{
    double tuitionCalc(int sumCreditHoursTaken);

    int numCourses;
    double total = 0.0;
    double tuitionCost= 0.0;


    cout << "\t\t This Program calculates a student's total number of\n";
    cout << "\t\tcredit hours and tution for a given semester.\n";

    cout << "\nPlease enter the number of Courses you will be taking this semester: ";
    cin >> numCourses;

    for ( int count = 1; count <= numCourses; count++)
    {
        double sumCreditHoursTaken;
        cout << " please enter the number of credit hours for course" << count << ": ";
        cin >> sumCreditHoursTaken;
        total += sumCreditHoursTaken;
    }

    cout << fixed << showpoint << setprecision(2);
    cout << "Your Total number of credit hours is: " << total << endl;
    cout << "Your total tuition will be: $" << tuitionCalc(tuitionCost) << "\n\n";

    return 0;
}

并且函数im调用

double tuitionCalc(int sumCreditHoursTaken)
{
    double tuitionCost = 0.0;
    double costCreditHour = 147.00;
    double maxHoursFullTuition = 12;
    double maintFeeAddOn = 29.33;`

    if (sumCreditHoursTaken <= maxHoursFullTuition)
        cout<< " " <<  (sumCreditHoursTaken * costCreditHour);
    else if (sumCreditHoursTaken > maxHoursFullTuition)
        cout << " " << (maxHoursFullTuition * costCreditHour) + ((sumCreditHoursTaken - maxHoursFullTuition) * maintFeeAddOn);

    return tuitionCost;
}

课程数量输入为5 和学分是3,3,3.5,4,2.5 我得到了总学分,但icant似乎显示了学费? 谢谢你

4 个答案:

答案 0 :(得分:2)

您实际上从未在tuitionCost方法中为tuitionCalc()分配值,因此它始终为0.0

详细说明:您将从tuitionCost返回tuitionCalc()。您首先初始化tuitionCost = 0.0,但永远不会继续为其分配任何计算值。因此,当您返回tuitionCost时,它会将您初始化的值返回到:0.0

答案 1 :(得分:0)

我没有详细检查过您的代码,但是如果输入包含浮点数,那么将sumCreditHoursTaken的类型从int更改为double。

另外,将调用tuitionCalc的参数从 tuitionCost 更改为 total

答案 2 :(得分:0)

OP似乎已经误解了误解范围以及变量如何传递给函数。

main中,OP定义tuitionCosttuitionCalc定义了另一个tuitionCost。这些是不同的tuitionCost。它们代表内存中的不同位置,可以存储不同的值。

接下来,因为定义了tuitionCalc函数

double tuitionCalc(int sumCreditHoursTaken)

tuitionCalc(tuitionCost)tuitionCost转换为整数,并将副本传递到tuitionCalc,其中将使用名称sumCreditHoursTaken }。可以说OP此时有三个tuitionCalc s。不是他们想要的。

分解tuitionCalc原型,我们看到它需要sumCreditHoursTaken,一个整数,并根据名称计算的信用小时数,而不是总费用。 tuitionCalc还会返回double并从中推断出该函数的用途;&#39; s的名称,可以预期它会计算并返回学费。

就像阿纳托利在答案中所述,tuitionCalc的输入几乎肯定是total,计算的学分总数,输出应为tuitionCost

由于这有家庭作业的气味,完全回答这个问题并不符合OP的最佳利益。相反,这里有一些建议:

tuitionCost取消main。它只会增加混乱。您可以重用变量名称,但只能在有明显优势的地方重复使用。如果你有一个cost和一个使用cost的函数,那么使用cost就可以了。请记住,除非您通过引用传递,否则函数内部的成本是不同的实体。在

void function(int & cost)

调用
function(cost);

两个cost都是一样的。但是在

void function(int cost)

调用
function(cost);

function的费用都是来电者cost的副本,而且功能所做的任何更改都只会影响副本。

声明变量靠近您使用它们的位置。这样,阅读代码的人不必向上和向下滚动,否则就会去打猎。它也可以帮助你,因为它会犯错误,&#34;为什么调用一个intdouble的函数?&#34;更明显。

cout中不要tuitionCalc。计算并返回tuitionCost。允许main输出tuitionCost。名称为calc的函数只应计算。名称应尽可能详细地描述功能。

答案 3 :(得分:0)

首先,您应该在声明之前创建函数原型。 在您使用的代码中存在一些混淆我尽力省略错误希望这对您有所帮助!

#include <iostream>
#include <iomanip>
using namespace std;
double tuitionCal(double sumCreditHoursTaken);

int main() {

     double tuitionCalc(int sumCreditHoursTaken);

    int numCourses;
    double total = 0.0;
    //double tuitionCost= 0.0;


    cout << "\t\t This Program calculates a student's total number of\n";
    cout << "\t\tcredit hours and tution for a given semester.\n";

    cout << "\nPlease enter the number of Courses you will be taking this semester: ";
    cin >> numCourses;

    double sumCreditHoursTaken; // you should create this variable outside the for loop

    for ( int count = 1; count <= numCourses; count++)
    {
        cout << " please enter the number of credit hours for course" << count << ": ";
        cin >> sumCreditHoursTaken;
        total += sumCreditHoursTaken;
    }

    double tuitionCost=tuitionCal(total);
    cout << fixed << showpoint << setprecision(2);
    cout << "Your Total number of credit hours is: " << total << endl;
    cout << "Your total tuition will be: $" <<tuitionCost<< "\n\n";// I assume this is what you want



    return 0;
}

double tuitionCal(double sumCreditHoursTaken)//the parameter type is double now
{
    double tuitionCost = 0.0;
    double costCreditHour = 147.00;
    double maxHoursFullTuition = 12;
    double maintFeeAddOn = 29.33;

    if (sumCreditHoursTaken <= maxHoursFullTuition)
        tuitionCost=(sumCreditHoursTaken * costCreditHour);
    else if (sumCreditHoursTaken > maxHoursFullTuition)
       tuitionCost=(maxHoursFullTuition * costCreditHour) + ((sumCreditHoursTaken - maxHoursFullTuition) * maintFeeAddOn);

    return tuitionCost;
    //I don't see a point of returning the value and couting both you can 
    //do only one of the oprations
}