Help with pointers

This is a discussion on Help with pointers within the C++ Programming forums, part of the General Programming Boards category; Hey, mode code is glitched up. If you "get <item>" it will say it's not there. When the command is ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    83

    Help with pointers

    Hey, mode code is glitched up. If you "get <item>" it will say it's not there. When the command is being tested, it get's altered. I don't know what's doing it. Take a look at my code (VC6). I'm really in desperate need!
    Attached Files Attached Files

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I don't download attachments just to answer a question. Try posting the smallest possible program that exhibits your problem along with the input you're using, the output you expect, and the output you are actually getting. Help us to help you more easily and you will get a higher quality answer, much faster.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    83
    the problem is, I don't know what the problem is. It's a rather large application also... It would be next to impossible to post just the code that's causing the problem.. because I don't know what it is! But heres the smallest code possible that's directly causing it.
    Code:
    				if(commandTest("get", input) || commandTest("take", input))
    		{	
    			input = splitString(input);
    			bool take= false;
    			for(int i=0;i < 5;i++)
    			{
    				if(!area.curArea[player1.curX][player1.curY].itemsIR[i])
    					continue;
    
    				if(commandTest(area.curArea[player1.curX][player1.curY].itemsIR[i]->pName,
    					input, false))
    				{
    					if(area.curArea[player1.curX][player1.curY].itemsIR[i]->canTake == false)
    					{
    						cout << "You can't take that.";
    						take = true;
    						break;
    					}
    					give((*area.curArea[player1.curX][player1.curY].itemsIR[i]));
    					take = true;
    					area.curArea[player1.curX][player1.curY].itemsIR[i] = 0;
    					cout << "You took it.";
    					break;
    				}
    			}
    			if(take == false)
    				cout << "It's not here.";
    			continue;
    		}
    CommandTest() tests to see if two things are the same, basically

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    83
    Right before I call the commandTest, the input will change from "blue" to "blue~@" and it throws everything off. And then, it will change to something totally bogus like "|p4" for no apparent reason.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I would wager that the corruption is somewhere in splitString (most likely), or in commandTest. If it's even caused here.

    >It would be next to impossible to post just the code that's causing the problem.
    That would be the whole point of writing a smaller application that exhibits the problem. You isolate it more easily by removing the fluff. Most of the time you can solve your own problems that way.
    My best code is written with the delete key.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Post the splitString function. I'm betting that is where the problem lies. From the looks of it, commandTest logically shouldn't change anything, so I would assume (hope) that if this is the case, your strings are passed in some const manner (e.g. by const reference).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    83
    Code:
    char* splitString(char string[])
    {
    	const int len = sizeof(string)/sizeof(string[0]) + 1;
    	char buffer[len] = {0};
    	int i = 0;
    	for(;string[i] != ' '; i++);
    	i++;
    	int j = 0;
    	for(;string[i] != 0;j++)
    	{
    		if(string[i] > 40 && string[i] < 91)
    		{
    			buffer[j] = string[i]  + 32;
    			continue;
    		}
    		buffer[j] = string[i];
    		i++;
    	}
    	buffer[++j] = '\0';
    	string = buffer;
    	return string;
    }
    and command test....
    Code:
    int commandTest(char commandName[], const char* const input, int i, bool stopSpace)
    {
    	int j = 0;	// CommandName counter
    
    	// If its empty, return 0
    	if(input[0] == '\0')
    		return 0;
    
    	// If there are spaces, skip them until you get to a word
    	for(;;)
    	{
    		if(input[i] == ' ')
    			i++;
    		else
    			break;
    	}
    	
    	// The test
    	for(;;i++,j++)
    	{	
    		// Is is the end or a space?
    		if(input[i] == '\0')
    			// Its the same
    			return 1;
    		if(input[i] == ' ' && stopSpace)
    			return 1;
    		// Is it the same letter?
    		if(input[i] == commandName[j])
    			// Keep going
    			continue;
    		// Is it different?
    		if(input[i] != commandName[j]){
    			// Its not it
    			return 0;
    		}
    	}
    }

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    83
    I noticed where the corruption takes place. When I pass down input to commandTest, before any code in the commanTest takes place, it changes "blue" to "blue- . @". And next, the first line of code (int j = 0 will make it 3 weird sybols. Something like a d with a line, a tongue and a bar. This is really weird.

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    83
    The other uses of commandTest work fine. Equip, dequip, buy, sell. Werd.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > const int len = sizeof(string)/sizeof(string[0]) + 1;

    This should be:
    const int len = strlen(string) + 1;

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > buffer[++j] = '\0';

    And here:
    buffer[j] = '\0';

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > string = buffer;

    And this:
    strcpy( string,buffer );

    Or alternately you could make buffer static.

  13. #13
    Registered User
    Join Date
    Oct 2003
    Posts
    83
    I did all that; and the fact that the buffer wasnt static was one of the problems.. But the other problem... Now, when I pass down the itemName to commandTest, it will pass down a 0, even though the value is true.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    int commandTest(char commandName[], const char* const input, int i, bool stopSpace)
    {
    	int j = 0;	// CommandName counter
    
    	// If its empty, return 0
    	if(input[0] == '\0')
    		return 0;
    
    	// If there are spaces, skip them until you get to a word
    	for(;;)
    	{
    		if(input[i] == ' ')
    			i++;
    		else
    			break;
    	}
    	
    	// The test
    	for(;;i++,j++)
    	{	
    		// Is is the end or a space?
    		if(commandName[j] == '\0')
    			// Its the same
    			return 1;
    		if(input[i] == ' ' && stopSpace)
    			return 1;
    		if(input[i] != commandName[j]){
    			// Its not it
    			return 0;
    		}
    	}
    }

  15. #15
    Registered User
    Join Date
    Oct 2003
    Posts
    83
    Don't worry about it now, I took code from an old backup and it works now. Thanks

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 11:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

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