布尔值始终为 false
Boolean value always false
我正在创建一个图书存档程序,其中一个数据成员说这本书是否已读。但是我无法让程序将 "Yes" 写入屏幕,所以我猜它永远不会改变,即使当被问及我是否读过这本书时我说 '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;
}
haveRead == true
是比较,不是赋值。从两个子句中删除一个 =
if(toupper(ch) == 'Y')
haveRead = true;
else
haveRead = false;
运算符==
是比较运算符,用来比较事物。这里你只想赋值,所以你需要赋值运算符 =
.
我正在创建一个图书存档程序,其中一个数据成员说这本书是否已读。但是我无法让程序将 "Yes" 写入屏幕,所以我猜它永远不会改变,即使当被问及我是否读过这本书时我说 '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;
}
haveRead == true
是比较,不是赋值。从两个子句中删除一个 =
if(toupper(ch) == 'Y')
haveRead = true;
else
haveRead = false;
运算符==
是比较运算符,用来比较事物。这里你只想赋值,所以你需要赋值运算符 =
.