Function Call Not Working

I have a function that I set up in the implementation file that removes any duplicate strings, and everything is working with it when called from the client program. Now, I am trying to make the implementation file so that it does not allow duplicates regardless of what the client programs. So, I took advantage of an add function who whose job is to fill an array as programmed by the client. Inside the add function I placed an if statement that test when two strings are equal; if true then call noDuplicates function. I do not get any errors. However, it does not seem to work either.


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
// Implementation file
template<class ItemType>
void  ArraySet<ItemType>::noDuplicates(std::string items[])
{
	for (int index = 0; index < END; index++)
	{
		std::string& firstItem = items[index];
		for (int nextIndex = index + 1; nextIndex < END; nextIndex++) {

			if (firstItem == items[nextIndex])
				this->remove(items[nextIndex]);
		}
	}
}




 template<class ItemType>
bool ArraySet<ItemType>::add(const ItemType& newEntry)
{
	bool hasRoomToAdd = (itemCount < maxItems);
	if (hasRoomToAdd)
	{
		items[itemCount] = newEntry;
		itemCount++;

		if ((items[itemCount-1]) == items[itemCount]) // Added condition to test for equal strings
			noDuplicates(items);                  // Function call to remove duplicates
	}  
	return hasRoomToAdd;
}  // end add





// CLient file
template<class ItemType>
void setTester(ArraySet<ItemType>& set)
{
	cout << "isEmpty: returns " << set.isEmpty()
		<< "; should be 1 (true)" << endl;
	displaySet(set);
	
	std::string items[] = { "one", "two", "three", "four", "five", "one" };
	cout << "Add 6 items to the set: " << endl;
	for (int i = 0; i < END; i++)
	{
		set.add(items[i]);
	}  // end for

	displaySet(set);  // Still displays two "one"s.
	
}


Last edited on
Is the problem that the function call is inside of a bool function, so the changes aren't being returned?
> Is the problem that the function call is inside of a bool function, so the changes aren't being returned?
Why aren't you just running the code in the debugger to find the answer?

At some point, you NEED to learn how to use a debugger.
Because posting random bits of code out of context will only get you so far.

For example, you put a breakpoint on
items[itemCount] = newEntry;

So when the second "one" gets added, you then figure out
a) why it was added
b) why it wasn't removed
I'm sorry I don't know anything about a breakpoint and how to use a debugger. Do you have any recommendations to learn from?
Start typing into google "using the debugger in "
Then look down the list until you find the name of your IDE.
Then start reading some information.

Practice "debugging" some simple code, or a simple program you've worked on before.
1
2
3
4
5
6
7
8
9
void foo ( int x ) {
  for ( int i = 0 ; i < x ; i++ ) {
    cout << i << endl;
  }
}
int main ( ) {
  int x = 10;
  foo(x);
}

Eg.
Put a breakpoint at the start of foo()
Put a breakpoint on the cout statement.
Put a conditional breakpoint on the cout, for when i == 5
Put a breakpoint on the call to foo() in main, then change the value of x to say 12 before you call foo().
Get a stack trace when you're in the foo function.

Hi Salem. Sorry for getting back to you late. I have done a little research on debugging by following Microsoft's "How to debug for absolute beginners." It gives an example program to debug, tells one where to set breakpoints, what variable to check, how to change it, and how to use the step into command.

I just tried to follow the example that you set up, and I am not sure where we are going with this? I looked up stack trace, and it is basically the location of the error in the program correct? If I change x = 10 to x = 12 , x is also used as the limit in the for loop so there is no error to practice debugging with. Is there an error in what you wanted to present, or am I missing the point of where we are going with this?
Do you bake a cake whilst blindfolded, only to wonder at the charred remains 3 hours later.

The point of debugging is to give you visibility and insight when things are about to go wrong. As opposed to an information-free "your application has stopped working".

> I looked up stack trace, and it is basically the location of the error in the program correct?
Yes.
Suppose you had a function that received a NULL pointer and you were expecting something valid. You could work your way up the stack to find out where that NULL originally came from, and thus figure out how to correct that bit of code.

> If I change x = 10 to x = 12 , x is also used as the limit in the for loop so there is no error to practice debugging with
Again, the point is to demonstrate you CAN change things on the fly.
Suppose x was the result of some calculation that you recognise you got wrong.
You could change x to the right answer, and then carry on to check the rest of the code would work as expected.

> or am I missing the point of where we are going with this?
Yes you are missing the point - badly.

The whole point of using a debugger is that you don't have to keep re-editing the code every time something goes wrong, just to find out why it went wrong. Or worse, just dumping code fragments on a forum for someone else to debug for you.

The whole point of the practice is that you know what the code is going to do just by looking at it, so "debugging" the code would just show you the familiar.

So that when you debug the unfamiliar, you have some sense of being able to associate an effect (say a null pointer) with cause (failing to call new).
I see where are going with all this. I got the same impression with the little bit of research I did on debugging, but with your example I thought that there was something small and specific you wanted me to practice a debug on. Obviously not though, and it was a generalization of how to set breakpoints, conditional breakpoints, follow stack traces, and step into a code one line at a time to examine how variables change.

I wish they went over all this kind of stuff in school as it would save some headache. It seems that they expect us to understand the code well enough to debug it. Unfortunately, that hasn't been the case for me; I take these classes online (with no prior experience) and all I have is the textbook, assignment, and online written lecture, and sometimes we don't even get a lecture. Also, the assignments are a HUGE jump from what is covered in the in the textbook. It is extremely challenging to do these courses without any prior experience or help.

This is my third course in C++. I got A's in the first two but it would take me all week to complete an assignment through trial and error, and I have never had time to review things that I have learned as I am too busy with family obligations, work, and other classes. As a result, now this third class is too hard for me because I haven't retained enough; I still feel like a beginner and that is why I come to the beginners forum. So, I just want you to know that I am trying, and I don't want you to feel that I want to go around with an attitude of, "just dumping code fragments on a forum for someone else to debug for you." Everyone here at this site has been really helpful and I definitely appreciate that.

I will continue to learn as much as I can with the little time that I have. It's gonna be a long slow road for me, but one that is worth while.


Topic archived. No new replies allowed.