【问题标题】:Calling class functions in main在 main 中调用类函数
【发布时间】:2016-02-24 02:14:39
【问题描述】:

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

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

【问题讨论】:

  • 欢迎来到 Stack Overflow 和编程。这里有一个比任何一个错误修复都更有价值的指导方针:当你编写代码时,从一些小而简单的完美运行的东西开始,然后一次增加一点复杂性。这种方法的一个后果是,当你无法让某些东西工作时,你有一个接近 minimal complete example 的东西已经盯着你了。

标签: c++ dev-c++


【解决方案1】:

这是您的代码精简到一个最小的示例(这可能是最后一次有人在此站点上为您这样做):

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

【讨论】:

  • 这解决了我的显示问题,谢谢。但是,现在无论再次输入如何,它都会循环。
  • @jibicax:你在用我的最小例子吗?
  • 不是,我是放在原版的。
  • @jibicax:所以第二个错误在旧代码的其他地方,而不是在补丁中。所以按照我的做法,将代码减少到第二个错误的最小示例,如果问题的原因在此过程中没有变得明显,您可以发布新示例。
猜你喜欢
  • 2019-04-25
  • 1970-01-01
  • 1970-01-01
  • 2014-09-10
  • 2019-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-07
相关资源
最近更新 更多