if Statement, easier way?

This is a discussion on if Statement, easier way? within the C++ Programming forums, part of the General Programming Boards category; I have an array of structs holding amongst other things a list of ip Addresses, these ip Addresses are the ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    64

    if Statement, easier way?

    I have an array of structs holding amongst other things a list of ip Addresses, these ip Addresses are the users on my system.

    I want to convert these ip addreses to the names of my users. Since there are 50+ users on my system i would like to know if there is a better and hopefully shorter way than an if statement to change them. i Have

    Code:
       if (db[i].ipAddress == "10.0.0.101")
    	db[i].ipAddress = "A User" ;
       else if (db[i].ipAddress == "10.0.0.102")
    	db[i].ipAddress = "A User" ;
    ......etc, etc
    p.s A switch wouldn't work, and thats not much shorter anyway

    Thanks a lot

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    If you are storing the IP address in a string as you appear to be, you can use strcmp() to compare your variable with the literals...

    Code:
    if (!strcmp(db[i].ipAddress, "10.0.0.101"))
    	db[i].ipAddress = "A User" ;
    else if (!strcmp(db[i].ipAddress, "10.0.0.102"))
    	db[i].ipAddress = "B User" ;
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if else statement ????
    By pczafer in forum C++ Programming
    Replies: 11
    Last Post: 04-29-2009, 09:45 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 03:14 PM
  3. Using while loop for if statement.
    By Galens in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2008, 04:14 PM
  4. having trouble understanding this statement
    By kes103 in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2003, 10:00 AM
  5. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 10:56 PM

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