关于C++的bool问题~!

wudialan 2007-04-13 01:11:30
bool DeliveryRequired()
{
char DelRequired;
bool boolean;
cout << "Do you require delivery? (Y/N) ";
cin >> DelRequired;
while(toupper(DelRequired)!='Y' && toupper(DelRequired)!='N')
{
cout << "Do you require delivery? (Y/N) ";
cin >>DelRequired;
}
if(DelRequired==toupper('Y')) boolean=true;
else boolean=false;
return boolean;
}
void main()
{
bool DeliveryRequired()
if(boolean=true)
...................
...................
}

以上的程序是判断用户输入是否错误而且还判断输入的Y和N.如果输入Y或y就返回true,n或N就返回false;但返回方面好象出现了问题..怎么样都不能返回true,请问下应该怎么改?
...全文
335 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangzhangyong411 2007-04-13
  • 打赏
  • 举报
回复
我不知道你的程序干妈的 我只是改得可以编译通过

其他逻辑上的不管

凌殿Y_ST 2007-04-13
  • 打赏
  • 举报
回复
虽说boolean 在 C/C++中并不是关键字,不过的确不大好, 若是bool型的,一般可以直接用
if( boolean ) 而相对的则用 if( !boolean )

wangzhangyong411 2007-04-13
  • 打赏
  • 举报
回复
#include <stdlib.h>
#include <iomanip>
先包含上面这俩头文件

再写成cout << ios::fixed << ios::showpoint << setprecision(2);

把那个清屏的函数用system("cls");来代替

你原来用的那个只能在TC下用的,VC不行

int GetUnitPrice( )
{
return 0;
}

这个函数记得返回个值


就这样了阿 这样编译就对了


SF

wudialan 2007-04-13
  • 打赏
  • 举报
回复
bool DeliveryRequired()
{
char DelRequired;
bool boolean;
cout << "Do you require delivery? (Y/N) ";
cin >> DelRequired;
while(toupper(DelRequired)!='Y' && toupper(DelRequired)!='N')
{
cout << "Do you require delivery? (Y/N) ";
cin >>DelRequired;
}
if(DelRequired==toupper('Y')) boolean=true;
else boolean=false;
return boolean;
}
void main()
{
bool boolean;
DeliveryRequired();
if(boolean==true)
...................
...................
}

我改成这样了..但还是有问题啊..
Elodie_gh 2007-04-13
  • 打赏
  • 举报
回复
if(true == boolean)那位大哥说的不错,得有个好习惯!命名上是不是也该注意点呢?boolean??
minioreo 2007-04-13
  • 打赏
  • 举报
回复
常量写左边?哈哈 好方法。
wudialan 2007-04-13
  • 打赏
  • 举报
回复
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <fstream.h>

const int ZONE1=5, ZONE2=12, ZONE3=20; // Delivery zones representing miles from shop
const float ZONE1_CHARGE=0.1, ZONE2_CHARGE=0.2, ZONE3_CHARGE=0.3,ZONE4_CHARGE=0.4;// Corresponding delivery zone charges
const int QUALITY_1=11,QUALITY_2=13,QUALITY_3=14,QUALITY_4=17,QUALITY_5=20; // Price per sq m of carpet (based on quality)
const int GUARANTEE_Q1=1,GUARANTEE_Q2=2,GUARANTEE_Q3=3,GUARANTEE_Q4=4,GUARANTEE_Q5=5; // Guarantee periods for corresponding quality prices
const char POUND_SIGN = 156;
const int INITIAL_QUOTE_REF_NUM=1000;
const int NAME_LEN=20;
const int QUOTE_COL_WIDTH = 7, NAME_COL_WIDTH = NAME_LEN, COST_COL_WIDTH = 11, DELIVERY_COL_WIDTH = 12, TOTAL_COL_WIDTH = 9, GUARANTEE_COL_WIDTH = 2;
const int TABLE_WIDTH = 79; // Num of characters in output table row


void DisplayHeadings()
{

cout << endl << endl << endl
<< setfill('_') << setw(TABLE_WIDTH) << "" << "\n\n"
<< setfill(' ')
<< setw(7) << " Ref | " << setw(NAME_COL_WIDTH) << " Name | " << setw(COST_COL_WIDTH) << "Carpet | " << setw(DELIVERY_COL_WIDTH)
<< "Delivery | " << setw(TOTAL_COL_WIDTH) << " Total | " << setw(GUARANTEE_COL_WIDTH+7) << "Guarantee |" << endl
<< setfill('_') << setw(TABLE_WIDTH) << "" << "\n\n" << setfill(' ');

cout << fixed << showpoint << setprecision(2);


}


void DisplayRecord(int QuoteNum,char Name[],float Cost,float DeliveryCharge,int GuaranteePeriod)

{

cout << setfill(' ')
<< setw(7) << QuoteNum << " | "
<< setw(NAME_COL_WIDTH) << Name << " | "
<< setw(1) << POUND_SIGN << setw(COST_COL_WIDTH-4) << Cost << " | "
<< setw(1) << POUND_SIGN << setw(DELIVERY_COL_WIDTH-4) << DeliveryCharge << " | "
<< setw(1) << POUND_SIGN << setw(TOTAL_COL_WIDTH-1) << Cost + DeliveryCharge << " | "
<< setw(7) << "Years: " << setw(GUARANTEE_COL_WIDTH) << GuaranteePeriod << " |"
<< endl;

}


bool DeliveryRequired()
{
char DelRequired;
bool boolean;
cout << "Do you require delivery? (Y/N) ";
cin >> DelRequired;
while(toupper(DelRequired)!='Y' && toupper(DelRequired)!='N')
{
cout << "Do you require delivery? (Y/N) ";
cin >>DelRequired;
}
if(DelRequired==toupper('Y'))
{
boolean=true;
}
else boolean=false;
return boolean;
}

int GetUnitPrice( )
{

}



void main()
{
float Length=0, Width=0, Area=0, Cost=0, DeliveryCharge=0;
int UnitPrice=0, GuaranteePeriod=0, DelDistance=0, QuoteNum=INITIAL_QUOTE_REF_NUM;
char AnotherQuote, DelRequired;
char Name[NAME_LEN];
bool ValidPrice;

do
{
clrscr();

cout << "\nPlease enter surname and initials: ";
cin >> ws;
cin.getline(Name,NAME_LEN);

cout << "Please enter length of room: ";
cin >> Length;

cout << "Please enter width of room: ";
cin >> Width;

do
{
DeliveryCharge=0;
ValidPrice = true;

cout << "Please enter the price per square metre from the following price range:\n\n";
cout << POUND_SIGN << QUALITY_1 << ", " << POUND_SIGN << QUALITY_2 << ", " << POUND_SIGN
<< QUALITY_3 << ", " << POUND_SIGN << QUALITY_4 << ", " << POUND_SIGN << QUALITY_5 << "\n\n" << POUND_SIGN;
cin >> UnitPrice;

cout << "\n\n";

//Assign correct guarantee period for unit price entered.
switch(UnitPrice)
{
case QUALITY_1 : GuaranteePeriod=GUARANTEE_Q1; break;
case QUALITY_2 : GuaranteePeriod=GUARANTEE_Q2; break;
case QUALITY_3 : GuaranteePeriod=GUARANTEE_Q3; break;
case QUALITY_4 : GuaranteePeriod=GUARANTEE_Q4; break;
case QUALITY_5 : GuaranteePeriod=GUARANTEE_Q5; break;
default : cout << "\n\nInvalid price.\n\n";
ValidPrice = false;
}

}while (!ValidPrice);


Area = Length * Width;
Cost = Area * UnitPrice;

bool boolean;
DeliveryRequired();
//Force correct input to delivery question.

if(boolean==true)
{
cout << "\n\nPlease enter delivery distance in miles: ";
cin >> DelDistance;
if(DelDistance >=0 && DelDistance <=ZONE1)
{
DeliveryCharge = Cost * ZONE1_CHARGE;
}
else
{
if(DelDistance <=ZONE2)
{
DeliveryCharge = Cost * ZONE2_CHARGE;
}
else
{
if(DelDistance <=ZONE3)
{
DeliveryCharge = Cost * ZONE3_CHARGE;
}
else
{
DeliveryCharge = Cost * ZONE4_CHARGE;
}
} //end else > ZONE2
} //end else > ZONE1
} //end delivery required

DisplayHeadings();
DisplayRecord(QuoteNum,Name,Cost,DeliveryCharge,GuaranteePeriod);
cout << "\n\nEnter another quote?";
cin >> AnotherQuote;

} while(AnotherQuote=='Y'||AnotherQuote=='y');

cout << "\n" << endl;
system("pause");
}

不行啊..这是我的整个程序``你编译下看看```不行哦...
晨星 2007-04-13
  • 打赏
  • 举报
回复
最好养成把常量写前边的习惯:
if(true == boolean)
这样,万一写错了,编译器还能帮忙查出来。
晨星 2007-04-13
  • 打赏
  • 举报
回复
if(boolean=true)
改为
if(boolean==true)

64,687

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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