Last 2 lines

This is a discussion on Last 2 lines within the C++ Programming forums, part of the General Programming Boards category; Code: if (input == 'n' && NewUser.location->north) NewUser.location = NewUser.location->north; if (input == 's' && NewUser.location->south) NewUser.location = NewUser.location->south; if ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128

    Last 2 lines

    Code:
    			if (input == 'n' && NewUser.location->north)
    				NewUser.location = NewUser.location->north;
    			if (input == 's' && NewUser.location->south)
    				NewUser.location = NewUser.location->south;
    			if (input == 'e' && NewUser.location->east)
    				NewUser.location = NewUser.location->east;
    			if (input == 'w' && NewUser.location->west)
    				NewUser.location = NewUser.location->west;
                if (input != 'n') or (input != 's') or (input != 'e') or (input != 'w')
                    NewUser.location = NewUser.location;
    Im trying to get this to work. It works until I add the last 2 lines.

    these ones :

    Code:
                if (input != 'n') or (input != 's') or (input != 'e') or (input != 'w')
                    NewUser.location = NewUser.location;

    Then I get this :

    P:\KOAGAME\main.cpp||In function `int main(int, char**)':|
    P:\KOAGAME\main.cpp|598|error: expected primary-expression before '||' token|
    P:\KOAGAME\main.cpp|599|error: expected `;' before "NewUser"|
    ||=== Build finished: 2 errors, 0 warnings ===|

    It is in same format so Im not sure what im doing wrong, I tried changing everything around and using && instead of or's but I really need it run like this :

    If input does not equal either n s e w , then location stays same.

    Wondering if anyone could enlighten me

    thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You know by now that the condition of an if must be surrounded in parentheses.
    Code:
    if ((input != 'n') or (input != 's') or (input != 'e') or (input != 'w'))

  3. #3
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Don't use "or", use "||" instead. So it should look like:
    Code:
    if ( ( input != 'n' ) || ( input != 's' ) || ( input != 'e' ) || ( input != 'w' ) )

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by yaya View Post
    Don't use "or", use "||" instead. So it should look like:
    Code:
    if ( ( input != 'n' ) || ( input != 's' ) || ( input != 'e' ) || ( input != 'w' ) )
    Why not use or?

  5. #5
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    It might just be me, but "or" doesn't work for me, and I'm using VS2008. Though, now that you mention it, it doesn't say anything in the errors he's got listed there.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I had to #include <ciso646> to make it work in Visual Studio, although I'm not sure why.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 07:19 PM
  2. Print out first N lines
    By YoYayYo in forum C Programming
    Replies: 1
    Last Post: 02-21-2008, 12:58 AM
  3. Finding number of lines of code
    By arron in forum Linux Programming
    Replies: 8
    Last Post: 01-06-2006, 05:35 AM
  4. Reading lines from a file
    By blackswan in forum C Programming
    Replies: 9
    Last Post: 04-26-2005, 05:29 PM
  5. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 08:08 PM

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