I'm having problem with my program here. See, I have an array of char* as data member in Action class. With the help of getFirst() function, I'm returning the value of the first element in the array. But, when I tried to print out this value in the test.cpp, it showed me some weird characters. It doesn't work. Please look at my codes:

--> Action.h

#ifndef ACTION_H
#define ACTION_H

class Action
{
	public:
		inline Action() {}
		inline ~Action() {}
		inline char **getCommand() { return command; }
		inline char *getFirst() { return command[0]; }
		inline char *getSecond() { return command[1]; }
		void askCommand();
	
	private:
		char *command[5];
};

#endif

--> Action.cpp

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

#include "Action.h"

static const int NUM_OF_ARG = 5;

void Action::askCommand()
{
	int i = 0;
	char *res = NULL;
	char temp[200];
	
	cout << "> ";
	
	cin.getline(temp, sizeof(temp), '\n');
	
	res = strtok(temp, " ");
	
	while( res != NULL && i < NUM_OF_ARG )
	{
		command[i] = res;
		res = strtok(NULL, " ");
		i++;
	}
}

--> test.cpp

#include <iostream>

using namespace std;

#include "Action.h"

int main()
{
	Action a;
	
	a.askCommand();
	
	char **string = a.getCommand();
	cout << string[0] << endl;           // this doesn't show me the value
	cout << string[1] << endl;           // this doesn't show me the value too
	cout << a.getFirst() << endl;      // this shows me some weird characters
	cout << a.getSecond();              // this shows me weird characters too

	return 0;
}

Could anybody please tell me what is wrong with the codes? Thanks in advance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.