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

Re: Passing command line argument to function

 
0
  #11
Mar 27th, 2009
Hi Jason,
thanks a lot for the explanation. However, I want the program to take only 1 parameters for the seed (argv[1] will be assign to seed).
So I want to get rid of the parse function. Can I do it like:

  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. unsigned int seed;
  15. int puzzle[ROWS][COLS];
  16.  
  17. if (argc != 2)
  18. {
  19. cout << "Please enter right number of argument";
  20. return 1;
  21. }
  22.  
  23. seed = atoi (argv[1]);
  24.  
  25. initPuzzle(&puzzle, seed);
  26.  
  27. cout << "vn : " << "(0 <= n <= 2) to move column n down 1 position" << "\n";
  28. cout << "hn : " << "(0 <= n <= 1) to move row n right 1 position" << "\n";
  29. cout << "i : " << "to print these instructions" << "\n";
  30. cout << "q : " << "to quit" << "\n";
  31.  
  32.  
  33.  
  34. system("PAUSE");
  35. return 0;
  36. }
  37.  
  38.  
  39. void movePuzzle(int puzzle[][COLS], char dir, int rowcol)
  40. {
  41. }

would this works? I mean to populate the array (since the function to set up and populate the array is initPuzzle).
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Passing command line argument to function

 
0
  #12
Mar 27th, 2009
About converting the seed, you'd be better of using stringstream (atoi() is a poor choice for that). Below is a snippet for the
conversion ...
  1. #include <sstream>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main(int argc, char * argv[])
  6. {
  7. // only two arguments accepted, check ...
  8. if(argc != 2)
  9. {
  10. cout << "Wrong number of arguments: " << argc << endl;
  11. return 1;
  12. }
  13.  
  14. // Initialize the stringstream object with the argument
  15. stringstream ss(argv[1]);
  16. unsigned int seed;
  17.  
  18. // Try converting the seed
  19. if( ! (ss >> seed))
  20. {
  21. cout << "Invalid argument for seed: " << argv[1] << endl;
  22. return 1;
  23. }
  24.  
  25. cout << "Got seed: " << seed << endl;
  26.  
  27. return 0;
  28. }
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
  #13
Mar 27th, 2009
Hmm, I tried to print the 2D array:

  1. #include "puzzle.h"
  2.  
  3. void printPuzzle(int puzzle[][COLS]);
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. unsigned int seed;
  8. int puzzle[ROWS][COLS];
  9.  
  10. if (argc != 2)
  11. {
  12. cout << "Please enter right number of argument";
  13. return 1;
  14. }
  15.  
  16. seed = atoi (argv[1]);
  17.  
  18. initPuzzle(puzzle, seed);
  19.  
  20. cout << "vn : " << "(0 <= n <= 2) to move column n down 1 position" << "\n";
  21. cout << "hn : " << "(0 <= n <= 1) to move row n right 1 position" << "\n";
  22. cout << "i : " << "to print these instructions" << "\n";
  23. cout << "q : " << "to quit" << "\n";
  24.  
  25. printPuzzle(puzzle);
  26.  
  27. system("PAUSE");
  28. return 0;
  29. }
  30.  
  31. void printPuzzle(int puzzle[][COLS])
  32. {
  33. int i,j;
  34.  
  35. for(i = 0; i < ROWS; i++)
  36. {
  37. for(j = 0; j < COLS; j++)
  38. cout << " " << puzzle[i][j];
  39. cout << endl;
  40. }
  41. }

i ran the program and gives it input parameter

./a.out 1234

but it gives me this array output

1 2 3
4 5 6

which means the initPuzzle doesn't work.

However, this is the kind of function I'm expecting

| 0 1 2
-----+----------------------
0 | 5 3 6
1 | 4 2 1

any idea how to make the initPuzzle works (scramble the puzzle) and how to format the output so that I got that dash and lines?
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
  #14
Mar 27th, 2009
Originally Posted by mitrmkar View Post
About converting the seed, you'd be better of using stringstream (atoi() is a poor choice for that). Below is a snippet for the
conversion ...
  1. #include <sstream>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main(int argc, char * argv[])
  6. {
  7. // only two arguments accepted, check ...
  8. if(argc != 2)
  9. {
  10. cout << "Wrong number of arguments: " << argc << endl;
  11. return 1;
  12. }
  13.  
  14. // Initialize the stringstream object with the argument
  15. stringstream ss(argv[1]);
  16. unsigned int seed;
  17.  
  18. // Try converting the seed
  19. if( ! (ss >> seed))
  20. {
  21. cout << "Invalid argument for seed: " << argv[1] << endl;
  22. return 1;
  23. }
  24.  
  25. cout << "Got seed: " << seed << endl;
  26.  
  27. return 0;
  28. }
Hi, umm... I appreciate the concern for using sstream. But the project specification requires me not to add any other library in the code. I'm not allowed to change the puzzle.h (that means no new library should be used). Sorry but that's the limitation I have
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Passing command line argument to function

 
0
  #15
Mar 27th, 2009
Originally Posted by delifion View Post
Hi, umm... I appreciate the concern for using sstream. But the project specification requires me not to add any other library in the code. I'm not allowed to change the puzzle.h (that means no new library should be used). Sorry but that's the limitation I have
Oh I see, let the professor have his/her ways then. But in the future, you know to avoid atoi() .
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
  #16
Mar 27th, 2009
does anyone know how to make the initPuzzle works properly?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Passing command line argument to function

 
0
  #17
Mar 28th, 2009
Originally Posted by delifion View Post
does anyone know how to make the initPuzzle works properly?
What code you have in movePuzzle(...)?
Last edited by mitrmkar; Mar 28th, 2009 at 4:38 am.
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
  #18
Mar 28th, 2009
Originally Posted by mitrmkar View Post
What code you have in movePuzzle(...)?
The function initPuzzle suppose to populate array, setup the puzzle and scramble the order of number.

So it will be something like
| 0 1 2
--------
0 | 1 6 3
1 | 2 5 4

the movePuzzle is to give command (input from user)
(v = vertical)/(h = horizontal) + number of column/row to be shifted.

for example (still related to array above) user input v0, it will shift
the first column down one square.

new array after user input:

| 0 1 2
--------
0 | 2 6 3
1 | 1 5 4
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Passing command line argument to function

 
0
  #19
Mar 28th, 2009
Originally Posted by delifion View Post
The function initPuzzle suppose to populate array, setup the puzzle and scramble the order of number.
As per the code you are given in puzzle.h, the scrambling of the puzzle is actually done by means of the movePuzzle(), hence I asked about its code. If you haven't coded it yet, your output will be the initialized puzzle, i.e. 1,2,3,4,5,6.
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
  #20
Mar 28th, 2009
Originally Posted by mitrmkar View Post
As per the code you are given in puzzle.h, the scrambling of the puzzle is actually done by means of the movePuzzle(), hence I asked about its code. If you haven't coded it yet, your output will be the initialized puzzle, i.e. 1,2,3,4,5,6.
Hmm,
but initPuzzle function is in the puzzle.h

  1. void initPuzzle(int puzzle[][COLS], unsigned int seed)
  2. {
  3. /* Initialise the puzzle and scramble it around */
  4.  
  5. int x, y;
  6.  
  7. // initialise random number generator using the seed value
  8. srand(seed);
  9.  
  10. // fill the puzzle
  11. for (y=0; y<ROWS; y++) {
  12. for (x=0; x<COLS; x++) {
  13. puzzle[y][x] = 1 + y * COLS + x;
  14. }
  15. }
  16.  
  17. // scramble the puzzle
  18. for (y=0; y<=ROWS*COLS; y++) {
  19. if ((rand() % 2) == 0) {
  20. movePuzzle(puzzle, 'v', rand() % COLS);
  21. }
  22. else {
  23. movePuzzle(puzzle, 'h', rand() % ROWS);
  24. }
  25. }
  26. }

so it should scramble the puzzle around right?

I think there is something wrong with 'seed', I convert the input from user in argv[1] to int and then assign it to seed.
From there, i pass seed to initPuzzle as argument, which should do the setup and scrambling.

movePuzzle is the part where user can swap the order around (but the scrambling should be done in initPuzzle)
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