Is there a way to get all the arguments and put them in one string? I'm very new to C++, so I need all the help I can get. Here's what I have here:

#include <iostream> 
using namespace std;

int main(int argc, char* argv[]) { 
   cout << "argc = " << argc << endl; 
   for(int i = 0; i < argc; i++) 
      cout << "argv[" << i << "] = " << argv[i] << endl; 

   return 0;
   
}

Could someone explain or add to my code how it would work, I'm guessing something along the lines of (I'm a python coder, so this may be wrong): mystring = ''; and after the for loop: mystring = mystring + argv

That's probably completely wrong... but whatever, lol.

Of course there is

int main(int argc, char* argv[])
{
    string args;
    for(int i = 1; i < argc; i++)
    {
        args += argv[i];
        args += ' '; // add space between args
   }
}

Well that's easy ;)

But does that put a space or would it do this:

myscript -v -s

and output

myscripy-v-s

See my previous post -- I added a line to put a space between the arguments.

So if I used this:

int main(int argc, char* argv[])
{
    string args;
    for(int i = 1; i < argc; i++)
    {
        args += argv[i];
        args += ' '; // add space between args
   }
   printf(args);
}

It spits out this error:

args.cpp: In function ‘int main(int, char**)’:
args.cpp:12: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int printf(const char*, ...)’

What's going on?

Well the error is on this line: printf(args); .
printf is a function inherited from C, and therefore can't handle C++ strings, I would suggest you to use cout instead: cout << args << endl; .

But you could of course also convert the C++ string to a constant C string, by using the c_str() method from the string class, in that case it would be possible to display the string using printf: printf("%s\n", args.c_str()); :)

Hmmm, that'd be because I've used C before, I thought they were very similar ;)

Hmmm, that'd be because I've used C before, I thought they were very similar ;)

Well, C != C++ remember that :)

THANKS!!!!

#include <iostream> 
#include <stdlib.h>
using namespace std;

int main(int argc, char* argv[])
{
    char cmd[512];
    string args;
    for(int i = 1; i < argc; i++)
    {
	args += ' '; // add space between args
        args += argv[i];
        
   }
   sprintf(cmd,"python mypyfile.py %s",args.c_str());
   system(cmd);
   return 0;
}

This works perfectly, I encountered and error but remembered that printf was a C function, so sprintf must be too! So adding the string conversion command works!!

I'm trying to use C++ to pass arguments to my python code.
Thanks guys!!!! You rock!!!

I'll make sure to bug you guys more! Because I will need more help on my journey into C++!

sprintf(cmd,"python mypyfile.py %s",args.c_str());
   system(cmd);

You could do it this way to, to avoid using C character arrays.

string args = "python mypyfile.py ";
    for(int i = 1; i < argc; i++)
    {
	args += ' '; // add space between args
        args += argv[i];
   }
   system(args.c_str());
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.