Thread: Defining Variables

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    15

    Defining Variables

    I am very new to C++ so this may be an easy question.

    I have a folder and i want to filter out everything with and extension of *.dat OR *.pdf so I want to define a variable that will only accept them extensions. In the past the program used *.* so it grabbed everything in this directory. Can I define a variable to only accept *.dat files and *.pdf files? Or do i have to make separate variables?

    *********

    My code used to look like this:

    #define FILTER "*.*"


    Do i have to make two variables like the following or is there a way to make my FILTER variable accept both *.pdf and *.dat files:

    #define FILTER1 "*.pdf"
    #define FILTER2 "*.dat"


    Thanks for any help and being patient with a rookie!

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    You're on the right track with two different constants. Of course with two constants, you'll have to use some logic... AND (&&), OR (||), if...

    BTW - It is considered better practice to use const, rather than #define. This is mainly because #define is global, so it can cause hard to find bugs in bigger programs. (i.e. Something can get redefined without you knowing it.)

    The other possibility is to use an array: FILTER[0], FILTER[1], FILTER[2], etc. That way, you could add file extensions later without changing the whole program. (You might need another variable to keep track of the number of valid elemements in your array.)

  3. #3
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    It's possible to use one #define / const variable if you feel like parsing it and finding a delimiter (pipe, whitespace, etc) to split it up with.

    I'd rather just use a vector of strings, myself.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    In the past the program used *.* so it grabbed everything in this directory. Can I define a variable to only accept *.dat files and *.pdf files? Or do i have to make separate variables?
    I doubt there is any 'filter' that will allow you to do that. And I really have no idea what you're talking about - what variables you've defined has nothing to do with what a function does. There may be a workaround that will accomplish what you're trying to achieve, but without knowing more about this 'function' you're using, there's no way to tell.
    Just Google It. √

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. defining macros or static variables
    By l2u in forum C++ Programming
    Replies: 15
    Last Post: 08-05-2008, 06:28 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Globaly defining variables
    By PaloDeQueso in forum C Programming
    Replies: 5
    Last Post: 03-20-2002, 09:38 PM