为什么我的数组不能正确增加?

时间:2018-12-10 23:33:28

标签: c++ arrays struct

我在C ++类中,并且在项目上遇到麻烦。该项目的想法是使用结构和数组创建订购应用程序。据我所知,程序正在按预期方式工作,除了该人订购了我的printMenu函数的一部分的每个项目中有多少个项目。如果我弄错了,或者您发现更多错误,请告诉我。

这是代码:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

struct dinnerItemType
{
    string dinnerItem;
    double dinnerPrice;
    int dinnerOrdered;
};

void getFood(dinnerItemType ourMenu[], int &size);
void printMenu(dinnerItemType ourMenu[], int size);
void printCheck(dinnerItemType ourMenu[], int size);

//Defines the global tax constant of 6%
const double TAX = 0.06;

int main()
{
    dinnerItemType ourMenu[150];
    int size = 0;

    getFood(ourMenu, size);

    printMenu(ourMenu, size);

    printCheck(ourMenu, size);


    system("pause");
    return 0;
}

void getFood(dinnerItemType ourMenu[], int &size)
{
    ourMenu[0].dinnerItem = "Chicken Sandwich";
    ourMenu[0].dinnerPrice = 4.45;
    ourMenu[0].dinnerOrdered = 0;

    ourMenu[1].dinnerItem = "Fries";
    ourMenu[1].dinnerPrice = 2.47;
    ourMenu[1].dinnerOrdered = 0;

    ourMenu[2].dinnerItem = "Truffle Fries";
    ourMenu[2].dinnerPrice = 0.97;
    ourMenu[2].dinnerOrdered = 0;

    ourMenu[3].dinnerItem = "Filet 8oz";
    ourMenu[3].dinnerPrice = 11.99;
    ourMenu[3].dinnerOrdered = 0;

    ourMenu[4].dinnerItem = "Fruit Basket";
    ourMenu[4].dinnerPrice = 2.44;
    ourMenu[4].dinnerOrdered = 0;

    ourMenu[5].dinnerItem = "Tea";
    ourMenu[5].dinnerPrice = 0.69;
    ourMenu[5].dinnerOrdered = 0;

    ourMenu[6].dinnerItem = "Water";
    ourMenu[6].dinnerPrice = 0.25;
    ourMenu[6].dinnerOrdered = 0;

    size = 7;
}

void printMenu(dinnerItemType ourMenu[], int size)
{

    int number;
    int amount;

    cout << "Welcome to the restraunt here are your menu items: \n";
    for (int i = 0; i < size; i++)
    {
        cout << (i + 1) << ")";
        cout << ourMenu[i].dinnerItem
             << "$"
             << ourMenu[i].dinnerPrice
             << endl;
    }

    cout << "To order just type in the number associated with the menu item and hit enter.\n"
         << "Once you have completed your order just type in 0 to go to checkout.\n";
    cin >> number;

    while (number != 0)
    {
        if (number >= 1 && number <= 8)
        {
            ourMenu[number - 1].dinnerOrdered++;
        }
        else
        {
            cout << "The number does not coorispond with a menu item please try again.\n";
        }

        cout << "To order just type in the number associated with the menu item and hit enter.\n"
             << "Once you have completed your order just type in 0 to go to checkout.\n";
        cin >> number;
    }
}

void printCheck(dinnerItemType ourMenu[], int size)
{
    double total = 0;

    cout << "Your Bill: ";

    for (int i = 0; i < size; i++)
    {
        if (ourMenu[i].dinnerOrdered > 0)
        {
            total += ourMenu[i].dinnerPrice;
        }
    }

    cout << "Tax: $ " << fixed << setprecision(2) << (total * TAX);
    cout << " Ammount Due: $" << (total + (total * TAX)) << endl;
    cout << "Thank you come again!" << endl;
}

1 个答案:

答案 0 :(得分:2)

此行:

total += ourMenu[i].dinnerPrice;

仅将一顿饭的费用加到总数上,而与订购一顿饭的次数无关。

要解决此问题:只需将价格乘以订购次数即可:

total += ourMenu[i].dinnerPrice * ourMenu[i].dinnerOrdered;