| | |
Passing command line argument to function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 34
Reputation:
Solved Threads: 0
Hi, I've got a puzzle game program consist of 2D array 2 rows x 3 cols.
The professor wants no changes to be made in puzzle.h, which is:
This is my puzzle.cpp which includes puzzle.h
My problem is to call initPuzzle function before any other code.
I need to pass array and seed using command line argument.
I'm new to C++, can someone explain how to do that? (the line with ????)
initPuzzle function suppose to set up the puzzle and randomise it.
The function itself is in the puzzle.h and I can only call it in the puzzle.cpp (no changes can be made to puzzle.h).
Any help would be appreciated.
The professor wants no changes to be made in puzzle.h, which is:
c++ Syntax (Toggle Plain Text)
#ifndef _PUZZLE_H #define _PUZZLE_H #include <iostream> #include <iomanip> #include <stdlib.h> using namespace std; // Constants const int COLS = 3; const int ROWS = 2; // Function prototypes // Your cpp file must have the code for movePuzzle void movePuzzle(int puzzle[][COLS], char dir, int rowcol); void initPuzzle(int puzzle[][COLS], unsigned int seed); void initPuzzle(int puzzle[][COLS], unsigned int seed) { /* Initialise the puzzle and scramble it around */ int x, y; // initialise random number generator using the seed value srand(seed); // fill the puzzle for (y=0; y<ROWS; y++) { for (x=0; x<COLS; x++) { puzzle[y][x] = 1 + y * COLS + x; } } // scramble the puzzle for (y=0; y<=ROWS*COLS; y++) { if ((rand() % 2) == 0) { movePuzzle(puzzle, 'v', rand() % COLS); } else { movePuzzle(puzzle, 'h', rand() % ROWS); } } } #endif
This is my puzzle.cpp which includes puzzle.h
c++ Syntax (Toggle Plain Text)
/*****************************************************************************\ This is a C++ program that runs a puzzle game. The puzzle consist of 2 row x 3 column grid. Grid filled with numbers 1 to 6 in random order. The main aim of this puzzle game is to get user swap those number so that they are in order (with number 1 in top left and 6 in bottom right). \*****************************************************************************/ #include "puzzle.h" int main(int argc, char *argv[]) { int puzzle[ROWS][COLS]; initPuzzle(); ????? cout << "vn : " << "(0 <= n <= 2) to move column n down 1 position" << "\n"; cout << "hn : " << "(0 <= n <= 1) to move row n right 1 position" << "\n"; cout << "i : " << "to print these instructions" << "\n"; cout << "q : " << "to quit" << "\n"; system("PAUSE"); return 0; } void movePuzzle(int puzzle[][COLS], char dir, int rowcol) { }
My problem is to call initPuzzle function before any other code.
I need to pass array and seed using command line argument.
I'm new to C++, can someone explain how to do that? (the line with ????)
initPuzzle function suppose to set up the puzzle and randomise it.
The function itself is in the puzzle.h and I can only call it in the puzzle.cpp (no changes can be made to puzzle.h).
Any help would be appreciated.
If im understanding you right, you need to populate the array from the command line.
So you should set up a command line function that is called just before initPuzzle(); ?????. Just make sure that the user only enters enough numbers to fill the array. You can do this in the commandLineArguments() function.
the prototype should look like this:
void commandLineArguments( int argc, char *argv[], int puzzle[][], int seed);
then just call initPuzzle(); with the correct arguments. initPuzzle(puzzle, seed)
So you should set up a command line function that is called just before initPuzzle(); ?????. Just make sure that the user only enters enough numbers to fill the array. You can do this in the commandLineArguments() function.
the prototype should look like this:
void commandLineArguments( int argc, char *argv[], int puzzle[][], int seed);
then just call initPuzzle(); with the correct arguments. initPuzzle(puzzle, seed)
•
•
Join Date: Oct 2007
Posts: 34
Reputation:
Solved Threads: 0
•
•
•
•
If im understanding you right, you need to populate the array from the command line.
So you should set up a command line function that is called just before initPuzzle(); ?????. Just make sure that the user only enters enough numbers to fill the array. You can do this in the commandLineArguments() function.
the prototype should look like this:
void commandLineArguments( int argc, char *argv[], int puzzle[][], int seed);
then just call initPuzzle(); with the correct arguments. initPuzzle(puzzle, seed)
I tried something like this:
c++ Syntax (Toggle Plain Text)
int main(int argc, char *argv[]) { int puzzle[ROWS][COLS]; void commandLineArguments(int argc, char *argv[], int puzzle[][COLS], unsigned int seed); initPuzzle(puzzle, seed); cout << "vn : " << "(0 <= n <= 2) to move column n down 1 position" << "\n"; cout << "hn : " << "(0 <= n <= 1) to move row n right 1 position" << "\n"; cout << "i : " << "to print these instructions" << "\n"; cout << "q : " << "to quit" << "\n"; system("PAUSE"); return 0; }
but the compiler complains:
in line initPuzzle(puzzle, seed);
`seed' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
As Danny said, declare seed in main().
Second, to have the function modify a variable you need something like
Second, to have the function modify a variable you need something like
void foo(int &bar); note the "&". Last edited by MosaicFuneral; Mar 27th, 2009 at 2:35 am.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
•
•
Join Date: Oct 2007
Posts: 34
Reputation:
Solved Threads: 0
I've been working on it for hours... >.<
So the program will take command line argument and pass it to initPuzzle right?
I'm a bit confused about the commandLineArguments function.
Can't I just use the convention main(int argc, char *argv[]) and then do something in the initPuzzle to receive those argument?
I've change my code to this:
but i'm still not sure what goes in the commandLineArguments function.
I'm still banging my head against the wall in learning command line argument and passing it into function as array.
Can someone explain what should I do so that the puzzle array can be set up and randomise through initPuzzle? >.<
So the program will take command line argument and pass it to initPuzzle right?
I'm a bit confused about the commandLineArguments function.
Can't I just use the convention main(int argc, char *argv[]) and then do something in the initPuzzle to receive those argument?
I've change my code to this:
c++ Syntax (Toggle Plain Text)
/*****************************************************************************\ This is a C++ program that runs a puzzle game. The puzzle consist of 2 row x 3 column grid. Grid filled with numbers 1 to 6 in random order. The main aim of this puzzle game is to get user swap those number so that they are in order (with number 1 in top left and 6 in bottom right). \*****************************************************************************/ #include "puzzle.h" void commandLineArguments(int argc, char *argv[], int puzzle[][COLS], unsigned int seed); int main(int argc, char *argv[]) { int seed; int puzzle[ROWS][COLS]; commandLineArguments(argc, argv, puzzle, seed); initPuzzle(puzzle, seed); cout << "vn : " << "(0 <= n <= 2) to move column n down 1 position" << "\n"; cout << "hn : " << "(0 <= n <= 1) to move row n right 1 position" << "\n"; cout << "i : " << "to print these instructions" << "\n"; cout << "q : " << "to quit" << "\n"; system("PAUSE"); return 0; } void commandLineArguments(int argc, char *argv[], int puzzle[][COLS], unsigned int seed) { } void movePuzzle(int puzzle[][COLS], char dir, int rowcol) { }
but i'm still not sure what goes in the commandLineArguments function.
I'm still banging my head against the wall in learning command line argument and passing it into function as array.
Can someone explain what should I do so that the puzzle array can be set up and randomise through initPuzzle? >.<
•
•
Join Date: Oct 2007
Posts: 34
Reputation:
Solved Threads: 0
Hmm, I've been working on it again for 2 hours now...
this all I got... When I tried to print the puzzle array, it just display some random memory. I have no idea what seem to be the problem.
this all I got... When I tried to print the puzzle array, it just display some random memory. I have no idea what seem to be the problem.
c++ Syntax (Toggle Plain Text)
/*****************************************************************************\ This is a C++ program that runs a puzzle game. The puzzle consist of 2 row x 3 column grid. Grid filled with numbers 1 to 6 in random order. The main aim of this puzzle game is to get user swap those number so that they are in order (with number 1 in top left and 6 in bottom right). \*****************************************************************************/ #include "puzzle.h" void commandLineArguments(int argc, char *argv[], int puzzle[][COLS], unsigned int seed); int main(int argc, char *argv[]) { int seed; int puzzle[ROWS][COLS]; commandLineArguments(argc, argv, puzzle, seed); initPuzzle(puzzle, seed); cout << "vn : " << "(0 <= n <= 2) to move column n down 1 position" << "\n"; cout << "hn : " << "(0 <= n <= 1) to move row n right 1 position" << "\n"; cout << "i : " << "to print these instructions" << "\n"; cout << "q : " << "to quit" << "\n"; cout << puzzle; system("PAUSE"); return 0; } void commandLineArguments(int argc, char *argv[], int puzzle[][COLS], unsigned int seed) { if (argc != 2) { cout << "Please enter right number of argument"; } &seed = atoi (argv[1]); } void movePuzzle(int puzzle[][COLS], char dir, int rowcol) { }
Hey there, first up you need to decide exactly what you are going to be passing via the command line when the program is called..
i.e. perhaps you're going to call the program and pass the numbers to populate the array and the seed..
The numbers used are 1 to 6 and for the sake of argument, we'll use a seed of 45 which gives us a total of 7 parameters..
So for this example you'll end up calling the puzzle program passing 1 2 3 4 5 6 and 45 as parameters.
ok, thats all straightforward so far..
The purpose of your commandlinearguments function is to parse the arguments passed into the main function to try to extract the bits you want to use.
Personally I'd rename the commandlinearguments function to parseCommandLineArguments (cause that's what it's doing) and make it return a bool (to denote whether the parsing succeded or failed)....Alternatively, make it return an int and use 1 for success and 0 for failure!
So you'd have something like this:
I hope that makes some sense, I purposely haven't filled in all of the blanks for you. But fingers crossed, that should be enough for you to get on with and find your way forward!
i.e. perhaps you're going to call the program and pass the numbers to populate the array and the seed..
The numbers used are 1 to 6 and for the sake of argument, we'll use a seed of 45 which gives us a total of 7 parameters..
So for this example you'll end up calling the puzzle program passing 1 2 3 4 5 6 and 45 as parameters.
ok, thats all straightforward so far..
The purpose of your commandlinearguments function is to parse the arguments passed into the main function to try to extract the bits you want to use.
Personally I'd rename the commandlinearguments function to parseCommandLineArguments (cause that's what it's doing) and make it return a bool (to denote whether the parsing succeded or failed)....Alternatively, make it return an int and use 1 for success and 0 for failure!
So you'd have something like this:
c++ Syntax (Toggle Plain Text)
/*****************************************************************************\ This is a C++ program that runs a puzzle game. The puzzle consist of 2 row x 3 column grid. Grid filled with numbers 1 to 6 in random order. The main aim of this puzzle game is to get user swap those number so that they are in order (with number 1 in top left and 6 in bottom right). \*****************************************************************************/ #include "puzzle.h" // NOTE: return type set to bool to indicate whether or not parsing was successful! (or you could use int as a return type using 1 for true and 0 for false!) bool parseCommandLineArguments(int argc, char *argv[], int puzzle[][COLS], unsigned int &seed); // seed passed as a reference, so we can modify it's contents...alteratively a pointer would suffice! (change & to *) int main(int argc, char *argv[]) { unsigned int seed; // NOTE: in your .h file, seed is an unsigned int! int puzzle[ROWS][COLS]; // as long as parsing of the command line succeeds, then we can setup and play the game... if ( parseCommandLineArguments(argc, argv, puzzle, seed) ) { initPuzzle(puzzle, seed); cout << "vn : " << "(0 <= n <= 2) to move column n down 1 position" << "\n"; cout << "hn : " << "(0 <= n <= 1) to move row n right 1 position" << "\n"; cout << "i : " << "to print these instructions" << "\n"; cout << "q : " << "to quit" << "\n"; // I'm guessing you'll be putting your main game loop somewhere in here at some point! } // You'll end up here at the end of the game loop or if parsing fails.. system("PAUSE"); return 0; } bool parseCommandLineArguments(int argc, char *argv[], int puzzle[][COLS], unsigned int &seed) { // if there are 7 parameters, then we're ok to continue parsing if(argc==7) { // Now all you have to do is loop through the parameters in argv, strip out any control characters (i.e. '/' if you passed /1 /2 /3 /4 /5 /6 /45 as params) // check that they are valid i.e. numerical and not characters // If valid, copy them to the appropriate place, either the puzzle array or the seed // else display an appropriate error message and return false (parsing failed) return true; // if you get this far without returning false, then the parsing is complete and successful } else { // Show an error message: parsing failed due to an incorrect number of parameters return false; } }
I hope that makes some sense, I purposely haven't filled in all of the blanks for you. But fingers crossed, that should be enough for you to get on with and find your way forward!
Last edited by JasonHippy; Mar 27th, 2009 at 9:10 am.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Those who understand binary .....
And those who don't!
•
•
•
•
Hmm, I've been working on it again for 2 hours now...
this all I got... When I tried to print the puzzle array, it just display some random memory. I have no idea what seem to be the problem.
c++ Syntax (Toggle Plain Text)
/*****************************************************************************\ This is a C++ program that runs a puzzle game. The puzzle consist of 2 row x 3 column grid. Grid filled with numbers 1 to 6 in random order. The main aim of this puzzle game is to get user swap those number so that they are in order (with number 1 in top left and 6 in bottom right). \*****************************************************************************/ #include "puzzle.h" void commandLineArguments(int argc, char *argv[], int puzzle[][COLS], unsigned int seed); int main(int argc, char *argv[]) { int seed; int puzzle[ROWS][COLS]; commandLineArguments(argc, argv, puzzle, seed); initPuzzle(puzzle, seed); cout << "vn : " << "(0 <= n <= 2) to move column n down 1 position" << "\n"; cout << "hn : " << "(0 <= n <= 1) to move row n right 1 position" << "\n"; cout << "i : " << "to print these instructions" << "\n"; cout << "q : " << "to quit" << "\n"; cout << puzzle; system("PAUSE"); return 0; } void commandLineArguments(int argc, char *argv[], int puzzle[][COLS], unsigned int seed) { if (argc != 2) { cout << "Please enter right number of argument"; } &seed = atoi (argv[1]); } void movePuzzle(int puzzle[][COLS], char dir, int rowcol) { }
•
•
•
•
You can't cout a 2d array like that. You'll have to make a nested loop, which loops trough all the elements in the array and prints them one at a time.
Sorry I missed these posts as they were posted while I made the mammoth post above...Niek is quite correct, when trying to output the contents of a 2D array you need to set up some nested loops to loop through the entire array and output each item individually.
The reason you got a seemingly random memory address output by the cout statement is because essentially your puzzle variable itself is a pointer...Any array object is automatically treated as a pointer. The value you were getting from your cout was the address of the first element of the array!
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Those who understand binary .....
And those who don't!
![]() |
Similar Threads
- passing parameters to .exe file using C++ (C++)
- Edit sendmain aliases file. (Perl)
- array with pointers (C++)
- GUIs with c++ (C++)
- seek function (Perl)
- C++ GUI (Graphical User Interface) for beginners (C++)
- help with writing each 100 lines into different files. (C++)
- need help with a project (Shell Scripting)
- Problem in passing parameters (Visual Basic 4 / 5 / 6)
- Exit Codes (Python)
Other Threads in the C++ Forum
- Previous Thread: C++ Windows API over-riding?
- Next Thread: Array help
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






