我在c ++上很新,所以请跟我说说穴居人。我正在尝试让代码在循环中运行,并且当循环完成时,计算总数和所有顺序。我遇到了这个错误,我不知道为什么。
[错误]预期在'。'之前的不合格ID令牌
#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();
};
Pizza::Pizza()
{
type = DEEPDISH;
size = SMALL;
cheese = pepperoni = false;
}
int Pizza::getType()
{
return type;
}
int Pizza::getSize()
{
return size;
}
bool Pizza::getCheese()
{
return cheese;
}
bool Pizza::getPepperoni()
{
return pepperoni;
}
void Pizza::setType(int t)
{
type = t;
}
void Pizza::setSize(int s)
{
size = s;
}
void Pizza::setCheese(bool choice)
{
cheese = choice;
}
void Pizza::setPepperoni(bool choice)
{
pepperoni= choice;
}
void Pizza::outputDescription()
{
switch (size)
{
case SMALL:
cout << "Small "; break;
case MEDIUM:
cout << "Medium "; break;
case LARGE:
cout << "Large "; break;
default:
cout << "Unknown" ;
}
switch (type)
{
case DEEPDISH:
cout << "deepdish "; break;
case HANDTOSSED:
cout << "hand tossed "; break;
case PAN:
cout << "pan "; break;
default:
cout << "Unknown";
}
cout << "pizza";
}
double Pizza::computePrice()
{
double cost = 0.0;
switch (size)
{
case SMALL:
cost += 10; break;
case MEDIUM:
cost += 14; break;
case LARGE:
cost += 17; break;
}
if (cheese)
cost += 2.0;
if (pepperoni)
cost += 2.0;
return cost;
}
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;
}
}
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();
//Order.customerinput();
if(again != "y"){
custmizedTotal.customerTotal();
}
return 0;
}
答案 0 :(得分:0)
替换
int main(){
Order.customerinput(); //error is here
if(again != "y"){
custmizedTotal.customerTotal();
}
return 0;
}
人:
int main(){
custmizedTotal.customerinput(); // Change this line
if(again != "y"){
custmizedTotal.customerTotal();
}
return 0;
}
由于您忘记定义Order构造函数而导致的第二个错误。
将其添加到您的代码中(在main()方法上方):
Order::Order(){
// Set the initial values for order
}
您也忘记添加customerOrder方法(但这不会导致错误,因为您没有使用此方法):
void Order::customerOrder() {
}