在 main 中调用类函数

Calling class functions in main

本文关键字:类函数 调用 main      更新时间:2023-10-16

我在主函数之外有一些类函数,我想在其中回调,但我做得不正确。代码将编译,但在运行时不显示任何内容。

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

主要

int main()
{
    custmizedTotal.customerinput();
    if(again != "y"){
    custmizedTotal.customerinput();
    }
    return 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");