我在编写这个程序时遇到了麻烦。我的问题似乎是使用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;
}
}
答案 0 :(得分:2)
简单地说:
void ProcessOrder(Order & myOrder);
和
void DisplayOrders(Order orderList[]);
是函数声明。即:你用这些行声明函数,而不是调用已经声明的函数 在这种情况下,请改用:
ProcessOrder(myOrder);
DisplayOrders(orderList);
如果您是c ++的新手,我建议您访问此link,这是一种有效学习应用程序的好方法。
希望这有用。