-2

我不知道为什么这个程序不会运行。getStockInfo 中的值应该存储在参考参数中。然后 displayStatus 接受它们作为参数。我知道它与 main 中的 getStockInfo 和 displayStatus 有关,当它们被定义时,我就是想不通

#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

2 回答 2

0

这是您声明的方式displayStatus

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

这是您的使用方法displayStatus

displayStatus(spoolsOrdered, spoolsStock, specialCharges);

你看出区别了吗?

提示:计算每一行的参数个数。

于 2013-04-02T03:55:14.937 回答
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;

在您上面的代码中,我发现您有冗余。我标记了它。您有两个同名变量total,一个是 displayStatus 函数的参数,另一个是 displayStatus 函数的局部变量。

当我逐行查看您的代码时,这个变量没有在 main 函数中执行任何计算,也没有在 getStockInfo 函数中执行任何计算。变量的唯一计算发生在 displayStatus 函数中。因此,我建议你最好在 displayStatus 函数的参数上删除变量total。它不会损害您的代码,因为您已经有了局部变量total

编辑:主函数中不需要总变量,因此,删除此变量。

于 2013-04-02T05:28:37.887 回答