这是我们为100级课程设计的一个特殊问题,我们最近在代码中添加了函数。我似乎可以确切地想要做什么来让这个代码运行。另外,我的老师说我的变量getSID不需要返回值,这对我来说很困惑,任何帮助都非常感谢!!谢谢。
#include <iostream>
#include <iomanip>
using namespace std;
//here are the function prototypes for my problem
int getSID(int &);
int getResidentStatus(int &);
double calculateTuition(double, double, double, double, double, double);
double calculateTax(double &);
int main()
{//declared variables
char name[20], address[30], veteran, runningStart;
int credits, SID, residency;
double tuitionCost, taxDeductible, serviceFee, techFee, facilitiesFee, studentCenterFee, tuitionCostAndFees;
cout << "***********************************" << endl;
cout << "* *" << endl;
cout << "* Green River Community College *" << endl;
cout << "* *" << endl;
cout << "* 12401 SE 320th St. *" << endl;
cout << "* Auburn, WA, 98092-3622 *" << endl;
cout << "* *" << endl;
cout << "* Phone (253) 833-9111 *" << endl;
cout << "* *" << endl;
cout << "***********************************" << endl;
cout << "Enter name:";
cin.getline(name, 20);
getSID(SID);//my teacher says this doesn't need to use a return value?
getResidentStatus(residency);
cout << "Are you a veteran(y or n)?:";
cin >> veteran;
while (!(veteran == 'y' || veteran == 'n'))
{
cout << "Enter either 'y' for yes or 'n' for no: ";
cin >> veteran;
}
cin.ignore();
cout << "Are you involved in the running start program(y or n)?:";
cin >> runningStart;
while (!(runningStart == 'y' || runningStart == 'n'))
{
cout << "Enter either 'y' for yes or 'n' for no: ";
cin >> runningStart;
}
cin.ignore();
cout << "Enter home address(street address, and city):";
cin.getline(address, 30);
cout << "Enter number of credits:";
cin >> credits;
serviceFee = credits*0.5;
if (credits <= 12)
{
techFee = credits * 5;
}
else if (credits>12)
{
techFee = 60;
}
if (residency == 1 && credits>18)
{
tuitionCost = ((credits - 18)*268.26) + (149 * 8) + (278.84 * 10);
}
else if (residency == 1 && credits>10)
{
tuitionCost = ((credits - 10) * 149) + (278.84 * 10);
}
else if (residency == 1 && credits <= 10)
{
tuitionCost = (credits*278.84);
}
if (residency == 2 && credits>18)
{
tuitionCost = ((credits - 18)*109.26) + (53.68 * 8) + (119.84 * 10);
}
else if (residency == 2 && credits>10)
{
tuitionCost = ((credits - 10)*53.68) + (119.84 * 10);
}
else if (residency == 2 && credits <= 10)
{
tuitionCost = (credits*119.84);
}
if (residency == 3 && credits>18)
{
tuitionCost = ((credits - 18)*96.26) + (52.99 * 8) + (106.84 * 10);
}
else if (residency == 3 && credits>10)
{
tuitionCost = ((credits - 10)*52.99) + (106.84 * 10);
}
else if (residency == 3 && credits <= 10)
{
tuitionCost = (credits*106.84);
}
if (veteran == 'y'&&credits>18)
{
tuitionCost = ((credits - 18)*96.26) + (52.99 * 8) + (96.26 * 10);
}
else if (veteran == 'y'&&credits>10)
{
tuitionCost = ((credits - 10)*52.99) + (96.26 * 10);
}
else if (veteran == 'y'&&credits <= 10)
{
tuitionCost = (credits*96.26);
}
if (runningStart == 'y')
{
tuitionCostAndFees = 0;
}
if (credits <= 10)
{
facilitiesFee = 17.5 + (credits - 5)*3.5;
}
else
facilitiesFee = 35;
studentCenterFee = 25;
calculateTuition(tuitionCostAndFees, tuitionCost, serviceFee, facilitiesFee, studentCenterFee, techFee);//This line seems to be the main problem as to why it won't run!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
calculateTax(tuitionCostAndFees);
cout << "Your name is: " << name << endl;
cout << "Your student identification number is: " << SID << endl;
cout << "Your address is : " << address << endl;
cout << "You are taking " << credits << " credits this quarter. " << endl;
cout << "The total cost of your tuition is: $" << tuitionCostAndFees << endl;
cout << "The total tax deductible amount is: $" << fixed << setprecision(2) << taxDeductible << endl;
system("pause");
return 0;
}//function definitions
int getSID(int & SID)
{
cout << "Enter your student identification number: " << endl;
cin >> SID;
return SID;
}
int getResidentStatus(int & residency)
{
cout << "Are you an international student(1), non-washington resident(2) or washington resident(3)?: " << endl;
cin >> residency;
while (!(residency == 1 || residency == 2 || residency == 3))
{
cout << "Enter either 1, 2 or 3";
cin >> residency;
}
return residency;
}
double calculateTuition(double & tuitionCostAndFees, double & tuitionCost, double & serviceFee, double facilitiesFee, double & studentCenterFee, double & techFee)
{
tuitionCostAndFees = tuitionCost + serviceFee + facilitiesFee + studentCenterFee + techFee;
return tuitionCostAndFees;
}
double calculateTax(double & tuitionCostAndFees, double & taxDeductible)
{
taxDeductible = tuitionCostAndFees*.125;
return taxDeductible;
}
答案 0 :(得分:1)
您的老师意味着您应该像这样编码void getSID(int &);
并为其定义做同样的事情。
答案 1 :(得分:1)
您已将参数函数CalculateTuition声明为double:
calculateTuition(double, double, double, double, double, double);
在按值调用时将调用此方法。
然而,在main之后你已经尝试通过引用调用参数的函数定义中。这会产生错误。正如下面调用函数一样:
calculateTuition(tuitionCostAndFees, tuitionCost, serviceFee, facilitiesFee, studentCenterFee, techFee);
您正在发送双倍值(即声明为:double tuitionCost,taxDeductible,serviceFee,techFee,facilitiesFee,studentCenterFee,tuitionCostAndFees;)
这将尝试访问上面的函数定义。它不读取main之后定义的函数。
请改为尝试:
calculateTuition(double &, double &, double &, double &, double &, double &);
在你的函数声明中。这应该有用。