在过去的几个小时里,我在学习了星期五的功能后,一直在研究这个程序,而且我不确定如何从我的函数中调用特定的变量。在下面的代码中,我需要调用我在cash()函数中计算的值,这些是;数百,五十,二十,几十,五,toonies和loonies。谢谢。我的问题是如何从我的函数调用特定值,下面的代码不起作用。
int main()
{
//DECLARATIONS
int totalDollarAmount;
int hundreds = 0;
int fifties = 0;
int twenties = 0;
int tens = 0;
int fives = 0;
int loonies = 0;
int toonies = 0;
//prompt for input
cout << "Please input a dollar amount: " << totalDollarAmount << endl;
hundreds = cash(hundreds);
hundreds = cash(fifties);
hundreds = cash(twenties);
hundreds = cash(tens);
hundreds = cash(fives);
hundreds = cash(toonies);
hundreds = cash(loonies);
cout << " The total number of Fifties is: " << fifties << endl;
cout << "The total number of Twenties is: " << twenties << endl;
cout << "The total number of Tens is: " << tens << endl;
cout << "The total number of Fives is: " << fives << endl;
cout << "The total number of Toonies is: " << toonies << endl;
cout << "The total number of Loonies is: " << loonies << endl;
return 0;
}//end main
//code function
int cash(int hundreds, int fifties, int twenties, int tens, int fives, int loonies, int toonies, int totalDollarAmount)
{
totalDollarAmount * 100;
hundreds = totalDollarAmount/10000;
totalDollarAmount = totalDollarAmount % 10000;
fifties = totalDollarAmount/5000;
totalDollarAmount = totalDollarAmount % 5000;
twenties = totalDollarAmount/2000;
totalDollarAmount = totalDollarAmount % 2000;
tens = totalDollarAmount/1000;
totalDollarAmount = totalDollarAmount % 1000;
fives = totalDollarAmount/500;
totalDollarAmount= totalDollarAmount % 500;
toonies = totalDollarAmount/200;
totalDollarAmount = totalDollarAmount % 200;
loonies = totalDollarAmount/100;
totalDollarAmount = totalDollarAmount % 100;
}//end function
答案 0 :(得分:3)
您的代码存在一些问题。
cash
函数接受8个参数,而您在每个函数调用中只传递一个参数。cash
函数返回int
,但在函数内部您没有返回任何内容。cin
输入值而不是cout
。对代码进行一次快速修复可能就像将cash
功能更改为
void cash(int &hundreds, int &fifties, int &twenties, int &tens, int &fives, int &loonies, int &toonies, int &totalDollarAmount)
然后在main
函数中,执行以下更改:
将输入提示更改为
cout << "Please input a dollar amount: ";
cin >> totalDollarAmount;
将调用的函数更改为
cash(hundreds, fifties, twenties, tens, fives, loonies, toonies, totalDollarAmount);
P.S。您的cash
函数中可能仍存在一些逻辑问题,例如您从未分配给变量的第一行totalDollarAmount * 100;
。但是那些程序逻辑我会留给你解决。