Passing multiple Arguments

Started by
7 comments, last by jouley 17 years, 2 months ago
Hello, I'm trying to make a simple GUI. I want to make a function that receives the number of arguments and their names. show_menu(int argc, char *argv[]) //I copied int main(int argc, char *argv[]) { /*while int i < argc, cout argv*/ } My problem is I don't know what to pass. show_menu(3, "save", "continue", "exit"); //obviously doesn't work Can someone help me please?
Advertisement
assuming C++:

void show_menu( std::vector<std::string> const& args ){  ...}

What do you want show_menu to actually do? You may not need such a convoluted solution, but we won't know for sure unless you tell us what you're trying to do. If you have any code, you could post it inside of [source] and [/source] tags to have it formatted nicely.

One option you may want to consider, if you do need to have multiple strings passed to a function, is to use a std::vector of std::strings. You can create one like this:
std::vector<std::string> vecStrings;vecStrings.push_back("save");vecStrings.push_back("continue");vecStrings.push_back("exit");show_menu(vecStrings);//  show_menu definitionvoid show_menu(const std::vector<std::string>& vecStrings){    for (int i = 0; i < vecStrings.size( ); ++i)        //  do something, like draw_item(const std::string&), the strings:        draw_item(vecStrings);}

There's much more to it than that, but it's something to think about. If this is the sort of functionality you're looking for, it'll be much easier and safer if you use some of C++'s capabilities. If, however, this is C, you'll need to tell us what show_menu does, exactly.

Best of luck!
-jouley
Ok, your function declares that it takes an array. How do you define an array?
char [] myArray = new char[ 10 ];
myArray[0] = 'x';
...
myArray[9] = 'y';
myFunctionWhichTakesAnArray( myArray );

But you want variable parameters right? Thats totally different.
example:

 #include <stdarg.h>#define  MAXARGS     31/* * execl is called by * execl(file, arg1, arg2, ..., (char *)(0)); */int execl(const char *file, const char *args, ...){    va_list ap;    char *array[MAXARGS +1];    int argno = 0;    va_start(ap, args);    while (args != 0 && argno < MAXARGS)    {        array[argno++] = args;        args = va_arg(ap, const char *);    }    array[argno] = (char *) 0;    va_end(ap);    return execv(file, array);}

look up 'variable length parameter lists'
http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html

Quote:
It is often desirable to implement a function where the number of arguments is not known, or is not constant, when the function is written. Such a function is printf, described in Section 9.11. The following example shows the declaration of such a function.
I'm using C++, I'm not very familiar with C.

Ok, so far i've got...

int menu(int argc, char *argv[]){  int i = 0;  while(i < argc)  {    cout<<argv<<'\n';  }  return 0;//I will intend to return the users selection.}int main(){  char mainMenu[][5] = {{'s', 'a', 'v', 'e', '\0'}, {"cont"}, {"exit"}};  menu(3, mainMenu);//this is the part I cant get to work  return 0;}


For your simplicity I've left it as a consolish program, all I'm tryining to do here is display the simple menu on the screen by calling a function.

Thanks for the support so far.
Ben.
Please consider using C++'s available constructs for passing "multiple arguments." Here's a version of what you've done, using a quick and safe std::vector:
#include <iostream>#include <vector>#include <string>int menu(std::vector<std::string>& vecMenu){	for (int i = 0; i < (int)vecMenu.size( ); ++i)	{		std::cout << vecMenu << std::endl;	}	return 0;//I will intend to return the users selection.}int main(){	std::vector<std::string> vecMenu;	vecMenu.push_back("save");	vecMenu.push_back("continue");	//	it's okay to have variable sizes!	vecMenu.push_back("exit");	//	or "leave" or "walk away" or "run"...	menu(vecMenu);	return 0;}

See how much easier it is to read? See how much safer it is, how much cleaner it is? If not, just take my word (and that of RDragon1 and countless others) for it. [wink]

-jouley
Ah, Thankyou for that, As you said It works really well!

Until now i have simply used: using namespace std;

Do you have some recomended reading I could do? As most of this is new to me.
Quote:Original post by fujitsu
Until now i have simply used: using namespace std;

Gulp!
Quote:Do you have some recomended reading I could do? As most of this is new to me.

Others will undoubtedly have more, but here's a small list:

Thinking in C++
http://www.cplusplus.com/doc/tutorial/
http://www.cprogramming.com/tutorial.html#c++tutorial

Finally, while a little more advanced, is a reference with unlimited utility (referenced at the beginning of this post): The C++ FAQ Lite

That should get you started!
-jouley

This topic is closed to new replies.

Advertisement