c++ - 未初始化?,tuitionCost,局部变量,

标签 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;
}

我调用的函数是

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;
}

类(class)数量的输入是 5 学分是 3,3,3.5,4,2.5 我得到了总学分,但似乎无法显示学费? 谢谢你

最佳答案

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

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

关于c++ - 未初始化?,tuitionCost,局部变量,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36555334/

相关文章:

python - 可以通过调用另一个函数来覆盖一个函数吗?

c++ - 编写自己的流媒体运算符时如何检查当前的 ostream dec/hex 模式?

c++ - 将所有依赖项打包在一个 visual studio 项目中以便轻松发送

c++ - 从赋值运算符调用析构函数有什么意想不到的后果吗?

php - get_the_id 与 post->ID 与 the_id/get_post_meta

linux - 在 bash 中使用带有命令的变量

php - 通过函数将 PHP 变量传递给 JavaScript

mysql - #2002 - 服务器没有响应(或者本地 MySQL 服务器的套接字配置不正确)

Azure功能本地开发

c++ - 使用 libav/ffmpeg 将 RGB8 转换为 NV12