#include <iostream>
using namespace std;
void compute_coins(int change, int quarters, int dimes, int nickels, int pennies);
void output(int quarters, int dimes, int nickels, int pennies);
int main()
{
int change, quarters, dimes, nickels, pennies;
char again = 'y';
cout << "Welcome to the change dispenser!\n";
while(again == 'y'){//Creating loop to allow the user to repeat the process
cout << "Please enter the amount of cents that you have given between 1 and 99\n";
cin >> change;
while((change < 0) || (change >100)){//Making a loop to make sure a valid number is inputed
cout << "Error: Sorry you have entered a invalid number, please try again:";
cin >> change;
}
cout << change << " Cents can be given as: " << endl;
compute_coins(change, quarters, dimes, nickels, pennies);
output(quarters, dimes, nickels, pennies);
cout << "Would you like to enter more change into the change dispenser? y/n\n";//prompts the user to repeat this process
cin >> again;
}
return 0;
}
void compute_coins(int change, int quarters, int dimes, int nickels, int pennies) {//calculation to find out the amount of change given for the amount inpuied
using namespace std;
quarters = change / 25;
change = change % 25;
dimes = change / 10;
change = change % 10;
nickels = change / 5;
change = change % 5;
pennies = change;
return ;
}
void output(int quarters, int dimes, int nickels, int pennies){
using namespace std;
cout << "Quarters = " << quarters << endl;
cout << "dimes = " << dimes << endl;
cout << "nickels = " << nickels << endl;
cout << "pennies = " << pennies << endl;
}
对不起,代码传输不好,我对这个网站还很新。但是,对于季度,角钱,镍币和便士的结果,我得到了疯狂的数字。我已经这样做了一次,它工作正常,但我没有使用虚函数,所以我不得不重做它,我搞砸了自己,我被卡住了。任何帮助赞赏!
答案 0 :(得分:0)
void compute_coins(int change, int quarters, int dimes, int nickels, int pennies);
这意味着您只会获取传递给函数的值的副本。因此,无论你在函数中做什么都不会对你传入的实际值产生任何影响。
void compute_coins(int &change, int &quarters, int &dimes, int &nickels, int &pennies);
这意味着您提供实际变量,而不仅仅是副本。您对参数所做的任何更改实际上都是对您传入的变量进行的。
查找引用和指针。