简体   繁体   English

在main中调用类函数

[英]Calling class functions in main

I have some class functions outside of the main I want to call back within it and I'm not doing it correctly. 我要在其外部调用一些主类之外的类函数,但操作不正确。 The code compiles, but doesn't display anything when run. 该代码可以编译,但运行时不显示任何内容。

#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();
}; 

The functions 功能

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);
}

Main 主要

int main()
{

    custmizedTotal.customerinput();
    if(again != "y"){
    custmizedTotal.customerinput();
    }
    return 0;
}

Here is your code stripped down to a minimal example (this may be the last time anyone does this for you on this site): 这是将您的代码简化为一个最小的示例(这可能是最后一次有人在此站点上为您这样做):

#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;
}

Do you see the problem? 看到问题了吗? The variable again is not initialized. 该变量again未初始化。 The code uses its value before any value has been assigned to it, which is "Undefined Behavior" (which is worse than it sounds), but it's almost certainly not "y", so control never enters the while statement; 代码在分配任何值之前就使用它的值,这是“未定义行为”(比听起来更糟),但是几乎可以肯定不是“ y”,因此控件永远不会输入while语句。 that code never runs. 该代码永远不会运行。 A better way to do it is like this: 更好的方法是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM