明明调用了iostream函数库,可是怎么还是提示cout,cin 有问题
这是一个纯C++程序,来源于《c++编程》。已经定义了一个自己的函数库candyMachine.h,提示不存在candyMachine.h。于是我删除了这个函数库。可是我发现这个时候,竟然出现了cout,cin的不能识别错误,可我在这之前调用了iostream函数库,不知道怎么回事。帮忙解决一下!~
附源代码://Candy Machine Header File
class cashRegister
{
public:
int currentBalance();
void acceptAmount(int amountIn);
cashRegister(int cashIn=500);
private:
int cashOnHand;
};
class dispenserType
{
public:
int count();
int productCost();
void makeSale();
dispenserType(int setNoOfProducts=50,int setCost = 50);
private:
int numberOfProducts;
int cost;
};
void showSelection()
{
cout<<"*** Welcome to Shelly's candy shop***"<<endl;
cout<<"To select an item, enter"<<endl;
cout<<"1 for Candy"<<endl;
cout<<"2 for Chips"<<endl;
cout<<"3 for Gum"<<endl;
cout<<"4 for Cookies"<<endl;
}
#include <iostream>
#include "candyMachine.h"//在没有将#include "candyMachine.h"置为注释的时候只有一个错误
using namespace std;
int cashRegister::currentBalance()
{
return cashOnHand;
};
void cashRegister::acceptAmount(int amountIn)
{
cashOnHand +=amountIn;
}
cashRegister::cashRegister(int cashIn)
{
if(cashIn>=0)
cashOnHand=cashIn;
else
cashOnHand=500;
}
int dispenserType::count()
{
return numberOfProducts;
}
int dispenserType::productCost()
{
return cost;
}
void dispenserType::makeSale ()
{
numberOfProducts--;
}
dispenserType::dispenserType(int setNoOfProducts,int setCost)
{
if(setNoOfProducts>=0)
numberOfProducts = setNoOfProducts;
else
numberOfProducts =50;
if(setCost>=0)
cost=setCost;
else
cost=50;
}
//main program
#include <iostream>
#include "candyMachine.h"
using namespace std;
void showSelection()
{
cout<<"*** Welcome to Shelly's candy shop***"<<endl;
cout<<"To select an item, enter"<<endl;
cout<<"1 for Candy"<<endl;
cout<<"2 for Chips"<<endl;
cout<<"3 for Gum"<<endl;
cout<<"4 for Cookies"<<endl;
cout<<"9 for exit"<<endl;
}
void sellProduct(dispenserType& product,cashRegister& pCounter);
int main()
{
cashRegister counter;
dispenserType candy(100,50);
dispenserType chips(100,65);
dispenserType gum(75,45);
dispenserType cookies(100,85);
int choice;
showSelection();
cin>>choice;
while(choice!=9)
{
switch(choice)
{
case 1: sellProduct(candy,counter);
case 2: sellProduct(chips,counter);
case 3: sellProduct(gum,counter);
case 4: sellProduct(cookies,counter);
default: cout<<"Bad Selection"<<endl;
}
showSelection();
cin>>choice;
}
return 0;
}//end main
void sellProduct(dispenserType& product,cashRegister pCounter)
{
int amount;
int amount2;
if(product.count ()>0)
{
cout<<"Please deposit"<<product.productCost ()<<"cents"<<endl;
cin>>amount;
if(amount<product.productCost())
{
cout<<"please deposit another"<<product.productCost()-amount<<" cents"<<endl;
cin>>amount2;
amount=amount+amount2;
}
if(amount>=product.productCost())
{
pCounter.acceptAmount(amount);
product.makeSale();
cout<<"Collect what you deposited."<<endl;
cout<<"*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_"
<<endl<<endl;
}
else cout<<"Sorry this item is sold out."<<endl;
}
}//end sellProduct