Hello. This is my first posting so I will try to be as detailed and clear as possible. I am running into a problem with a method returning an incorrect value and I can't quite seem to figure out why.

I'm building a Rolodex which is really just an object with a private member variable of type list (STL list container) that accepts Card objects. Like a real Rolodex, I have to be able to "flip" through my Rolodex object. A new card added to the Rolodex becomes the "current card". As I "flip" through the Rolodex (starting from the current card), the card that's next in the list becomes the current card.

To figure out what card is the current card, I have a private member integer variable called Id that is a part of the Card object. A global variable called globalId (initialized to 0) is incremented as a new card is created and Card's Id takes on the value of globalId. Each Card Id in the Rolodex is unique identifier - at least, that is what I'm hoping to achieve. When adding a Card object to the Rolodex, the Card's Id is assigned to the Rolodex private member variable currentCardId. currentCardId keeps track of the Id number of the "current" card. currentCardId is subject change in other functions - like Rolodex::flip() for example.

I create a menu asking the user what he or she wants to do. If a user wants to add a card, I then prompt the user for last name, first name, etc etc. I then create a Card object using the supplied information and pass that Card object to my Rolodex::add(Card) method. The adding of Card objects via Rolodex::add(Card) works just fine. I then sort the list in ascending order and that works fine as well. I can iterate through the list and display the contents of the individual Card objects too but there's one private variable of the Card that isn't being displayed properly and I see no reason why since other private variables display just fine.

So, there are two issues that I'm running into that's preventing me from proceeding further with my assignment. The id being returned by Card::getId() { return id; } isn't working properly in some cases. This leads me to the inability to obtain the current card based on that unique identifier (Card private variable Id).

This function iterates through my sorted list variable myList and prints card information via void Card::show(). Card::show() is a void function that prints private member variables of the Card object to the screen. Card::getId() is a function of return type int but it isn't displayed properly. I get a negative 9 digit number instead of 1 for the first Card, the same negative 9 digit number for any other cards that are added later instead of 2, 3, 4...etc.

void Rolodex::show() {
	for ( iter=myList.begin(); iter != myList.end(); ++iter) {
			(*iter).show();
			cout << "id = " << (*iter).getId() << endl;
	}
}
void Card::show() {
	cout << this->lastName << ", " << this->firstName << endl;
	cout << this->job << endl;
	cout << this->street << endl;
	cout << this->town << endl;
	cout << this->phone << endl ;
	cout << "Card::show id = " << this->id << endl;
}

The id returned by this->id is displayed on the screen as a negative 9 digit integer instead of 1 for the first Card created. Other information is displayed just fine. The cout << "Cardblah blah is there to help me figure out where I am in the code when I'm stepping through it.

printCurrentCard calls getCurrentCard (getCurrentCard returns a Card object) and assigns the return value to a temporary Card object. From there, I call tmpCard.show() to display the card details. This doesn't work properly because what is being returned by getCurrentCard is likely not correct.

void Rolodex::printCurrentCard() {
	Card tmpCard;
	// cout << "In printCurrentCard() currentCardId = " << currentCardId << endl; // currentCardId returned properly
	tmpCard = getCurrentCard();
	cout << "printCurrentCard() -> Current Card Id = " << tmpCard.getId() << endl;
	tmpCard.show();
}

I was hoping to iterate through the sorted list and use the unique Id identifier to find the list object I want to return.

Card Rolodex::getCurrentCard() {
	for ( iter=myList.begin(); iter != myList.end(); ++iter) {
		// cout << "In for currentCardId = " << currentCardId << endl; // currentCardId returned properly
		cout << "In for loop iter.getId() = " << iter->getId() << endl;
		if ( iter->getId() == currentCardId ) {
			cout << "In If currentCardId = " << currentCardId << endl;
			cout << "In If iter.getId() = " << iter->getId() << endl;
			currentCard = (*iter);
		}
	}

	cout << "getCurrentCard() -> Current Card Id = " << currentCard.getId() << endl;
	
	return currentCard;
}

If there is any more information that would be required, please let me know.

Below is runtime output. Option 1 calls Rolodex::show(). Option 2 calls Rolodex::printCurrentCard(). :

"1" to list all contents of rolodex
"2" to print card at current position of the rolodex
"3" to flip to and display next card on the screen
"4" to find a card in the rolodex and display card contents on the screen
"5" to add a new card to the rolodex
"6" to remove a card from the rolodex
"7" to modify an existing card in the rolodex
"8" to exit program

Enter selection : 5
Enter last name : Doe
Enter first name : Jane
Enter job : Blah
Enter street : blah
Enter town : blah
Enter phone : 222-5555
Rolodex::add -> arg.getId = 1
Rolodex::add -> Current Card Id = 1
"1" to list all contents of rolodex
"2" to print card at current position of the rolodex
"3" to flip to and display next card on the screen
"4" to find a card in the rolodex and display card contents on the screen
"5" to add a new card to the rolodex
"6" to remove a card from the rolodex
"7" to modify an existing card in the rolodex
"8" to exit program

Enter selection : 1
Doe, Jane
Blah
blah
blah
222-5555
[B]Card::show id = -842150451[/B]
[B]id = -842150451[/B]
"1" to list all contents of rolodex
"2" to print card at current position of the rolodex
"3" to flip to and display next card on the screen
"4" to find a card in the rolodex and display card contents on the screen
"5" to add a new card to the rolodex
"6" to remove a card from the rolodex
"7" to modify an existing card in the rolodex
"8" to exit program

Enter selection : 5
Enter last name : blah
Enter first name : blah
Enter job : blah
Enter street : blah
Enter town : blah
Enter phone : 222-6666
Rolodex::add -> arg.getId = 2
Rolodex::add -> Current Card Id = 2
"1" to list all contents of rolodex
"2" to print card at current position of the rolodex
"3" to flip to and display next card on the screen
"4" to find a card in the rolodex and display card contents on the screen
"5" to add a new card to the rolodex
"6" to remove a card from the rolodex
"7" to modify an existing card in the rolodex
"8" to exit program

Enter selection : 1
Doe, Jane
Blah
blah
blah
222-5555
Card::show id = -842150451
id = -842150451
blah, blah
blah
blah
blah
222-6666
Card::show id = -842150451
id = -842150451
"1" to list all contents of rolodex
"2" to print card at current position of the rolodex
"3" to flip to and display next card on the screen
"4" to find a card in the rolodex and display card contents on the screen
"5" to add a new card to the rolodex
"6" to remove a card from the rolodex
"7" to modify an existing card in the rolodex
"8" to exit program

Enter selection : 2
In for loop iter.getId() = -842150451
In for loop iter.getId() = -842150451
getCurrentCard() -> Current Card Id = -858993460
printCurrentCard() -> Current Card Id = -858993460
,




Card::show id = -858993460
"1" to list all contents of rolodex
"2" to print card at current position of the rolodex
"3" to flip to and display next card on the screen
"4" to find a card in the rolodex and display card contents on the screen
"5" to add a new card to the rolodex
"6" to remove a card from the rolodex
"7" to modify an existing card in the rolodex
"8" to exit program

Enter selection :

There are a couple of other methods that will rely on my ability to find the current Card but until I get past this/these issues, or understand what it is that I'm doing incorrectly, I'm kind of stuck.

Any help/tips/suggestions/pointers would be greatly appreciated.
Thank you.

Edit: Just read one of the stickies at the top of the page about posting things related to homework. I have tried numerous methods to get the correct Id to return but all result in vain (from changing the Id from private to public to protected within the Card Class definition, to returning id in Card::getId instead of this-id). It just doesn't seem to matter once the Card object is in the list. What befuddles me is why the Card string data like firstName, phone, display properly but the Card integer id does not.
Also, I have a feeling that what I'm trying to do with the iterator to get a current card object is probably really messed up. I have cruised the SGI STL documentation website and a bunch of other web resources but all of my attempts to figure out wtf is wrong with my code (besides the fact that I'm writing it) have resulted in massive fail.

Edit: I have been to this site a few times and found a lot of responses in other threads helpful. I hope to be able to give back to this community in the future in some way (by responding to others requests for help by providing sound advice I hope). If I'm not able to contribute in this forum, perhaps I'll be able to contribute in one of the other sub-forums.

Edited 6 Years Ago by bsdbum: n/a

You've included a lot of code that is called and shows that there is an issue, but I'd like to see the function called when option 5 is selected to add a new card. I'd almost bet that the problem is that you're cards id variable is never set so it is returning and displaying an uninitialized value.

You've included a lot of code that is called and shows that there is an issue, but I'd like to see the function called when option 5 is selected to add a new card. I'd almost bet that the problem is that you're cards id variable is never set so it is returning and displaying an uninitialized value.

Sure.
At the start of the program, globalId = 0. This number increments as new Card objects are added to the Rolodex and ensures that all new Cards will have a unique Id identifier - the Rolodex can have duplicate names.

Option 5 calls addCardToRolodex( &Rolodexobject )

void addCardToRolodex( Rolodex* arg ){
	globalId += 1;

	string tempFirst, tempLast, tempJob, tempStreet, tempTown, tempPhone;
	cout << "Enter last name : ";
	getline ( cin, tempLast );
	cout << "Enter first name : ";
	getline ( cin, tempFirst );
	cout << "Enter job : ";
	getline ( cin, tempJob );
	cout << "Enter street : ";
	getline ( cin, tempStreet );
	cout << "Enter town : ";
	getline ( cin, tempTown );
	cout << "Enter phone : ";
	getline ( cin, tempPhone );

	Card tempCard;
	tempCard.setFirstName( tempFirst );
	tempCard.setLastName( tempLast );
	tempCard.setJob( tempJob );
	tempCard.setStreet( tempStreet );
	tempCard.setTown( tempTown );
	tempCard.setPhone( tempPhone );
	tempCard.setId( globalId );  //works

	//cout << " tempCard.getId() = " << tempCard.getId() << endl; //works

	arg->add( tempCard );

}

Rolodex::add( Card )-v

void Rolodex::add(Card arg) {
	myList.push_back( arg );
	currentCardId = arg.getId();
	cout << "Rolodex::add -> arg.getId = " << arg.getId() << endl; // works
	cout << "Rolodex::add -> Current Card Id = " << currentCardId << endl; // works
	myList.sort();
}

It's very gratifying to say that I think I found the problem with my code.
:-)

In my Card default ctor, I added id=0, and modified all (*iter).methods to use iter->method() instead in my other functions. That seemed to move my project along a ton.

Edited 6 Years Ago by bsdbum: n/a

I would need to see your class definitions. On thing you appear to be doing wrong is confusing the cardID with the iterator. You should use the iterator totally separately from the cardID.

I would need to see your class definitions. On thing you appear to be doing wrong is confusing the cardID with the iterator. You should use the iterator totally separately from the cardID.

Well, not really. The problem really stemmed from me not initializing id in the Card class. I figured it out on my own by reconstructing what I wanted to do piece by piece and it works just fine.
I marked this thread solved and have long since moved on. I do appreciate your time and thank you for the attempt. :-)

To future posters offering help, please note that this is solved.

This question has already been answered. Start a new discussion instead.