943,701 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2008
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Mar 27th, 2009
0

Re: Passing command line argument to function

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:

c++ Syntax (Toggle Plain Text)
  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).
Reputation Points: 10
Solved Threads: 0
Light Poster
delifion is offline Offline
34 posts
since Oct 2007
Mar 27th, 2009
0

Re: Passing command line argument to function

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 ...
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 27th, 2009
0

Re: Passing command line argument to function

Hmm, I tried to print the 2D array:

c++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Light Poster
delifion is offline Offline
34 posts
since Oct 2007
Mar 27th, 2009
0

Re: Passing command line argument to function

Click to Expand / Collapse  Quote originally posted by mitrmkar ...
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 ...
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
delifion is offline Offline
34 posts
since Oct 2007
Mar 27th, 2009
0

Re: Passing command line argument to function

Click to Expand / Collapse  Quote originally posted by delifion ...
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() .
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 27th, 2009
0

Re: Passing command line argument to function

does anyone know how to make the initPuzzle works properly?
Reputation Points: 10
Solved Threads: 0
Light Poster
delifion is offline Offline
34 posts
since Oct 2007
Mar 28th, 2009
0

Re: Passing command line argument to function

Click to Expand / Collapse  Quote originally posted by delifion ...
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.
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 28th, 2009
0

Re: Passing command line argument to function

Click to Expand / Collapse  Quote originally posted by mitrmkar ...
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
Reputation Points: 10
Solved Threads: 0
Light Poster
delifion is offline Offline
34 posts
since Oct 2007
Mar 28th, 2009
0

Re: Passing command line argument to function

Click to Expand / Collapse  Quote originally posted by delifion ...
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.
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 28th, 2009
0

Re: Passing command line argument to function

Click to Expand / Collapse  Quote originally posted by mitrmkar ...
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

c++ Syntax (Toggle Plain Text)
  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)
Reputation Points: 10
Solved Threads: 0
Light Poster
delifion is offline Offline
34 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ Windows API over-riding?
Next Thread in C++ Forum Timeline: Array help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC