【发布时间】:2017-07-08 21:10:49
【问题描述】:
我在编写这个程序时遇到了麻烦。我的问题似乎与 processOrder 和 displayOrder 函数有关,在运行此代码时,getOrder 函数运行得很好,但程序根本不调用其他函数,只是在输入数据后终止。我确定我错过了一些简单的东西,但我看不到它,任何帮助将不胜感激。
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Order {
string name;
double unitPrice;
int quantity;
double totalPrice;
};
const int NumOrder = 3;
Order GetOrder();
void ProcessOrder(Order& myOrder);
void DisplayOrders(Order orderList[]);
int main()
{
Order orderList[NumOrder];
for (int i = 0; i < NumOrder; i++) {
// create an order variable
Order myOrder;
cout << "Enter information for order #" << i + 1 << ": " << endl;
// call GetOrder() function to enter order’s information
myOrder = GetOrder();
cin.ignore(1000, '\n');
cout << endl;
// call ProcessOrder() function to update total price of an order
void ProcessOrder(Order & myOrder);
// add the new order in the order array
orderList[i] = myOrder;
}
// call DisplayOrders() function to display all of three orders’ information
void DisplayOrders(Order orderList[]);
return 0;
}
Order GetOrder()
{
Order myOrder;
cout << "Item Nane: ";
getline(cin, myOrder.name);
cout << "Unit Price: ";
cin >> myOrder.unitPrice;
cout << "Quantity: ";
cin >> myOrder.quantity;
myOrder.totalPrice = 0.00;
return myOrder;
}
void ProcessOrder(Order& myOrder)
{
myOrder.totalPrice = (myOrder.quantity * myOrder.unitPrice) + (myOrder.quantity * myOrder.unitPrice) * 0.007;
}
void DisplayOrders(Order orderList[NumOrder])
{
// complete function definition here
int j;
for (j = 0; j < NumOrder; j++) {
string iName = orderList[j].name;
cout << "Item name: " << iName << endl;
cout << "Unit price: $" << orderList[j].unitPrice << endl;
cout << "Quantity: " << orderList[j].quantity << endl;
cout << "Total price: $" << orderList[j].totalPrice << endl;
}
}
【问题讨论】:
-
void ProcessOrder(Order & myOrder);是函数声明而不是调用。调用写:ProcessOrder(myOrder); -
非常感谢。天哪,我知道这是小事。更正呼叫后,现在一切似乎都正常了。再次感谢。