2013-04-02 188 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函数的参数。它不会伤害你的代码,因为你已经有了你的本地变量总计

编辑:你不需要变量的主要功能,所以,删除这个变量。