【问题标题】:Program failing to call void function [closed]程序无法调用 void 函数 [关闭]
【发布时间】: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 &amp; myOrder); 是函数声明而不是调用。调用写:ProcessOrder(myOrder);
  • 非常感谢。天哪,我知道这是小事。更正呼叫后,现在一切似乎都正常了。再次感谢。

标签: c++ function void


【解决方案1】:

简单地说:

void ProcessOrder(Order & myOrder);

void DisplayOrders(Order orderList[]);

是函数声明。即:您使用这些行声明函数,而不是调用已声明的函数。
在这种情况下,请改用这些:

ProcessOrder(myOrder);
DisplayOrders(orderList);

如果您是 C++ 新手,我建议您访问此link,这是一种高效学习应用程序的好方法。

希望这对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 2012-02-18
    • 2021-06-06
    相关资源
    最近更新 更多