C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Passing command line argument to function Posted by delifion on 26 Mar 2009 at 10:20 PM
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:

#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); 
      }
   } 
}http://www.programmersheaven.com/mb/CandCPP/PostNew.aspx?S=B20000
Programmer's Heaven - Post New Message

#endif



This is my puzzle.cpp which includes puzzle.h

/*****************************************************************************\
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 temp;
    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).

If my understanding is right, I need to populate the array from the command line.

Any help would be appreciated.



Report
Re: Passing command line argument to function Posted by Malcolm_McLean on 27 Mar 2009 at 11:08 AM

User calls your program with an argument, eg

puzzle 1234

you extract the seed like this

unsigned int seed;

seed = atoi(argv[1]);

and simply pass it to initPuzzle().

For extra marks use strtol() instead of atoi and do error checking.
Report
Re: Passing command line argument to function Posted by Malcolm_McLean on 27 Mar 2009 at 11:09 AM

User calls your program with an argument, eg

puzzle 1234

you extract the seed like this

unsigned int seed;

seed = atoi(argv[1]);

and simply pass it to initPuzzle().

For extra marks use strtol() instead of atoi and do error checking.
Report
Re: Passing command line argument to function Posted by Malcolm_McLean on 27 Mar 2009 at 11:09 AM

User calls your program with an argument, eg

puzzle 1234

you extract the seed like this

unsigned int seed;

seed = atoi(argv[1]);

and simply pass it to initPuzzle().

For extra marks use strtol() instead of atoi and do error checking.
Report
Re: Passing command line argument to function Posted by delifion on 28 Mar 2009 at 3:19 AM
Hi,

Thanks for the help.

I did something like this:

/*****************************************************************************\
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 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;
	}
}
void movePuzzle(int puzzle[][COLS], char dir, int rowcol)
{
}


but when i tried to print the array, it's printing something like
123
456

it suppose to scramble the order of number, but it doesn't.

any idea what's wrong? do I need pointer in here?
Report
Re: Passing command line argument to function Posted by Malcolm_McLean on 28 Mar 2009 at 6:43 AM
In the code you've posted movePuzzle() is an empty stub function. So of course it doesn't make any changes to the puzzle.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.