首页 > 解决方案 > C++ 程序:披萨成本

问题描述

我无法弄清楚如何修复我在使用此代码时遇到的其余错误。我已经更改并修复了很多错误,但我被卡住了。我不明白它给我的语法错误。错误的语法与我们在教科书和教师笔记中学到的相匹配。该程序的规则如下: 定义一个名为 Pizza 的类,该类具有成员变量来跟踪比萨饼的类型(深盘、手抛或平底锅)以及大小(小、中或大)和数量意大利辣香肠或奶酪配料。您可以使用常量来表示类型和大小。为您的类包括 mutator (set) 和 accessor (get) 函数。创建一个 void 函数 outputDescription( ),它输出比萨对象的文本描述。还包括一个函数,computePrice(),

#include <iostream>
#include <Source.h>

using namespace std;

const int DEEPDISH=1, HANDTOSSED=2, PAN=3;
const int SMALL=1, MEDIUM=2, LARGE=3;

class Pizza
{
public:
    Pizza();
    int getCrust(), getSize();
    double computePrice();
    bool getCheese(), getPepperoni();
    void outputDesription();
    void setCrust(int c);
    void setSize(int s);
    void setCheese(bool choice);
    void setPepperoni(bool choice);

private:
    int crust, size;
    bool toppingCheese, toppingPepperoni;
};

Pizza::Pizza()
{
    crust = DEEPDISH;
    size = SMALL;
    toppingCheese = toppingPepperoni = false;
}
int Pizza::getCrust()
{
    return crust;
}
int Pizza::getSize()
{
    return size;
}
bool Pizza::getCheese()
{
    return toppingCheese;
}
bool Pizza::getPepperoni()
{
    return toppingPepperoni;
}
void Pizza::setCrust(int c)
{
    crust = c;
}
void Pizza::setSize(int s)
{
    size = s;
}
void Pizza::setCheese(bool choice)
{
    toppingCheese = choice;
}
void Pizza::setPepperoni(bool choice)
{
    toppingCheese = choice;
}
switch (size)
{
case SMALL:
    cout << "Small ";
    break;
case MEDIUM:
    cout << "Medium ";
    break;
case LARGE:
    cout << "Large ";
        break;
default:
    cou << "Size not recognized";
    break;
}
switch (crust)
{
case DEEPDISH:
    cout << "Deepdish ";
    break;
case HANDTOSSED:
    cout << "Hand tossed ";
    break;
case PAN:
    cout << "Pan ";
    break;
default:
    cout << "Crust style unknown ";
    break;
}
double Pizza::computePrice()
{
    double cost = 0.0;

    switch (size)
    {
    case SMALL:
        cost += 10;
        break;
    case MEDIUM:
        cost += 14;
        break;
    case LARGE:
        cost += 17;
        break;
    }
    if (toppingCheese)
        cost += 2.0;
    if (toppingPepperoni)
        cost += 2.0;
    return cost;
}

int main()
{
    char crustStyle, pizzaSize, topping;
    int crust = 0, size = 0;

    cout << "What size pizza would you like (S/M/L): ";
    cin >> pizzaSize;
    cin.clear();

    switch (pizzaSize)
    {
    case 'S':
    case's':
        size = SMALL;
        break;
    case'M':
    case 'm':
        size = MEDIUM;
        break;
    case 'L':
    case 'l':
        size = LARGE;
        break;
    }
    {
        cout << "What style crust would you like ((D)eep dish/(H)and tossed/(P)an): ";
        cin >> crustStyle;
        cin.clear();
    }
    switch (crustStyle)
    {
    case'D':
    case'd':
        crust = DEEPDISH;
        break;
    case'H':
    case'h':
        crust = HANDTOSSED;
        break;
    case'P':
    case'p':
        crust = PAN;
        break;
    }
    int Pizza::custPizza();
    {
        custPizza.setSize(size);
        custPizza.setCrust(crust);

        cout << "Add cheese topping (Y/N)? ";
        cin >> topping;
        cin.clear();
        if (topping == 'Y' || topping == 'y')
            custPizza.setCheese(true);

        cout << "Add pepperoni (Y/N)? ";
        cin >> topping;
        cin.clear();
        if (topping == 'Y' || topping == 'y')
            custPizza.setPepperoni(true);

        cout << endl << "Your Pizza: ";
        custPizza.outputDescription();
        cout << endl;
        cout << "Order total: $" << custPizza.computePrice() << endl;
    }
    system("Pause");
    return 0;
}

标签: c++

解决方案


Yove' 得到了一对switch不在函数定义中的随机语句。setPepperoni它只是夹在和之间的随机代码computePrice。当然编译器会抱怨。

void Pizza::setPepperoni(bool choice)
{
    toppingCheese = choice;
}
switch (size)                  // WHAT FUNCTION IS THIS STATEMENT IN?
{
case SMALL:
    cout << "Small ";
    break;
case MEDIUM:
    cout << "Medium ";
    break;
case LARGE:
    cout << "Large ";
    break;
default:
    cou << "Size not recognized";
    break;
}
switch (crust)              // THIS ONE TOO.
{
case DEEPDISH:
    cout << "Deepdish ";
    break;
case HANDTOSSED:
    cout << "Hand tossed ";
    break;
case PAN:
    cout << "Pan ";
    break;
default:
    cout << "Crust style unknown ";
    break;
}
double Pizza::computePrice()
{
    double cost = 0.0;

    switch (size)
    {
    case SMALL:
        cost += 10;
        break;
    case MEDIUM:
        cost += 14;
        break;
    case LARGE:
        cost += 17;
        break;
    }
    if (toppingCheese)
        cost += 2.0;
    if (toppingPepperoni)
        cost += 2.0;
    return cost;
}

在其他地方main,您似乎正在尝试使用定义来定义函数。

case'p':
    crust = PAN;
    break;
}
int Pizza::custPizza();     // a new function defintion within main???
{
    custPizza.setSize(size);
    custPizza.setCrust(crust);

推荐阅读