Having a problem with my code :(

This is a discussion on Having a problem with my code :( within the C++ Programming forums, part of the General Programming Boards category; Hi, this is my first time posting here and was hoping someone could help me. So basically it's a problem ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    3

    Having a problem with my code :(

    Hi, this is my first time posting here and was hoping someone could help me.

    So basically it's a problem with types I think, here is the code:
    Code:
    LPSTR Array[];
    
    BOOL File2Array(LPSTR pArray[], char *szFile)
    {
    	int i = 0;
    	std::ifstream file(szFile);
    	if (file.is_open()) {
    		std::string line;
    		while(std::getline(file, line))
    		{
    			pArray[i] = mysprintf("%s", line.c_str());
    			i++;
    			if(i > 500)
    				return TRUE;
    		}
    	}
    	else {
    		Print("Unable to open file.");
    		return FALSE;
    	}
    	return TRUE;
    }
    
    File2Array(Array, "file.txt");
    In file.txt theres a list of words like
    apple
    orange
    pear
    banana
    etc

    it populates the array fine, and if I do this:
    Code:
    for(int i = 0; i < 10; i++)
    cout << Array[i];
    it prints it prints them all fine, but if i want to do, say, this:

    Code:
    BOOL IsListed(char *szWord)
    {
    	for(int i = 0; _strcmpi(Array[i], "(null)") != 0 && i < 500; i++)
    			if(!_strcmpi(Array[i], szWord))
    				return TRUE;
    	return FALSE;
    }
    char szWord[0x10];
    strcpy(szWord, "apple");
    IsListed(Word);
    I crash..

    This is a dll too, it's a hack for a game.. honestly this is kind of out of my knowledge of c++, but I practically have it working and wrote most of it, so I wish I could get it working..

    Thanks,
    Vivec45
    Last edited by vivec45; 02-05-2011 at 08:37 PM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Is there an entry called "(null)" on the last line of the text file, or are there over 500 lines?

    You can't do this in C++, so what is your real code?:
    Code:
    LPSTR Array[];
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    Quote Originally Posted by iMalc View Post
    Is there an entry called "(null)" on the last line of the text file, or are there over 500 lines?

    You can't do this in C++, so what is your real code?:
    Code:
    LPSTR Array[];
    My mistake, was kind of doing that from memory. And I was trying to put it in format of a console. Oh and I was just trying to fix it when I did, I thought it might be doing some huge loop and freezing. I noticed when it reads an empty line it equates to (null), so I thought I'd just stop reading at that point.

    Code:
    char *mysprintf(CHAR* szFormat, ...)
    {
    	CHAR *szString = new CHAR[8192];
    	::memset(szString, 0, 8192);
    	va_list vaArgs;
    
    	va_start(vaArgs, szFormat);
    	vsprintf(szString, szFormat, vaArgs);
    	va_end(vaArgs);
    
    	return szString;
    	delete[] szString;
    }
    
    BOOL File2Array(LPSTR pArray[], char *szFile)
    {
    	int i = 0;
    	std::ifstream file(szFile);
    	if (file.is_open()) {
    		std::string line;
    		while(std::getline(file, line))
    		{
    			pArray[i] = mysprintf("%s", line.c_str());
    			i++;
    			if(i > 500)
    				return TRUE;
    		}
    	}
    	else {
    		Print("Unable to open file.");
    		return FALSE;
    	}
    	return TRUE;
    }
    
    BOOL IsFriendlisted(char *szPlayer)
    {
    	for(int i = 0; _strcmpi(FriendList[i], "(null)") != 0 && i < 500; i++)
    			if(!_strcmpi(FriendList[i], szPlayer))
    				return TRUE;
    	return FALSE;
    }
    
    #define DEFINEVAR(type, name)		extern type name;
    
    	DEFINEVAR(LPSTR, FriendList[512])
    
    	if(!_strcmpi(Msg[0],"test"))
    	{
    		char *Word;
    		Word = "apple";
    
    		Print("%d", IsSquelchListed(mysprintf("%s", Word), false));
    		return true;
    	}
    Thanks for reply,
    Vivec45
    Last edited by vivec45; 02-06-2011 at 12:03 AM.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    Can anyone help please? :3

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    You seem to have a few problems with arrays and dynamic memory allocation/deallocation. It would make your coding life a lot easier if you'd use std::string instead of character pointers and std::list or std::vector instead of arrays.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem uder LINUX (gcc) and same code work at Windows (DevC++)
    By miroslavgojic in forum C Programming
    Replies: 5
    Last Post: 11-24-2010, 09:05 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 05:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 04:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2003, 11:53 PM

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