A Problem understanding operators

This is a discussion on A Problem understanding operators within the C++ Programming forums, part of the General Programming Boards category; Hi there everyone. This is my first post so I hope I get all the ettiquette right. I am just ...

  1. #1
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24

    A Problem understanding operators

    Hi there everyone. This is my first post so I hope I get all the ettiquette right.

    I am just getting started learning C++ from books and am really enjoying it but running into some of the problems that I'm sure many people have. Sadly this one seems to elude me and I have looked as carefully as I can in my books and on websites. So my apologies if this is so obvious.

    All I am trying to do is test the condition of a variable and the result of that test is used to determine which function to call. All I keep getting is a complie time error: expected primary-expression before '<' token.

    This is just a little program to test out some of the things I've learnt so far

    Code:
    #include <iostream>using namespace std;
    
    
    int addition (int a, int b)
    {
        return a + b;
    }
    
    
    int mult (int a, int b)
    {
        return a * b;
    }
    
    
    int main ()
    {
    
    
        int a, b, path;
        cout << "Enter a pathway interger (1-10)\n";
        cin >> path;
        cout << "Enter the two variables needed for the program: ";
        cin >> a;
        cout << "\t";
        cin >> b;
        
        if (path != 0 && < 5)
        {
                 cout << addition(a,b);
        }
        
        if (path > 5 && < 11)
        {
                 cout << mult(a,b);
        }
        
        if (path == 0 || >= 11)
        {
        cout << "you are a dumbass that can't follow instructions!";
        }
        
        system("PAUSE");
        return 0;
    }
    I'm not sure what I have done wrong as I am trying to follow the syntax in my reference books and tutorials so any help as to my errors would be greatly appreciated. Oh btw there is an error for every line where a conditional operator is used.

    Many thanks

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Instead of
    Code:
    if (path != 0 && < 5)
    you need:
    Code:
    if (path != 0 && path < 5)
    In other words, the && or || separates two individual conditional statements, not two conditions on one variable.

  3. #3
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24
    of course! of course!. Thanks so much.
    I feel a little daft now but I think lesson learned. cheers!

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    What if I type -6 for the path? Your logic will have me do the addition when you should be calling me a dumbass.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Be careful what you wish for!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Understanding this
    By RA-3000 in forum C Programming
    Replies: 9
    Last Post: 06-13-2010, 04:07 AM
  2. Replies: 7
    Last Post: 05-22-2010, 05:42 PM
  3. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  4. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 06:42 PM
  5. Replies: 11
    Last Post: 08-30-2004, 03:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21