Reputation: 11
I am new to C++ programming and haven't done anything in a week, so I was messing around with things I know thus far to see if I have to go over things again.
However, I ran into an issue with bools (haven't really used them before).
Source:
#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
The issue I have is when I try to compile, for one, the program exits before I can see the result of the cookie question [so I have to place a break point in the compiler], and second, when I can see the answer, it always comes up with the yes response, and not anything else. So, if I put no as the input, it still outputs the if for the bool yes being true. I am not sure if I am defining the if clause correctly in the last statement. Could anyone help?
Upvotes: 1
Views: 5061
Reputation: 1
Here is the edited sources code, I made changes so study it and compare with the original code. *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`
Upvotes: 0
Reputation: 9555
There are a couple of things going wrong here, but I think the one tripping you up is cin >> yes;
In C++, false
is 0
. Therefore, this will likely return true
for any non-zero input values. A more reliable approach would be to ask for and evaluate on something else, for instance, character input:
cout << "Do you like cheese? (y/n) ";
char c;
cin >> c;
if ( c == 'y' || c == 'Y' )
do whatever;
Additionally, when testing conditionals, be sure to use "double-equals", condition == true
. Better yet, adopt the shorthand where:
(condition)
means (condition == true)
(! condition)
means (condition == false)
Hope that gives you a start in the right direction.
Upvotes: 1
Reputation: 124632
Ok, two things. Your major problem is this:
if(yes = "yes")
'yes' is definend as a bool type, i.e., it can hold the values "true" or "false". You attempting to compare comparing (actually attempting to assign due to using only one = instead of ==, which is how you check for equality) a boolean to a string "yes". Well, that makes no sense. It should be:
if( yes )
That's it. 'yes' is already a boolean, and the expression in an if statement requires no more.
Secondly, constructs like this are redundant and unnecessary:
if (x >= 0)
{
POSX = true;
}//end of if for bool
else
{
POSX = false;
}
You are checking for a boolean value and then assigning one. Just do it in one line like this:
POSX = (x >=0 );
Also, you typically don't use all caps for local variables.
One more thing; you are entering string data ("no" or "yes") and cin is expecting an int. I suggest that you spend some time learning about what data types are.
Upvotes: 4
Reputation: 106530
The issue I have is when I try to compile, for one, the program exits before I can see the result of the cookie question
That's because you invalidated cin
when you put "no" as the value. operator>>(std::istream&, bool&)
assumes numeric input. Values not equal to zero will be interpreted as true
, and values equal to zero will be interpreted as false
.
If you supply input that cannot be parsed as numeric badbit
will be set on the stream. Attempting to use the stream while it is in this failed condition will result in garbage being read (or rather, undefined behavior), and no advancement of the get pointer in the stream.
Upvotes: 2
Reputation: 103693
Change yes to a string(#include <string>
), and then compare it like this:
if (yes == "yes")
Upvotes: 1