Thread: appending a int to a variable in a loop?

  1. #1
    wario
    Guest

    appending a int to a variable in a loop?

    say i run my app.

    prog.exe test1 test2 test3

    to save using..

    string arg1=argv[1] etc etc

    how can i use this loop to append "i" to the end of "arg"..

    for(int i=1;i<argc;i++)
    {
    string arg? = argv[i];
    }

    ? should be the number of "i".
    how can i add this to the end of "arg" ?
    thanks

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    You can use an array of strings. That's all you can do.
    Away.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    You can use the resize() function of the string class. i.e.
    Code:
    #include <iostream>
    #include <string>
    
    int main() {
      string arg;
      int i;
      int argv[50];
    
      for(i = 0; i < 50; i++) {
        arg.resize(arg.length() + 1);
        arg[arg.length() - 1] = argv[i];
      }
    
      return 0;
    }
    I haven't tested it, but I think it should work. I hope this was what you were looking for.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Just looking at that (I'm too lazy to test for sure at the moment), I don't think that'll do what you want.

    The operator[] for std::string returns a reference to a character. Assigning that character to an int will cause the int to be cast to a char (simply truncated if necessary), and then you'll end up appending the character corresponding to that number in the ASCII table.

    There is an IntToString function in the FAQ (here), and then you could simply use the operator+= for string.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Hmm... Looking back at the question (and blackrat's reply), I think something more like this is what your looking for:

    Code:
    std::vector<std::string> arg(argc);
    
    for(int i = 0; i < argc; ++i)
    {
       arg[i] = argv[i];
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    wow, I completely forgot about that. Geeze, not a good day for me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM