2013-04-02 222 views
-2

我不知道爲什麼這個程序不會運行。 getStockInfo中的值應該存儲在參考參數中。然後displayStatus接受它們作爲參數。我知道它是與getStockInfo和displayStatus主,當他們被定義,我只是不能想出辦法來C++參考參數函數

#include <iostream> 
#include <iomanip> 
using namespace std; 

void getStockInfo(int &, int&, double&); 
void displayStatus(int, int, double, double); 

int main() 
{ 
//Declare Variables 
int orderedSpools; 
int spoolsStock; 
double specialCharges; 
int spoolsOrdered; 

int backOrder; 
double subtotal, 
     shipping, 
     total; 


cout << "Middletown Wholesale Copper Wire Company" << endl; 

getStockInfo(spoolsOrdered, spoolsStock, specialCharges); 

displayStatus(spoolsOrdered, spoolsStock, specialCharges); 

    system("pause"); 
    return 0; 
} 

void getStockInfo(int &spoolsOrdered, int &spoolsStock, double &specialCharges) 
{ 
char ship; 

cout << "How many spools would you like to order: "; 
cin >> spoolsOrdered; 

//Validate the spools ordered 
while(spoolsOrdered < 1) 
{ 
    cout << "Spools ordered must be at least one" << endl; 
    cin >> spoolsOrdered; 
} 

cout << "How many spools are in stock: "; 
cin >> spoolsStock; 

//Validate spools in stock 
while(spoolsStock < 0) 
{ 
    cout << "Spools in stock must be at least 0" << endl; 
    cin >> spoolsStock; 
} 

cout << "Are there any special shipping charges? "; 
cout << "Enter Y for yes or another letter for no: "; 
cin >> ship; 

//Validate special charges 
if(ship == 'Y' || ship == 'y') 
{ 
    cout << "Enter the special shipping charge: $"; 
    cin >> specialCharges; 
} 
else 
{ 
    specialCharges = 10.00; 
} 
} 

void displayStatus(int spoolsOrdered, int spoolsStock, double specialCharges, 
        double total) 
{ 
double backOrder, 
     subtotal, 
     shipping, 
     total; 
int itemsReady; 


cout << "Items ordered: " << spoolsOrdered << endl; 
cout << "Items ready to ship: " << spoolsStock << endl; 


if(spoolsOrdered > spoolsStock) 
{ 
    backOrder = spoolsOrdered - spoolsStock; 
    cout << "Items on backorder: " << backOrder << endl; 
} 


subtotal = itemsReady * 100; 
cout << "Subtotal: " << subtotal << endl; 

shipping = specialCharges; 
cout << "Shipping: " << shipping << endl; 

total = subtotal + shipping; 
cout << "Total Due: " << total << endl; 



} 
+4

如果通過「不會運行」,你的意思是有一個編譯器錯誤,顯示錯誤。 – chris

+0

你剛剛問了[關於此問題的另一個問題](http://stackoverflow.com/questions/15755391)。在開除另一個問題之前,你必須做一些研究,至少要幫助那些幫助你解決最初問題的人......並且進一步看待這個問題,這不是同一個問題,而是密切相關的問題。更改原型和定義後,您甚至沒有努力改變調用代碼。 –

+1

@Fred Thomsen噢,去其他地方發牢騷吧。它根本就沒有密切關係。我現在修好了,現在我被困在一個部分,需要幫助 – user1807815

回答

0

這裏是你如何申報displayStatus

void displayStatus(int, int, double, double); 

這裏是你如何使用displayStatus

displayStatus(spoolsOrdered, spoolsStock, specialCharges); 

你看到有區別嗎?

提示:計數的每行參數的數量。

+0

是的,我看到它,我不知道我在想什麼。謝謝您的幫助 – user1807815

0
void displayStatus(int spoolsOrdered, int spoolsStock, double specialCharges, 
        double total) //**<-- here: total as parameter** 
{ 
double backOrder, 
     subtotal, 
     shipping, 
     total; //**<-- and here: total as local variable** 
int itemsReady; 

在上面的代碼中,我發現你有冗餘。我標記了它。 您有兩個同名的變量,,其中一個是displayStatus函數的參數,另一個是displayStatus函數的局部變量。

當我一行一行地看到你的代碼時,這個總共變量在getStockInfo函數中既沒有在main函數中執行任何計算。在displayStatus函數中只發生了總計變量的計算。所以我建議你,更好變量總共關於displayStatus函數的參數。它不會傷害你的代碼,因爲你已經有了你的本地變量總計

編輯:你不需要變量的主要功能,所以,刪除這個變量。