Linked list wont set the right value /HELP!

//when I run the code, everything works for the first input, but when it comes to the second the addOperator() function sets the value to " " instead ofwhat it should "i" or whatever the input is, and with setProperty(), it sets the property without the first letter. Please help this is the last day for the Google science fair

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void Brain::interact(){	

	char inputBuffer[100];
	char inputBufferCopy[100];
	cin >> inputBuffer;
	for (int counter = 0; counter < 100; counter++){

		inputBufferCopy[counter] = inputBuffer[counter]; 

	}		

	char **parsedInput;
	int stringSize = getTokenCount(inputBufferCopy);
	parsedInput = new char*[stringSize];
	char *tokenCheck;
	tokenCheck = strtok(inputBuffer, ".");

	if(tokenCheck != NULL){
			
		int counter = 1;
		parsedInput[0] = tokenCheck;
	
		while(tokenCheck != NULL){

			tokenCheck = strtok(NULL, ".");
			parsedInput[counter] = tokenCheck;
			counter++;

		}
	
	}

		
	add(parsedInput[0], parsedInput[1], parsedInput[2]);
	//think();

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
int Brain::add(char* id, char *op, char* prop){					//function designed to add a new knowledge object with properties

	if (idExist(id)){										// if the thing does exist

		kNode *currentNode = knowledgeList.firstNode;

		int listLength = knowledgeList.getLength();

		for (int counter = 0; counter < listLength; counter++){

			if (id == currentNode->getId()){

				currentNode->Data.addProperty(prop);
				currentNode->Data.addOperator(op);
				
				std::cout << id << "\n";
				std::cout <<currentNode->Data.operators.firstNode->getData()<<"\n";                
				std::cout << currentNode->Data.properties.firstNode->getData()<< "\n";
				
				return 1;

			}

			currentNode = currentNode->nextNode;

		}

	}
	else{
		
		knowledgeList.add(id);		

		kNode *currentNode = knowledgeList.firstNode;

		std::cout<< currentNode->getId();

		int listLength = knowledgeList.getLength();

		for (int counter = 0; counter < listLength; counter++){

			if (id == currentNode->getId()){

				currentNode->Data.addProperty(prop);
				currentNode->Data.addOperator(op);

				std::cout << id << "\n";
				std::cout <<currentNode->Data.operators.firstNode->getData()<<"\n"; //when it prints out this and the following it prints a blank line and the property without its first character
				std::cout << currentNode->Data.properties.firstNode->getData()<< "\n";
							
				return 1;

			}

			currentNode = currentNode->nextNode;

		}

	}

	return 0;

}
Last edited on
The first thing I notice here is that you're using character arrays. Try looking into strings, they're handy for preventing buffer overruns. You also don't have to muck about with pointer logic (which helps to prevent memory leaks).

(I know that's not what you asked. I'm just not really sure what your code is supposed to do.)
Last edited on
but how would I tokenize string input? with character arrays I can break the input into its three part syntax, how could I do that with a string?
Well, cin automatically tokenizes strings using whitespace as a delimiter. You can use the getline() function if you don't want that behaviour. You can also look at a string character-by-character using the [] operator just as you can with a C-style string. In addition, you can use the iterator.

Again, I'm not entirely sure what your code is trying to do.

EDIT:
My mistake, getline works on a character array, not a string.

EDIT #2:
http://cplusplus.com/reference/string/getline/
Turns out, you can use getline with a string. I knew I wasn't crazy (I'm not completely awake this morning).
Last edited on
that simplifies a lot, thank you ;P
Topic archived. No new replies allowed.