Variable length

This is a discussion on Variable length within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I am creating a console program that analyses txt files and one txt file contains some c++ source ...

  1. #1
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    Variable length

    Hey guys, I am creating a console program that analyses txt files and one txt file contains some c++ source code and I was wondering if there is a variable called int test is there any way i can count the length of the variable name in this case 4 as test is 4 characters?

    I was also wondering is there an easy way for the program to notice the different between an int variable and an int function in this case?

    Thanks for any advice.
    Last edited by 182; 02-14-2006 at 06:24 AM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    1:

    Entirely depends on how you do it, but if you have the variable name in an std::string use:
    Code:
    std::string VariableName;
    
    File >> VariableName; //Read the variable name somehow
    int Length = VariableName.size(); //Get the size of the variable name
    2:

    functions have a parameter list (brackets: ( ) ). Of course you'll need some kind of look-ahead to find these. One simple (and perhaps not too safe) way of doing this is to read an extra character, if it's a ( then it's a function, if it's a ; or = it's a variable.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Hi thanks for your reply, to read the variable name in a char array would it still be possible to count the length?

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    std::strlen(), assuming it's NULL terminated.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    What are you trying to do? I get the feeling you're attempting to write a whole C-parser which is a very VERY huge undertaking.

    EDIT: Hey, where did his reply go? :/
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Hey thanks again for your reply (don't know where the other guys went) im not trying to write a whole C parser I would just like a method of finding the length of a variable name using an array and being able to tell the difference between an int variable and an int function.

    But i've only been doing programming for about a year in my spare time so im not very good at it, so im not sure where to begin with this aspect of the program.

    I will have a go playing around with the arrays and post up what I get.

  7. #7
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Function identifiers are followed by parentheses at declaration.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    315
    use a char array[] , then you can access each char

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I have a feeling several posts got deleted.

    Function identifiers are followed by parentheses at declaration.
    If you're not careful you might get tripped up by things like this:
    Code:
    int x(3);
    Or this:
    Code:
    int function
        (void);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    I don't think anyone has addressed your question yet:
    is there any way i can count the length of the variable name
    The previous posts spelled out ways to count the length of what's stored in the variable--not a way to count the length of the variable name itself.

    As far as I know, there is no way to determine the length of a variable name in C++.

    I was also wondering is there an easy way for the program to notice the different between an int variable and an int function in this case?
    It sounds like you want to write a program that examines it's own C++ code. I've never heard of that before.

    You can write a program that examines some text in a text file. Then you can count the length of words in that file, or make up some rules as to what series of characters constitutes a function v. a variable name.
    Last edited by 7stud; 02-14-2006 at 08:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. XOR encrypt with variable key length
    By Bleech in forum C Programming
    Replies: 9
    Last Post: 09-09-2006, 02:20 AM
  2. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 12:57 PM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  4. Weird modification to string length
    By ChwanRen in forum C Programming
    Replies: 0
    Last Post: 08-17-2003, 10:45 AM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM

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