#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double tuitionCalc(int sumCreditHoursTaken); //function prototype
double tuitionCost;
double creditHours;
int numCourses;
int count;
int sumCreditHoursTaken = 0;
cout << " This program calculates a students total number of\n\n";
cout << " credit hours and tuition for a given semester.\n\n\n";
cout << "Please enter the amount of courses you will be taking this semester: ";
cin >> numCourses;
for (count = 1; count <= numCourses; count++) //for loop to find the total credit hours taken
{
cout << "\nPlease enter the number of credit hours for class " << count << ": ";
cin >> creditHours;
sumCreditHoursTaken += creditHours;
}
cout << "\n\nYour total number of credit hours taken is: " << sumCreditHoursTaken << "\n\n";
cout << "Your total tuition will be: $" << tuitionCalc(tuitionCost) << "\n\n";
system("PAUSE");
return 0;
}
它说问题发生在我尝试调用函数tuitionCalc()
的上方。
这是我试图打电话的功能:
double tuitionCalc(int sumCreditHoursTaken)
{
double tuitionCost = 0;
double costCreditHour = 147.00;
double maintFeeAddOn = 29.33;
int maxHoursFullTuition = 12;
if (sumCreditHoursTaken <= maxHoursFullTuition)
sumCreditHoursTaken * costCreditHour;
else
(maxHoursFullTuition * costCreditHour) + ((sumCreditHoursTaken - maxHoursFullTuition) * maintFeeAddOn);
return tuitionCost;
}
答案 0 :(得分:2)
在第
行cout << "Your total tuition will be: $" << tuitionCalc(tuitionCost) << "\n\n";
您将函数tuitionCalc
与未初始化的参数tuitionCost
一起使用。所以编译器试图警告你。这是技术上未定义的行为。您需要确保传递给函数的任何内容都具有明确定义的值。在您的情况下,因为tuitionCost
未初始化,您传递的任何垃圾值恰好存储在内存位置&tuitionCost
。请记住,C ++ 不会将变量初始化为零。如果按值将它们传递给函数,则需要手动初始化它们。我的猜测是你实际上想要将sumCreditHoursTaken
(你刚刚在函数调用之上计算)传递给tuitionCalc
函数。
答案 1 :(得分:0)
您将tuitionCost的值传递给tuitionCalc方法,但您没有在tuitionCost中添加任何内容。由于变量是在本地声明的,因此它将具有随机值。编译器希望您知道。