[英]C++ code, bools, and loops
我是C ++編程的新手,一個星期沒做任何事情,所以我在弄亂我到目前為止所知道的事情,以查看是否需要再次進行研究。
但是,我遇到了一個關於布爾的問題(以前從未真正使用過它們)。
資源:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
signed long int x;
unsigned short int y = 12345;
bool POSX;
bool yes;
cin >> x;
if (x >= 0)
{
POSX = true;
}//end of if for bool
else
{
POSX = false;
}
cout << "This is the current value of X: " << x << endl;
if(x < y)
{
cout << "x is less than the integer y \n \n";
}//end of if
else
{
cout << "y is greater than the integer x \n \n";
}//end of else
cout << "The current value of Y is: " << y << endl << endl << endl;
cout << "Is X positive?: " << POSX << endl << endl;
cout << "How much more would X need to be to surpass y? The answer is: " << y - x << endl;
if(x > y)
{
cout << "well, actually, x is greater than y by: " << y - x << " so you would need to add that to get to the value of x" <<endl <<endl;
}//end of if
cout << "Do you like cookies? Enter below. . ." <<endl;
cin >> yes;
if(yes = "yes") // should this be if(yes = 1)?
{
cout << "I do too! But only when they are soft and gooey!";
} //end of if for bool yes
else
{
cout << "Well, join the dark side, and you may be persuaded by the power of cookies and the power of the dark forces!";
}//end of else for bool yes
char f;
cin >> f;
return 0;
} //end of main
我遇到的問題是,當我嘗試編譯時,一方面,程序退出,然后才能看到Cookie問題的結果[因此,我必須在編譯器中放置一個斷點];其次,當我看到答案時,它總是會給出“是”響應,而不是其他任何響應。 因此,如果我輸入no作為輸入,它仍然會輸出if for布爾值yes。 我不確定在最后一條語句中是否正確定義了if子句。 有人可以幫忙嗎?
好,兩件事。 您的主要問題是:
if(yes = "yes")
“ yes”被定義為布爾類型,即它可以包含值“ true”或“ false”。 您試圖進行比較(實際上是由於僅使用一個=而不是==進行賦值,這是您檢查相等性的方法),一個布爾值與字符串“ yes”的比較。 好吧,那沒有道理。 它應該是:
if( yes )
而已。 'yes'已經是一個布爾值,並且if語句中的表達式不再需要。
其次,像這樣的構造是多余且不必要的:
if (x >= 0)
{
POSX = true;
}//end of if for bool
else
{
POSX = false;
}
您正在檢查一個布爾值,然后分配一個。 只需像這樣一行即可:
POSX = (x >=0 );
另外,通常不會對局部變量使用所有上限。
還有一件事; 您輸入的是字符串數據(“否”或“是”),而cin期望為整數。 我建議您花一些時間來了解什么是數據類型。
我遇到的問題是,當我嘗試編譯時,其中一個程序在我看到Cookie問題的結果之前已經退出
這是因為當您將“ no”作為值時,使cin
無效。 operator>>(std::istream&, bool&)
假定為數字輸入。 不等於零的值將被解釋為true
,而等於零的值將被解釋為false
。
如果提供的輸入無法解析為數字badbit
則將在流上設置輸入。 嘗試在此失敗狀態下使用流時,將導致讀取垃圾(或更確切地說,是未定義的行為),並且流中的get指針不會前進。
將yes更改為字符串( #include <string>
),然后將其像這樣進行比較:
if (yes == "yes")
這里有幾處出問題的地方,但是我認為絆倒您的是cin >> yes;
在C ++中, false
為0
。 因此,對於任何非零輸入值,這很可能返回true
。 一種更可靠的方法是要求並評估其他內容,例如字符輸入:
cout << "Do you like cheese? (y/n) ";
char c;
cin >> c;
if ( c == 'y' || c == 'Y' )
do whatever;
另外,在測試條件語句時,請確保使用“ double-equals”, condition == true
。 更好的是,在以下地方采用簡寫形式:
(condition)
表示(condition == true)
(! condition)
表示(condition == false)
希望能為您提供正確方向的開始。
這是經過編輯的源代碼,我進行了更改,因此請對其進行研究並與原始代碼進行比較。 * e *
#include <iostream>
#include <string>
#include <iomanip> //headerfile for boolalpha
using namespace std;
int main()
{
string answer;
signed long int x;
unsigned short int y = 12345;
bool POSX;
bool yes;
cout<<"Enter any value: ";
cin >> x;
if (x >= 0)
{
cout << boolalpha; // print bools as true or false
POSX = true;
}//end of if for bool
else
{
cout << boolalpha; // print bools as true or false
POSX = false;
}
cout << "This is the current value of X: " << x << endl;
if(x < y)
{
cout << "x is less than the integer y \n \n";
}//end of if
else
{
cout << "y is greater than the integer x \n \n";
}//end of else
cout << "The current value of Y is: " << y << endl << endl << endl;
cout << "Is X positive?: " << POSX << endl << endl;
cout << "How much more would X need to be to surpass y? The answer is: " << y - x << endl;
if(x > y)
{
cout << "well, actually, x is greater than y by: " << y - x << " so you would need to add that to get to the value of x" <<endl <<endl;
}//end of if
cout << "Do you like cookies? Yes/No. . ." <<endl;
cin >> answer; //this is a string
if(answer =="yes") // it can be a simple if statement
{
cout << "I do too! But only when they are soft and gooey!";
} //end of simple if
else
{
cout << "Well, join the dark side, and you may be persuaded by the power of cookies and the power of the dark forces!";
}//end of else for simple if
return 0;
} //end of main`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.