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

对不起,代码没有很好地转移,我对这个网站还是很陌生。但是,对于季度、硬币、镍和便士的结果,我得到了疯狂的数字。我已经这样做了一次,效果很好,但我没有使用 void 函数,所以我不得不重做它,我把自己搞砸了,我被卡住了。任何帮助表示赞赏!

4

1 回答 1

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

这意味着您提供实际变量,而不仅仅是副本。无论您对参数所做的任何更改实际上都是在您传入的变量上完成的。

查找引用和指针。

于 2013-10-07T07:07:38.607 回答