Like Tree1Likes
  • 1 Post By Elysia

Appending digits to variable

This is a discussion on Appending digits to variable within the C++ Programming forums, part of the General Programming Boards category; Let's say I have the objects file1, file2, file3, ... up to file9. I also have an array file_array[i] for ...

  1. #1
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Appending digits to variable

    Let's say I have the objects file1, file2, file3, ... up to file9.
    I also have an array file_array[i] for i = 1 to 9 with a value assigned to each element.
    How do I assign the array values to the corresponding objects with a for loop?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Code:
    string filename[6];
    sprintf(filename, "file%d", file_array[i]);
    Is that what you're asking?
    Remember that arrays start at index 0, not 1. Also, the array isn't necessary is you're trying to accomplish what I laid out. You could simply use the loop iterator:
    Code:
    for(i = 1;i <= 9;++i)
      sprintf(filename, "file%d", i);
    If you understand what you're doing, you're not learning anything.

  3. #3
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Sorry, but I don't think that's what I was referring to. The object names are file1, file2, etc. and I want to assign integer values to them, not to print the result.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    No, it was what you were referring to! Go read the man page for sprintf, as it doesn't print anything.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,423
    As far as I can tell, what 843 is after is something like this:
    Code:
    for (int i = 0; i < n; i++)
        object##file_array[i] = m;
    Where ##file_array[i] means appending the number in file_array[i] to the variable name and assigning a value to it.
    If this is the case, a map might be a solution for you.

    Also, I'd rather not use sprintf in C++. It's dangerous and unsafe.
    I'd much rather recommend using string streams or alternatively Boost.Format.
    rags_to_riches likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    If this is the case, a map might be a solution for you.
    Yes, but what's a 'map'?

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,423
    It's a data structure, std::map.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    I see, thanks!

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Actually, I totally missed this was C++ and you were using strings, which makes my solution a bad one. As Elysia said, go for a stringstream. See this post for an example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic c++ variable types, digits of precision etc
    By jackson6612 in forum C++ Programming
    Replies: 3
    Last Post: 04-17-2011, 12:50 AM
  2. Counting number of digits in a variable
    By Saeid87 in forum C Programming
    Replies: 6
    Last Post: 06-05-2009, 01:13 PM
  3. appending to an array
    By mr_nice! in forum C Programming
    Replies: 1
    Last Post: 06-28-2004, 09:36 AM
  4. appending a int to a variable in a loop?
    By wario in forum C++ Programming
    Replies: 5
    Last Post: 06-28-2003, 11:29 PM
  5. appending some letters to the end of a CString variable.
    By Eber Kain in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2001, 02:00 PM

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