在 main 中调用类函数
Calling class functions in 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;
}
以下是您的代码,简化为一个最小示例(这可能是最后一次有人在此站点上为您执行此操作):
#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");
相关文章:
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 在c++类上调用void函数
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- C++/CLI System.AccessViolation在托管类中调用非托管函数时出现异常
- 使用基类指针调用基类的值构造函数的语法是什么?
- 为用户定义的类正确调用复制构造函数/赋值运算符
- 仅在可变模板类中调用现有函数
- 无法为类成员调用函数
- C++调用使用重写函数的父类函数
- 在具有向量的类构造函数中进行析构函数调用
- 计算对类函数的所有调用次数
- 检查类是否具有可能重载的函数调用运算符
- 从多重继承中的派生类函数调用适当的父类函数
- 在类函数调用期间丢失私有变量信息
- 从基类函数调用派生类函数
- 基类函数调用的替代设计模式
- 从派生类函数调用基函数
- 如何从基类函数调用重写虚函数
- C++:从非类函数调用类方法
- Windows内CreateThread类函数调用,指针引用崩溃