Thread: The "||" function

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    16

    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.)

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    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

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Um curlious || is boolean or | is bitwise or. A little big of a different

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    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();
                                 }

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    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 )

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    YES thats it! ty but I am confused about
    Code:
     int zipcode = 59602;
    Does that have nothing to do with the outcome?

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Not really. You initalize zipcode to 59602 but over write that information with your cin>>zipcode;

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    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,

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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
    Last edited by misplaced; 01-09-2005 at 05:17 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    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%

  11. #11
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    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;.
    Last edited by Scribbler; 01-09-2005 at 02:08 PM.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>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.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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).
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    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();
    }

  15. #15
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    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.
    Last edited by Scribbler; 01-09-2005 at 03:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM