2013-10-07 83 views
-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函數數學錯誤

+0

C++是通過按值,除非指定的參考。 – chris

+1

什麼*具體*會出錯?你到目前爲止已經嘗試了哪些*具體內容?我們很樂意幫忙,但沒有一些指導它爲我們解答你的問題很難。你能否用更多的細節更新你的問題? – templatetypedef

+0

你的編譯器能夠提供有用的警告,如果你讓它:http://coliru.stacked-crooked.com/a/6449ef3601fb9ef4 – chris

回答

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

這意味着,你給實際變量,而不僅僅是一個副本。無論你在做參數的變化實際上是對您在傳遞變量來完成。

查找refences和指針。