Unsorted List ADT Client Code Help

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2005
Posts: 15
Reputation: oRg is an unknown quantity at this point 
Solved Threads: 1
oRg oRg is offline Offline
Newbie Poster

Unsorted List ADT Client Code Help

 
1
  #1
Sep 28th, 2006
Hello,

I'm a college student taking a programming class and I'm trying to write a program for this class. I've done most of the work already I just need some hints or a push in the right direction as far as my client-code is concerned. Here's the code for the various .CXX and .H files. Any help is appreciated. I'm not asking for a solution just some hints.

ItemType.h
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. const int MAX_ITEMS = 10;
  8. enum RelationType {LESS, EQUAL, GREATER};
  9.  
  10. class ItemType
  11. {
  12. public:
  13. RelationType ComparedTo(ItemType) const;
  14. void GetItemFromFile(ifstream&);
  15. void WriteItemToFile(ofstream&) const;
  16. void WriteInvalidItemToFile(ofstream&) const;
  17. bool invalidItem(ItemType) const;
  18.  
  19. private:
  20. int id;
  21. float gpa;
  22. char major[2];
  23. };

ItemType.cxx
  1. #include "ItemType.h"
  2.  
  3. void ItemType::GetItemFromFile(ifstream& inFile)
  4. // PURPOSE:
  5. // INPUT:
  6. // PRE:
  7. // OUTPUT:
  8. // POST:
  9. // NOTE:
  10. {
  11. inFile >> id >> gpa >> major[0] >> major[1];
  12. }
  13.  
  14. void ItemType::WriteItemToFile(ofstream& outFile) const
  15. // PURPOSE:
  16. // INPUT:
  17. // PRE:
  18. // OUTPUT:
  19. // POST:
  20. // NOTE:
  21. {
  22. outFile << id << setw(20) << gpa << setw(20) << major[0] << major[1];
  23. }
  24.  
  25. bool ItemType::invalidItem(ItemType item) const
  26. // PURPOSE:
  27. // INPUT:
  28. // PRE:
  29. // OUTPUT:
  30. // POST:
  31. // NOTE:
  32. {
  33. return(item.id >= 111 && item.id <= 999 &&
  34. item.gpa >= 0.0 && gpa <= 4.0 &&
  35. item.major[0] == 'C' || item.major[0] == 'I' &&
  36. major[1] == 'S');
  37. }
  38.  
  39. void ItemType::WriteInvalidItemToFile(ofstream& outFile) const
  40. // PURPOSE:
  41. // INPUT:
  42. // PRE:
  43. // OUTPUT:
  44. // POST:
  45. // NOTE:
  46. {
  47. outFile << id << " " << gpa << " " << major[0] << major[1];
  48. }
  49.  
  50. RelationType ItemType::ComparedTo(ItemType otherItem) const
  51. // PURPOSE:
  52. // INPUT:
  53. // PRE:
  54. // OUTPUT:
  55. // POST:
  56. // NOTE:
  57. {
  58. if(id < otherItem.id)
  59. return LESS;
  60. else if(id > otherItem.id)
  61. return GREATER;
  62. else return EQUAL;
  63. }

UnsortedType.h
  1. #include "ItemType.h"
  2.  
  3. class UnsortedType
  4. {
  5. public:
  6. UnsortedType();
  7. void MakeEmpty();
  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 GetNextItem(ItemType& item);
  15.  
  16. private:
  17. int length;
  18. ItemType info[MAX_ITEMS];
  19. int currentPos;
  20. } ;

UnsortedType.cxx
  1. #include "UnsortedType.h"
  2.  
  3. UnsortedType::UnsortedType()
  4. {
  5. length = 0;
  6. }
  7.  
  8. void UnsortedType::MakeEmpty ( )
  9. // Pre: None.
  10. // Post: List is empty.
  11. {
  12. length = 0 ;
  13. }
  14.  
  15. bool UnsortedType::IsFull () const
  16. // Pre: List has been initialized.
  17. // Post: Function value == (list is full).
  18. {
  19. return (length == MAX_ITEMS);
  20. }
  21.  
  22. int UnsortedType::LengthIs() const
  23. // Pre: List has been inititalized.
  24. // Post: Function value == (number of elements in list).
  25. {
  26. return length ;
  27. }
  28.  
  29. void UnsortedType::RetrieveItem(ItemType& item, bool& found)
  30. // Pre: Key member of item is initialized.
  31. // Post: If found, items key matches an elements key in the list and a copy
  32. // of that element has been stored in item; otherwise, item is unchanged.
  33. { bool moreToSearch;
  34. int location = 0;
  35.  
  36. found = false;
  37. moreToSearch = (location < length);
  38. while (moreToSearch && !found)
  39. { switch (item.ComparedTo(info[location]))
  40. { case LESS :
  41. case GREATER : location++;
  42. moreToSearch = (location < length);
  43. break;
  44. case EQUAL : found = true;
  45. item = info[location] ;
  46. case EQUAL : found = true;
  47. item = info[location] ;
  48. break ;
  49. }
  50. }
  51. }
  52.  
  53. void UnsortedType::InsertItem(ItemType item)
  54. // Pre: List has been initialized. List is not full. item is not in list.
  55. // Post: item is in the list.
  56. {
  57. info[length] = item ;
  58. length++ ;
  59. }
  60.  
  61.  
  62. void UnsortedType::DeleteItem(ItemType item)
  63. // Pre: items key has been inititalized.
  64. // An element in the list has a key that matches items.
  65. // Post: No element in the list has a key that matches items.
  66. {
  67. int location = 0;
  68.  
  69. while (item.ComparedTo(info[location]) != EQUAL)
  70. location++;
  71.  
  72. // move last element into position where item was located
  73.  
  74. info[location] = info[length - 1] ;
  75. length-- ;
  76. }
  77.  
  78.  
  79. void UnsortedType::ResetList()
  80. // Pre: List has been inititalized.
  81. // Post: Current position is prior to first element in list.
  82. {
  83. currentPos = -1;
  84. }
  85.  
  86. void UnsortedType::GetNextItem(ItemType& item)
  87. // Pre: List has been initialized. Current position is defined.
  88. // Element at current position is not last in list.
  89. // Post: Current position is updated to next position.
  90. // item is a copy of element at current position.
  91. {
  92. currentPos++;
  93. item = info[currentPos];
  94. }

runList.cxx (This is my driver/client-code)
  1. #include "UnsortedType.h"
  2.  
  3. int main()
  4. {
  5. ifstream inFile;
  6. ofstream outFile;
  7. char listOperation;
  8. int length = 0;
  9.  
  10. inFile.open("in.data");
  11. outFile.open("out.data");
  12. if(inFile.fail() || outFile.fail())
  13. {
  14. cout << "Input or output file opening failed" << endl;
  15. }
  16.  
  17. UnsortedType rdList;
  18. ItemType item;
  19.  
  20. outFile << "<~~~~~~~ Grade Report ~~~~~~~>" << endl;
  21.  
  22. rdList.MakeEmpty();
  23. inFile >> listOperation;
  24.  
  25. while(inFile)
  26. {
  27. item.GetItemFromFile(inFile);
  28. if(listOperation == 'A')
  29. rdList.InsertItem(item);
  30. else if(listOperation == 'D')
  31. rdList.DeleteItem(item);
  32. inFile >> listOperation;
  33. }
  34.  
  35. rdList.ResetList();
  36. length = rdList.LengthIs();
  37.  
  38. for(int count = 1; count <= length; count++)
  39. {
  40. rdList.GetNextItem(item);
  41. if(!item.invalidItem(item))
  42. {
  43. item.WriteInvalidItemToFile(outFile);
  44. outFile << " **** invalid exam score" << endl << endl;
  45. rdList.DeleteItem(item);
  46. }
  47. }
  48.  
  49. rdList.ResetList();
  50. length = rdList.LengthIs();
  51.  
  52. outFile << "STUDENT ID" << setw(20) << "GPA" << setw(20) << "MAJOR" << endl;
  53.  
  54. for(int count = 1; count <= length; count ++)
  55. {
  56. rdList.GetNextItem(item);
  57. item.WriteItemToFile(outFile);
  58. outFile << endl;
  59. }
  60.  
  61. outFile << "> > > end < < <" << endl;
  62.  
  63. return 0;
  64. }

Here is a sample of my input
  1. A 444 3.33 CS
  2. A 777 2.75 IS
  3. A 1000 3.21 CS
  4. A 222 2.88 IS
  5. D 222
  6. A 666 3.54 IS
  7. A 999 3.25 CS
Here is a sample of my output
  1. <~~~~~~~ Grade Report ~~~~~~~>
  2. 1000 3.21 CS **** invalid exam score
  3.  
  4. STUDENT ID GPA MAJOR
  5. 444 3.33 CS
  6. 777 2.75 IS
  7. > > > end < < <

Here is what I would like my output to be
  1. <~~~~~~~ Grade Report ~~~~~~~>
  2. 1000 3.21 CS **** invalid exam score
  3.  
  4. STUDENT ID GPA MAJOR
  5. 444 3.33 CS
  6. 777 2.75 IS
  7. 666 3.54 IS
  8. 999 3.25 CS
  9. > > > end < < <

In the Input file the char 'A' adds the item to the list while the char 'D' deletes the item from the list.

Forgive the unfinished and sloppy code...still working on cleanliness

The problem description is:
Create an unsorted list for student id (111-999), GPA (0.0-4.0), and major (two chars either "CS" or "IS") as ItemType of the Unsorted List. Do not add invalid data to the list, for invalid data print the record with a message at the beginning of output file.
Again, I'm not looking for an answer just some hints. I'm just trying to figure out where my logic is wrong.

Thanks in advance.

[edit]Oh, I forgot to add that the UnsortedType.h and UnsortedType.cxx source was given to me and is perfect and shouldn't have much of an effect on my client-code. I just posted it so you can see the whole picture. However the problem just basically wants me to write my own ItemType.cxx, ItemType.h, and runList.cxx code.[/edit]
Last edited by oRg; Sep 28th, 2006 at 1:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Unsorted List ADT Client Code Help

 
0
  #2
Sep 28th, 2006
First thing, get rid of those floats, it's a pain in the backside. Use double instead. Also forget char arrays, you're in c++, so might as well use std::strings, there's less chance of overflow.

Start putting a load of couts in your header files to aid debugging. Or if you're lucky some mo fo will come along and do it for you.

  1. bool UnsortedType::IsFull () const
  2. // Pre: List has been initialized.
  3. // Post: Function value == (list is full).
  4. {
  5. return (length == MAX_ITEMS);
  6. }

Bool, would have to return true or false, so what's going on up there? Also check your file handling is without bugs. (not checked it myself...but just saying)
Last edited by iamthwee; Sep 28th, 2006 at 2:41 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 15
Reputation: oRg is an unknown quantity at this point 
Solved Threads: 1
oRg oRg is offline Offline
Newbie Poster

Re: Unsorted List ADT Client Code Help

 
1
  #3
Sep 28th, 2006
Thanks for your reply. I do appreciate it.

Anyways, like I said I'm a college student and my prof. expects it done a certain way. Believe me I would rather use doubles and strings but this is how he wants it done.

I will be sure to add the couts to see where things are going awry.

Like I said, everything associated with the UnsortedType class (it's member functions and its data) are what he gave us to use. We can't modify it in anyway. I couldn't tell you why but that is what he wants. The only thing that I can modify and change are ItemType.cxx, ItemType.h, and runList.cxx. I'm pretty sure the ItemType files are right. I just think that there's a problem in my client-code (runList.cxx).
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Unsorted List ADT Client Code Help

 
1
  #4
Sep 29th, 2006
My immediate guess would be the line:

  1. D 222
Is causing your program to exit prematurely. Probably because this:

void ItemType::GetItemFromFile(ifstream& inFile)
// PURPOSE:
// INPUT:
// PRE:
// OUTPUT:
// POST:
// NOTE:
{
        inFile >> id >> gpa >> major[0] >> major[1];
}

...is expecting another two more variables, which it will still try an attempt to read.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 15
Reputation: oRg is an unknown quantity at this point 
Solved Threads: 1
oRg oRg is offline Offline
Newbie Poster

Re: Unsorted List ADT Client Code Help

 
1
  #5
Sep 29th, 2006
Yeah, thats actually right. I added values for the gpa and major variables into my "in.data" file and it worked perfectly. So what I did is I made a new member function. Here's the new member function.
  1. void ItemType::GetDeleteItemFromFile(ifstream& inFile)
  2. // PURPOSE:
  3. // INPUT:
  4. // PRE:
  5. // OUTPUT:
  6. // POST:
  7. // NOTE:
  8. {
  9. inFile >> id;
  10. }

I also modified my client-code just a little. Here's the modified code.
  1. while(inFile)
  2. {
  3. if(listOperation == 'A')
  4. {
  5. item.GetItemFromFile(inFile);
  6. rdList.InsertItem(item);
  7. }
  8. else if(listOperation == 'D')
  9. {
  10. item.GetDeleteItemFromFile(inFile);
  11. rdList.DeleteItem(item);
  12. }
  13. inFile >> listOperation;
  14. }
However, even in this setup if my "in.data" is not modified it still messes up. So I'm just looking for a way that after reading 'D', then the id number it should go to the next line in the data file for the next read cycle.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Unsorted List ADT Client Code Help

 
1
  #6
Sep 30th, 2006
Here's how I see it. Your class infrastructure:-
  1. private:
  2. int id;
  3. float gpa;
  4. char major[2];

Will ONLY work if your lines contain those exact three variables. Obviously, your
teacher has given you the line "D 222" to test your little programming skills.

The only way to overcome this, is make sure that any lines that do not contain these three variables, are discarded straight away. I.e. they don't even make it through to your class infrastructure.

What you ideally need to be doing, as well, is to be reading your file in LINE by LINE. You are NOT doing this at the momentf.

The worst problem with your code is how you have ASSUMED each line will always follow this particular structure:-

  1. inFile >> id >> gpa >> major[0] >> major[1];
Bad, let's say the first word ain't an integer, as you have stated the id to be, what do you think's gonna happen? It's gonna crash.

Thus, I would read the file each line at a time.


Then, each line would be represented by a string of characters. This way it can't possibly crash. Then I would split the line into parts using the whitespace as a delimiter.

If the parts, match the description:-
  1. id (int) >> gpa (float) >> major(char[])
And only three variables are found, then I would pass it into the class. Job done.

If you need help how to read in lines, and splitting each line into parts using the whitespace as a delimiter - ask.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,651
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Unsorted List ADT Client Code Help

 
0
  #7
Sep 30th, 2006
Try using fgets () for reading the contents line by line and try using strtok () for tokenising the c style string.

You can find the function prototypes and short examples here:

http://www.cplusplus.com/ref/#libs
http://www.cplusplus.com/ref/#libs

Hope it helped,bye.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 15
Reputation: oRg is an unknown quantity at this point 
Solved Threads: 1
oRg oRg is offline Offline
Newbie Poster

Re: Unsorted List ADT Client Code Help

 
0
  #8
Oct 1st, 2006
Thanks again for your replies.

As I stated above I have fixed the problem I was originally having. I just added an extra emember function which would only read the id and nothing else.

The only thing I'm struggling with now is getting the output in the correct order.

Here's a sample of my current output:
  1. <~~~~~~~ Grade Report ~~~~~~~>
  2. 1000 3.21 CS **** invalid exam score
  3.  
  4. STUDENT ID GPA MAJOR
  5. 444 3.33 CS
  6. 777 2.75 IS
  7. 999 3.25 CS
  8. 666 3.54 IS
  9.  
  10. > > > end < < <
The student id entry 666 should come right after the id entry 777 and entry 999 should be last. Unfortunately as you can see this is not the case. Again I believe that it may be solved in my client-code.
Last edited by oRg; Oct 1st, 2006 at 8:41 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 15
Reputation: oRg is an unknown quantity at this point 
Solved Threads: 1
oRg oRg is offline Offline
Newbie Poster

Re: Unsorted List ADT Client Code Help

 
0
  #9
Oct 1st, 2006
Ok, I have figured out my problem and I'm pretty much finished except I have to add documentation to the source code. Thanks for your help guys.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 2780 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC