| | |
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 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:
would this works? I mean to populate the array (since the function to set up and populate the array is initPuzzle).
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)
/*****************************************************************************\ 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[]) { unsigned int seed; int puzzle[ROWS][COLS]; if (argc != 2) { cout << "Please enter right number of argument"; return 1; } seed = atoi (argv[1]); 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 movePuzzle(int puzzle[][COLS], char dir, int rowcol) { }
would this works? I mean to populate the array (since the function to set up and populate the array is initPuzzle).
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
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 ...
conversion ...
C++ Syntax (Toggle Plain Text)
#include <sstream> #include <iostream> using namespace std; int main(int argc, char * argv[]) { // only two arguments accepted, check ... if(argc != 2) { cout << "Wrong number of arguments: " << argc << endl; return 1; } // Initialize the stringstream object with the argument stringstream ss(argv[1]); unsigned int seed; // Try converting the seed if( ! (ss >> seed)) { cout << "Invalid argument for seed: " << argv[1] << endl; return 1; } cout << "Got seed: " << seed << endl; return 0; }
•
•
Join Date: Oct 2007
Posts: 34
Reputation:
Solved Threads: 0
Hmm, I tried to print the 2D array:
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?
c++ Syntax (Toggle Plain Text)
#include "puzzle.h" void printPuzzle(int puzzle[][COLS]); int main(int argc, char *argv[]) { unsigned int seed; int puzzle[ROWS][COLS]; if (argc != 2) { cout << "Please enter right number of argument"; return 1; } seed = atoi (argv[1]); 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"; printPuzzle(puzzle); system("PAUSE"); return 0; } void printPuzzle(int puzzle[][COLS]) { int i,j; for(i = 0; i < ROWS; i++) { for(j = 0; j < COLS; j++) cout << " " << puzzle[i][j]; cout << endl; } }
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?
•
•
Join Date: Oct 2007
Posts: 34
Reputation:
Solved Threads: 0
•
•
•
•
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)
#include <sstream> #include <iostream> using namespace std; int main(int argc, char * argv[]) { // only two arguments accepted, check ... if(argc != 2) { cout << "Wrong number of arguments: " << argc << endl; return 1; } // Initialize the stringstream object with the argument stringstream ss(argv[1]); unsigned int seed; // Try converting the seed if( ! (ss >> seed)) { cout << "Invalid argument for seed: " << argv[1] << endl; return 1; } cout << "Got seed: " << seed << endl; return 0; }
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
•
•
•
•
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
atoi() . •
•
Join Date: Oct 2007
Posts: 34
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
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.
•
•
Join Date: Oct 2007
Posts: 34
Reputation:
Solved Threads: 0
•
•
•
•
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.
but initPuzzle function is in the puzzle.h
c++ Syntax (Toggle Plain Text)
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); } } }
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)
![]() |
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 arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





