943,969 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1373
  • C++ RSS
Feb 12th, 2009
0

input items to a list

Expand Post »
Hey. I'm trying to simply input items into a list, then be able to sort them and do whatever. I've done this before for my previous classes, but we have we have to use class files out of the book and modify them to make this work and I'm having way too much trouble. If somebody could point me in the right direction, it would be much appreciated.

I created the .cpp file. The unsorted.h is from the book. There are a couple of more files, let me know if they are needed.

C++ Syntax (Toggle Plain Text)
  1. // This is the .cpp file.
  2. #include "unsorted.h"
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. UnsortedType list;
  9. ItemType item;
  10. list.MakeEmpty();
  11. item.Initialize();
  12.  
  13. cout << "Enter number (-999 to quit): ";
  14. cin >> item;
  15. list.InsertItem(item);
  16.  
  17. }
C++ Syntax (Toggle Plain Text)
  1. // This is the unsorted.h file.
  2. #include "itemtype.h"
  3.  
  4. class UnsortedType
  5. {
  6. public:
  7. UnsortedType();
  8. bool IsFull() const;
  9. int LengthIs() const;
  10. void RetrieveItem(ItemType& item, bool& found);
  11. void InsertItem(ItemType item);
  12. void DeleteItem(ItemType item);
  13. void ResetList();
  14. void MakeEmpty();
  15. void GetNextItem(ItemType& item);
  16. private:
  17. int length;
  18. ItemType info[MAX_ITEM];
  19. int currentPos;
  20. };
Last edited by sfrider0; Feb 12th, 2009 at 1:55 am.
Similar Threads
Reputation Points: 16
Solved Threads: 0
Junior Poster
sfrider0 is offline Offline
149 posts
since Oct 2008
Feb 12th, 2009
1

Re: input items to a list

Why are you using 'ItemType' for your lists? Is this some sort of generic container-type object? (Presumably it overloads the '>>' operator, since you were using it in main().) If I were designing the UnsortedType class, I would probably use a template so the object type can be specified when a new instance of the class is defined.

It also seems a bit of a waste the way you store and pass objects, if it's possible, try passing by reference so you don't end up with as much overhead on the stack (since you're going to have to copy the object into the internal array anyway).
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 12th, 2009
0

Re: input items to a list

That's exactly what I was thinking. The directions specifically said to put the input data into an "item", then put the item in the list. Right now I keep getting errors when I use the "cin >> item" then try to store that into the list. I just get an ItemType error. What exactly do I have to initialize in the "item.Initialize(????)" to get this to work. This whole method of doing this seems messed up.
Reputation Points: 16
Solved Threads: 0
Junior Poster
sfrider0 is offline Offline
149 posts
since Oct 2008
Feb 13th, 2009
1

Re: input items to a list

>Right now I keep getting errors when I use the "cin >> item"
Well, then clearly you haven't overloaded the '>>' operator for ItemType.

>What exactly do I have to initialize in the "item.Initialize(????)"
>to get this to work.
You've never posted how ItemType was defined, so can't help you there. Either make due with the current ItemType structure (perhaps modify it if you're allowed), or you're going to have to use a different structure to hold your list nodes, such as an integer.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Nov 7th, 2009
0
Re: input items to a list
thanx
Reputation Points: 10
Solved Threads: 0
Newbie Poster
HAZEM8002 is offline Offline
1 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: building sphinx4 helloworld in netbeans - errors
Next Thread in C++ Forum Timeline: C++ Improving my code - semaphores and mutex





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC