布尔值始终为false

时间:2015-02-25 17:05:44

标签: c++ boolean

我正在创建一个图书档案计划,其中一位数据成员说是否读了这本书。但是我不能让程序在屏幕上写“是”,所以我猜它永远不会改变,即使我被问到是否已经阅读了这本书时我说“Y”。谁能看到我做错了什么?

cout << "\n\tEnter title of book: ";
cin >> title;
cout << "\tEnter author of book: ";
cin >> author;
cout << "\tEnter genre of book (1. Crime, 2. Sports, 3. Science, 4. Drama): ";
cin >> choice;
switch(choice)
{
    case 1: genre = crime;   break;
    case 2: genre = sports;  break;
    case 3: genre = science; break;
    case 4: genre = drama;   break;
}
cout << "\tEnter year of publication: ";
cin >> yearPublished;
cout << "\tHave you read the book? (Y/N): ";
cin >> ch;
if(toupper(ch) == 'Y')
{
    haveRead == true;
}
else
{
    haveRead == false;
}

2 个答案:

答案 0 :(得分:5)

haveRead == true

是比较,而不是作业。从两个子句中删除一个

if(toupper(ch) == 'Y')
    haveRead = true;
else
    haveRead = false;

答案 1 :(得分:1)

运算符==是一个比较运算符,用于比较事物。在这里您只想分配,因此您需要=的赋值运算符。