-
The "||" function
I am having a problem with understanding the "||" function. ( I know I probably sound stupid, I just started learning C++ tonight...) I was just looking at a little program, and when I use integer = 59602 everything works fine, when I use integer = 59602 || 59601 I start to have problems. Please help. (Note, its really late and I'm really sleepy so please be kind at my ignorance and/or stupidity and lack of proper explanation.)
-
typicaly || is used in boolean expressions.
if (a or b) is
if (a || b)
As a bitwise operator it logicaly Ors the two values
1 || 1 = 1
1 || 0 = 1
0 || 1 = 1
0 || 0 = 0
so it takes the binary equivalent of the two numbers and performs an || on each bit returning the result
-
Um curlious || is boolean or | is bitwise or. A little big of a different ;)
-
ok here is what I have, What is the problem with it please?
Code:
#include <iostream>
using namespace std;
int main()
{
int zipcode = 59602 ;
cout<<"What is your zip code?\n";
cin>> zipcode;
cin.ignore();
if ( zipcode == 59602 ) {
cout<<"Ok, good.\n";
}
else {
cout<<"Are you sure?\n";
}
cin.get();
}
-
well since there isn't a || anywhere in that code I assume you had this
Code:
if ( zipcode == 59602 || 59601 )
you need to do this
Code:
if ( zipcode == 59602 || zipcode == 59601 )
-
:D :D :D YES thats it! ty but I am confused about
Code:
int zipcode = 59602;
Does that have nothing to do with the outcome?
-
Not really. You initalize zipcode to 59602 but over write that information with your cin>>zipcode;
-
ok, now I am trying to get a letter in plae of a number, i have the "char" tag, but when i put a letter in, it says that value "a" has not been used before,
-
you're question is too vague - either rephrase it or show us some code
my best guess though, is you need this line where you have "int zipcode":
char a;
on a side note: || is called an operator, not a function
-
ok, yet another problem:
Code:
#include <iostream>
using namespace std;
int main()
{
int a;
int b = ( 5/9 * a - 32 ) ;
cout<<"Enter Farenheit temperature to convert to Celcius:\n";
cin>> a;
cin.ignore();
{
cout<<"Your answer is:"<< b <<"\n";
}
cin.get();
}
Can someone tell me why this doesnt work?
Thanks for being patient with me, Im just trying to understand all the functions 100%
-
Your problem here is that you tried to inilialize b using a as part of the expression. However a at that point does not yet have a value assigned to it. So the expression ( 5/9 * a - 32 ) will use whatever value happens to reside in the address location associated with a. Since a has not yet been assigned a value by the user, there's no telling what it's current value is.
You should assign b = ( 5/9 * a - 32 ) ; after the cin >> a;.
-
>>int b = ( 5/9 * a - 32 ) ;
What you're doing is assigning a value to b. What you're trying to do is create a function, but b is a variable, so it just holds a value - i.e. what happens to a has no effect whatsoever on b. What your code does is perform an operation on a and store the result in b, not "define what b is in terms of a". Since at that point there is no value in a, you'll end up with some garbage value in b that never changes.
Refer to Scribbler's post for the solution to your problem.
-
another thing that anyone has yet to point is that dividing 5 by 9 is likely to produce a floating point value. rather than "int b " you should use "float b", otherwise a number like 123.456 becomes simply 123 (it does not get rounded).
-
Ok, all the suggestions have not helped, this is what i have, and everytime i get -32 as the output
Code:
#include <iostream>
using namespace std;
int main()
{
float a;
cout<<"Enter Farenheit temperature to convert to Celcius:\n";
cin>> a;
float b = ( 5/9 * a - 32 ) ;
cin.ignore();
{
cout<<"Your answer is:"<< b <<"\n";
}
cin.get();
}
-
Since you are now using float, the expression 5 / 9 should be 5.0 / 9.0.
In the expression ( 5/9 * a - 32 ), your compiler identifies 5 and 9 as being integers, so performs integer math. Making 5/9 always result in 0.
Also understand the order of operator precedence. Hint, Division and Multiplication will be performed first from left to right which will give you an incorrect result. So you'll need to use some parenthesis to ensure the proper order of operator calculation. However now you find the opportunity to experience the joys of floating point math, and see how frustratingly inaccurate it can be. You can test the code by entering -40 as the user input. I believe -40 is the only temerature where Farenheit and Celsius are identical. If you get the formula straight in this example though, the margin of error should probably be acceptable.