Passing command line argument to function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 34
Reputation: delifion is an unknown quantity at this point 
Solved Threads: 0
delifion delifion is offline Offline
Light Poster

Passing command line argument to function

 
0
  #1
Mar 27th, 2009
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:

  1. #ifndef _PUZZLE_H
  2. #define _PUZZLE_H
  3.  
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <stdlib.h>
  7.  
  8. using namespace std;
  9.  
  10. // Constants
  11. const int COLS = 3;
  12. const int ROWS = 2;
  13.  
  14. // Function prototypes
  15. // Your cpp file must have the code for movePuzzle
  16. void movePuzzle(int puzzle[][COLS], char dir, int rowcol);
  17. void initPuzzle(int puzzle[][COLS], unsigned int seed);
  18.  
  19. void initPuzzle(int puzzle[][COLS], unsigned int seed)
  20. {
  21. /* Initialise the puzzle and scramble it around */
  22.  
  23. int x, y;
  24.  
  25. // initialise random number generator using the seed value
  26. srand(seed);
  27.  
  28. // fill the puzzle
  29. for (y=0; y<ROWS; y++) {
  30. for (x=0; x<COLS; x++) {
  31. puzzle[y][x] = 1 + y * COLS + x;
  32. }
  33. }
  34.  
  35. // scramble the puzzle
  36. for (y=0; y<=ROWS*COLS; y++) {
  37. if ((rand() % 2) == 0) {
  38. movePuzzle(puzzle, 'v', rand() % COLS);
  39. }
  40. else {
  41. movePuzzle(puzzle, 'h', rand() % ROWS);
  42. }
  43. }
  44. }
  45.  
  46. #endif

This is my puzzle.cpp which includes puzzle.h

  1. /*****************************************************************************\
  2. This is a C++ program that runs a puzzle game.
  3. The puzzle consist of 2 row x 3 column grid.
  4. Grid filled with numbers 1 to 6 in random order.
  5.  
  6. The main aim of this puzzle game is to get user swap those number so that
  7. they are in order (with number 1 in top left and 6 in bottom right).
  8. \*****************************************************************************/
  9.  
  10. #include "puzzle.h"
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14. int puzzle[ROWS][COLS];
  15.  
  16. initPuzzle(); ?????
  17.  
  18. cout << "vn : " << "(0 <= n <= 2) to move column n down 1 position" << "\n";
  19. cout << "hn : " << "(0 <= n <= 1) to move row n right 1 position" << "\n";
  20. cout << "i : " << "to print these instructions" << "\n";
  21. cout << "q : " << "to quit" << "\n";
  22.  
  23. system("PAUSE");
  24. return 0;
  25. }
  26.  
  27. void movePuzzle(int puzzle[][COLS], char dir, int rowcol)
  28. {
  29. }

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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 39
Reputation: Danny_501 is an unknown quantity at this point 
Solved Threads: 1
Danny_501's Avatar
Danny_501 Danny_501 is offline Offline
Light Poster

Re: Passing command line argument to function

 
0
  #2
Mar 27th, 2009
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)
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 34
Reputation: delifion is an unknown quantity at this point 
Solved Threads: 0
delifion delifion is offline Offline
Light Poster

Re: Passing command line argument to function

 
0
  #3
Mar 27th, 2009
Originally Posted by Danny_501 View Post
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)
Hmm, so the command line function is a new function that I should declare inside the int main(int argc, char *argv[])?

I tried something like this:
  1. int main(int argc, char *argv[])
  2. {
  3. int puzzle[ROWS][COLS];
  4.  
  5. void commandLineArguments(int argc, char *argv[], int puzzle[][COLS], unsigned int seed);
  6.  
  7. initPuzzle(puzzle, seed);
  8.  
  9. cout << "vn : " << "(0 <= n <= 2) to move column n down 1 position" << "\n";
  10. cout << "hn : " << "(0 <= n <= 1) to move row n right 1 position" << "\n";
  11. cout << "i : " << "to print these instructions" << "\n";
  12. cout << "q : " << "to quit" << "\n";
  13.  
  14. system("PAUSE");
  15. return 0;
  16. }

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.)
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 941
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Passing command line argument to function

 
0
  #4
Mar 27th, 2009
As Danny said, declare seed in main().
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 34
Reputation: delifion is an unknown quantity at this point 
Solved Threads: 0
delifion delifion is offline Offline
Light Poster

Re: Passing command line argument to function

 
0
  #5
Mar 27th, 2009
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:
  1. /*****************************************************************************\
  2. This is a C++ program that runs a puzzle game.
  3. The puzzle consist of 2 row x 3 column grid.
  4. Grid filled with numbers 1 to 6 in random order.
  5.  
  6. The main aim of this puzzle game is to get user swap those number so that
  7. they are in order (with number 1 in top left and 6 in bottom right).
  8. \*****************************************************************************/
  9.  
  10. #include "puzzle.h"
  11.  
  12. void commandLineArguments(int argc, char *argv[], int puzzle[][COLS], unsigned int seed);
  13.  
  14. int main(int argc, char *argv[])
  15. {
  16. int seed;
  17. int puzzle[ROWS][COLS];
  18.  
  19. commandLineArguments(argc, argv, puzzle, seed);
  20.  
  21. initPuzzle(puzzle, seed);
  22.  
  23. cout << "vn : " << "(0 <= n <= 2) to move column n down 1 position" << "\n";
  24. cout << "hn : " << "(0 <= n <= 1) to move row n right 1 position" << "\n";
  25. cout << "i : " << "to print these instructions" << "\n";
  26. cout << "q : " << "to quit" << "\n";
  27.  
  28. system("PAUSE");
  29. return 0;
  30. }
  31.  
  32. void commandLineArguments(int argc, char *argv[], int puzzle[][COLS], unsigned int seed)
  33. {
  34. }
  35.  
  36. void movePuzzle(int puzzle[][COLS], char dir, int rowcol)
  37. {
  38. }

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? >.<
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 34
Reputation: delifion is an unknown quantity at this point 
Solved Threads: 0
delifion delifion is offline Offline
Light Poster

Re: Passing command line argument to function

 
0
  #6
Mar 27th, 2009
Can someone explain specifically about command line argument?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 34
Reputation: delifion is an unknown quantity at this point 
Solved Threads: 0
delifion delifion is offline Offline
Light Poster

Re: Passing command line argument to function

 
0
  #7
Mar 27th, 2009
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.

  1. /*****************************************************************************\
  2. This is a C++ program that runs a puzzle game.
  3. The puzzle consist of 2 row x 3 column grid.
  4. Grid filled with numbers 1 to 6 in random order.
  5.  
  6. The main aim of this puzzle game is to get user swap those number so that
  7. they are in order (with number 1 in top left and 6 in bottom right).
  8. \*****************************************************************************/
  9.  
  10. #include "puzzle.h"
  11.  
  12. void commandLineArguments(int argc, char *argv[], int puzzle[][COLS], unsigned int seed);
  13.  
  14. int main(int argc, char *argv[])
  15. {
  16. int seed;
  17. int puzzle[ROWS][COLS];
  18.  
  19. commandLineArguments(argc, argv, puzzle, seed);
  20.  
  21. initPuzzle(puzzle, seed);
  22.  
  23. cout << "vn : " << "(0 <= n <= 2) to move column n down 1 position" << "\n";
  24. cout << "hn : " << "(0 <= n <= 1) to move row n right 1 position" << "\n";
  25. cout << "i : " << "to print these instructions" << "\n";
  26. cout << "q : " << "to quit" << "\n";
  27.  
  28. cout << puzzle;
  29.  
  30. system("PAUSE");
  31. return 0;
  32. }
  33.  
  34. void commandLineArguments(int argc, char *argv[], int puzzle[][COLS], unsigned int seed)
  35. {
  36. if (argc != 2)
  37. {
  38. cout << "Please enter right number of argument";
  39. }
  40.  
  41. &seed = atoi (argv[1]);
  42.  
  43. }
  44.  
  45. void movePuzzle(int puzzle[][COLS], char dir, int rowcol)
  46. {
  47. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,823
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

Re: Passing command line argument to function

 
0
  #8
Mar 27th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 302
Reputation: JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough 
Solved Threads: 52
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz

Re: Passing command line argument to function

 
0
  #9
Mar 27th, 2009
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:
  1. /*****************************************************************************\
  2. This is a C++ program that runs a puzzle game.
  3. The puzzle consist of 2 row x 3 column grid.
  4. Grid filled with numbers 1 to 6 in random order.
  5.  
  6. The main aim of this puzzle game is to get user swap those number so that
  7. they are in order (with number 1 in top left and 6 in bottom right).
  8. \*****************************************************************************/
  9.  
  10. #include "puzzle.h"
  11.  
  12. // 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!)
  13. 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 *)
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17. unsigned int seed; // NOTE: in your .h file, seed is an unsigned int!
  18. int puzzle[ROWS][COLS];
  19.  
  20. // as long as parsing of the command line succeeds, then we can setup and play the game...
  21. if ( parseCommandLineArguments(argc, argv, puzzle, seed) )
  22. {
  23. initPuzzle(puzzle, seed);
  24.  
  25. cout << "vn : " << "(0 <= n <= 2) to move column n down 1 position" << "\n";
  26. cout << "hn : " << "(0 <= n <= 1) to move row n right 1 position" << "\n";
  27. cout << "i : " << "to print these instructions" << "\n";
  28. cout << "q : " << "to quit" << "\n";
  29. // I'm guessing you'll be putting your main game loop somewhere in here at some point!
  30. }
  31.  
  32. // You'll end up here at the end of the game loop or if parsing fails..
  33. system("PAUSE");
  34. return 0;
  35. }
  36.  
  37. bool parseCommandLineArguments(int argc, char *argv[], int puzzle[][COLS], unsigned int &seed)
  38. {
  39. // if there are 7 parameters, then we're ok to continue parsing
  40. if(argc==7)
  41. {
  42. // 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)
  43. // check that they are valid i.e. numerical and not characters
  44. // If valid, copy them to the appropriate place, either the puzzle array or the seed
  45. // else display an appropriate error message and return false (parsing failed)
  46.  
  47. return true; // if you get this far without returning false, then the parsing is complete and successful
  48. }
  49. else
  50. {
  51. // Show an error message: parsing failed due to an incorrect number of parameters
  52. return false;
  53. }
  54. }

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!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 302
Reputation: JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough 
Solved Threads: 52
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz

Re: Passing command line argument to function

 
0
  #10
Mar 27th, 2009
Originally Posted by delifion View Post
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.

  1. /*****************************************************************************\
  2. This is a C++ program that runs a puzzle game.
  3. The puzzle consist of 2 row x 3 column grid.
  4. Grid filled with numbers 1 to 6 in random order.
  5.  
  6. The main aim of this puzzle game is to get user swap those number so that
  7. they are in order (with number 1 in top left and 6 in bottom right).
  8. \*****************************************************************************/
  9.  
  10. #include "puzzle.h"
  11.  
  12. void commandLineArguments(int argc, char *argv[], int puzzle[][COLS], unsigned int seed);
  13.  
  14. int main(int argc, char *argv[])
  15. {
  16. int seed;
  17. int puzzle[ROWS][COLS];
  18.  
  19. commandLineArguments(argc, argv, puzzle, seed);
  20.  
  21. initPuzzle(puzzle, seed);
  22.  
  23. cout << "vn : " << "(0 <= n <= 2) to move column n down 1 position" << "\n";
  24. cout << "hn : " << "(0 <= n <= 1) to move row n right 1 position" << "\n";
  25. cout << "i : " << "to print these instructions" << "\n";
  26. cout << "q : " << "to quit" << "\n";
  27.  
  28. cout << puzzle;
  29.  
  30. system("PAUSE");
  31. return 0;
  32. }
  33.  
  34. void commandLineArguments(int argc, char *argv[], int puzzle[][COLS], unsigned int seed)
  35. {
  36. if (argc != 2)
  37. {
  38. cout << "Please enter right number of argument";
  39. }
  40.  
  41. &seed = atoi (argv[1]);
  42.  
  43. }
  44.  
  45. void movePuzzle(int puzzle[][COLS], char dir, int rowcol)
  46. {
  47. }
Originally Posted by niek_e View Post
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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC