在main中调用类函数

时间:2016-02-24 02:14:39

标签: c++ dev-c++

我有一些类主要功能,我想在其中回拨,我没有正确地做。代码编译,但在运行时不显示任何内容。

revokeObjectURL

功能

#include <iostream>
#include<string>
#include <vector>
using namespace std;

string again;
char pType, pSize, topping, temp;
const int SMALL = 1;
int type = 0, size = 0;
const int MEDIUM = 2;
const int LARGE = 3;
const int DEEPDISH = 1;
const int HANDTOSSED = 2;
const int PAN = 3;
double total = 0;

class Pizza
{
    private:
          int type;
          int size;
          bool cheese;    
          bool pepperoni; 

    public:
          Pizza();
          int getType();
          int getSize();
          bool getCheese();
          bool getPepperoni();
          void setType(int t);
          void setSize(int s);
          void setCheese(bool choice);
          void setPepperoni(bool choice);

  void outputDescription();
  double computePrice();
}; 
class Order
{
private:
    vector<Pizza> c;  
public:
    Order();
    void customerOrder();
    void customerTotal();
    void customerinput();
}; 

主要

Order custmizedTotal;
Pizza myPizza;  
bool done=false;
void Order::customerinput(){
while ( again == "y"){  
        cout << "What sized pizza, please enter S, M OR L: ";
        cin >> pSize;
        cin.clear();

        switch(pSize)
        {
            case 'S': case 's':
            size = SMALL; break;
            case 'M': case 'm':
            size = MEDIUM; break;
            case 'L': case 'l':
            size = LARGE; break;
        }

        cout << "What type of pizza, enter D for Deepdish, H for Hand tossed, and P for Pan: ";
        cin >> pType;
        cin.clear();

        switch(pType)
        {
            case 'D': case 'd':
            type = DEEPDISH; break;
            case 'H': case 'h':
            type = HANDTOSSED; break;
            case 'P': case 'p':
            type = PAN; break;
        }

        myPizza.setSize(size);
        myPizza.setType(type);

        cout << "Would you like cheese (y/n)? ";
        cin >> topping;
        cin.clear();

        if (topping == 'Y' || topping == 'y')
            myPizza.setCheese(true);

        cout << "Would you like pepperoni (y/n)? ";
        cin >> topping;
        cin.clear();

        if (topping == 'Y' || topping == 'y')
            myPizza.setPepperoni(true);

      cout << endl
       << "Your order: ";
      myPizza.outputDescription();
      cout << endl;
      cout << "Price: $" << myPizza.computePrice() << endl;

      cout << "Again? (y/n)";
      cin >> again;    
    }
        }

Order::Order(){
double total = 0;   
}


void Order::customerTotal(){

    cout << "Your Total order is: " << endl;
    for(int i=0; i<c.size(); i++)
    { 
        c[i].outputDescription();
        cout << endl;
        cout << c[i].computePrice();
        cout << endl;
        total=total+c[i].computePrice();
    }       
    cout << "Totat Cost: $" << total;
    cout << endl;
    c.push_back(myPizza);
}

1 个答案:

答案 0 :(得分:0)

以下是您的代码被剥离到一个最小的示例(这可能是最后一次有人在此网站上为您执行此操作):

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

int main()
{
  string again;
  while ( again == "y"){
    cout << "taking an order..." << endl;
    cout << "Again? (y/n)";
    cin >> again;
  }

  return 0;
}

你看到了问题吗?变量again未初始化。代码在为其分配任何值之前使用其值,这是“未定义的行为”(比它听起来更糟),但它几乎肯定不是“y”,因此控制永远不会进入while语句;该代码永远不会运行。更好的方法是:

string again;
do{
  cout << "taking an order..." << endl;
  cout << "Again? (y/n)";
  cin >> again;
}while ( again == "y");