我不知道为什么这个程序不会运行。 getStockInfo中的值应该存储在参考参数中。然后displayStatus接受它们作为参数。我知道它与getStockInfo和displayStatus在main中有关,当它们被定义时,我只是想不通它
#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;
}
答案 0 :(得分:0)
以下是您宣布displayStatus
:
void displayStatus(int, int, double, double);
以下是您使用displayStatus
的方式:
displayStatus(spoolsOrdered, spoolsStock, specialCharges);
你看到了区别吗?
提示:计算每行中的参数数量。
答案 1 :(得分: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函数的局部变量。
当我逐行看到你的代码时,这个 total 变量在getStockInfo函数中都没有在main函数中执行任何计算。 total 变量的唯一计算是在displayStatus函数中进行的。因此,我建议你,在displayStatus函数的参数上更好地删除变量 total 。它不会损害您的代码,因为您已经拥有本地变量 total 。
编辑:您在主函数中不需要 total 变量,因此, delete 此变量。