明明调用了iostream函数库,可是怎么还是提示cout,cin 有问题

huyunkun 2003-09-07 09:24:32
这是一个纯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
...全文
735 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
robertnet 2003-09-08
  • 打赏
  • 举报
回复
把include <iostream>
包含在你自定义的candyMachine.h头文件下面试试。
using namespace std;放在你的main()所在文件之上面试试。
jiacool 2003-09-07
  • 打赏
  • 举报
回复
1.在"candyMachine.h"文件中没有include<iostream.h>
2.还有一个错误 下面的

#include <iostream>

里面 的应该是iostream.h 少了h
peon 2003-09-07
  • 打赏
  • 举报
回复
最前面要加#include <iostream>
using namespace std;
jp311 2003-09-07
  • 打赏
  • 举报
回复

using namespace std;
加到candyMachine.h中试试看
huyunkun 2003-09-07
  • 打赏
  • 举报
回复
错误信息:
--------------------Configuration: cpp5 - Win32 Debug--------------------
Compiling...
cpp5.cpp
f:\vc\cpp5.cpp(29) : error C2065: 'cout' : undeclared identifier
f:\vc\cpp5.cpp(29) : error C2297: '<<' : illegal, right operand has type 'char [38]'
f:\vc\cpp5.cpp(29) : error C2065: 'endl' : undeclared identifier
f:\vc\cpp5.cpp(30) : error C2297: '<<' : illegal, right operand has type 'char [25]'
f:\vc\cpp5.cpp(31) : error C2297: '<<' : illegal, right operand has type 'char [12]'
f:\vc\cpp5.cpp(32) : error C2297: '<<' : illegal, right operand has type 'char [12]'
f:\vc\cpp5.cpp(33) : error C2297: '<<' : illegal, right operand has type 'char [10]'
f:\vc\cpp5.cpp(34) : error C2297: '<<' : illegal, right operand has type 'char [14]'
f:\vc\cpp5.cpp(38) : fatal error C1083: Cannot open include file: 'candyMachine.h': No such file or directory
Error executing cl.exe.

cpp5.obj - 9 error(s), 0 warning(s)
Benny1 2003-09-07
  • 打赏
  • 举报
回复
不对我 说错了,是在candyMachine.cpp文件中没include
就在void showSelection()这个函数前面
Benny1 2003-09-07
  • 打赏
  • 举报
回复
你在"candyMachine.h"文件中没有include<iostream>
Benny1 2003-09-07
  • 打赏
  • 举报
回复
你在"candyMachine.h"文件中没有include<iostream>

70,028

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧