Problem with char

This is a discussion on Problem with char within the C++ Programming forums, part of the General Programming Boards category; I'm having problem with my program here. See, I have an array of char* as data member in Action class. ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    15

    Problem with char

    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
    Code:
    #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
    Code:
    #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
    Code:
    #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.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    You've got an array of pointers that you point to local data that goes away.

    If you want to capture the text, you'll need to allocate memory and copy it.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    Could you show me how to allocate memory and copy it? I've tried to allocate memory using:

    Code:
    char *res = new char;
    and copying it into the array using strcpy:
    Code:
    strcpy(command[i], res);
    But, this strcpy statement has made the program encounters a runtime error.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    In lazy mode, I just did this.
    Code:
       while ( res != NULL && i < NUM_OF_ARG )
       {
          command[i] = new char[strlen(res)+1];
          strcpy(command[i], res);
          res = strtok(NULL, " ");
          i++;
       }
    Last edited by Dave_Sinkula; 09-22-2007 at 02:44 PM. Reason: strlen(res)+1
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Pff. That's pretty lazy, Dave. I mean where are all the colours?

    You should consider using std::strings!

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    Well, thanks a lot. I find it working now. Huh, I'm feeling preety stupid now. It must've been very easy to figure out, lol.

    Anyway, thanks. Yeah, using string is much better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  2. char problem
    By ForlornOdium in forum C++ Programming
    Replies: 10
    Last Post: 10-29-2003, 02:39 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM
  4. String Processing Problem
    By muffin in forum C Programming
    Replies: 0
    Last Post: 08-24-2001, 11:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21