943,534 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 704
  • C++ RSS
Nov 24th, 2008
0

problem in outstream

Expand Post »
hi everyone..
i have a small problem with the below code..
when i execute the program, the output file doesnt print me the unsortedList.ReturnLastItem().Print();
it gives me the following output:
the last item in the unsortedList is
how can i make it print me the last item?
here is the code:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using std::cin;
  3. using std::cout;
  4. using std::endl;
  5. #include <cstdlib>
  6. #include"ItemType.h"
  7. #include <fstream>
  8. using std::ifstream;
  9. using std::ofstream;
  10. class UnsortedListType
  11. {
  12. public:
  13. UnsortedListType();
  14. int LengthIs() const;
  15. void Push(ItemType item);
  16. void ResetList();
  17. void GetNextItem(ItemType& item);
  18. int GreaterThanItem(ItemType givenItem);
  19. ItemType ReturnLastItem();
  20.  
  21. private:
  22. int length;
  23. ItemType info[MAX_ITEMS];
  24. int currentPosition;
  25. };
  26.  
  27. UnsortedListType::UnsortedListType()
  28. {
  29. length=0;
  30. }
  31.  
  32.  
  33. int UnsortedListType::LengthIs() const
  34. {
  35. return length;
  36. }
  37. void UnsortedListType::Push(ItemType item)
  38. {
  39. info[length]= item;
  40. length++;
  41. }
  42. void UnsortedListType::ResetList()
  43. {
  44. currentPosition = -1;
  45. }
  46. void UnsortedListType::GetNextItem(ItemType& item)
  47. {
  48. currentPosition++;
  49. item = info[currentPosition];
  50. }
  51. void PrintUnsortedList(UnsortedListType unsortedList)
  52. {
  53. int length;
  54. ItemType item;
  55. unsortedList.ResetList();
  56. length = unsortedList.LengthIs();
  57. for( int i = 1; i <= length; i++)
  58. {
  59. unsortedList.GetNextItem(item);
  60. item.Print();
  61. }
  62. cout<<endl;
  63. }
  64. int UnsortedListType::GreaterThanItem(ItemType givenItem)
  65. {
  66. int counter=0;
  67. int location=0;
  68. while (location<length)
  69. {
  70. if (givenItem.ComparedTo(info[location])==LESS)
  71. {
  72. counter++;
  73. location++;
  74. }
  75. else
  76. location++;
  77. } return counter;
  78. }
  79. ItemType UnsortedListType::ReturnLastItem()
  80. {
  81. return (info[length-1]);
  82. }
  83. int main()
  84. {
  85. ItemType item;
  86. UnsortedListType unsortedList;
  87. int data;
  88.  
  89. ifstream instream;
  90. ofstream outstream;
  91.  
  92. instream.open("input.txt");
  93. if(instream.fail()){
  94. cout<<"Error opening file\n";
  95. exit(1);
  96. }
  97.  
  98. instream>> data;
  99. item.Initialize(data);
  100. unsortedList.Push(item);
  101. while ( !instream.eof() )
  102. {
  103. instream>> data;
  104. item.Initialize(data);
  105. unsortedList.Push(item);
  106. }
  107. instream.close();
  108.  
  109. PrintUnsortedList(unsortedList);
  110. cout<<"the last item in the unsortedList is ";
  111. unsortedList.ReturnLastItem().Print();
  112. cout<<endl;
  113. cout<<endl;
  114. cout<<"insert a number "<<endl;
  115. cin>>data;
  116. item.Initialize(data);
  117. cout<<"the number of elements greater than the numbered you entered is "<<unsortedList.GreaterThanItem(item)<<endl;
  118. cout<< endl;
  119.  
  120. outstream.open("output.txt");
  121. if (!outstream)
  122. return 0;
  123. outstream<<"the last item in the unsortedList is ";
  124. unsortedList.ReturnLastItem().Print();
  125. outstream<<endl;
  126. outstream<<"the number of elements greater than the numbered you entered is "<<unsortedList.GreaterThanItem(item)<<endl;
  127.  
  128. outstream.close();
  129. return 0;
  130. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
elsa87 is offline Offline
50 posts
since Oct 2008
Nov 24th, 2008
0

Re: problem in outstream

post ItemType.h code.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ALAZHARY is offline Offline
14 posts
since Jul 2006
Nov 24th, 2008
0

Re: problem in outstream

here is the ItemType.h code:
C++ Syntax (Toggle Plain Text)
  1. const int MAX = 20;
  2. enum RelationType {LESS, GREATER, EQUAL};
  3. class ItemType {
  4. public:
  5. ItemType();
  6. RelationType ComparedTo(ItemType) const;
  7. void Print() const;
  8. void Initialize(int number);
  9. private:
  10. int value;
  11. };
  12. ItemType::ItemType()
  13. {
  14. value=0;
  15. }
  16. RelationType ItemType::ComparedTo(ItemType otherItem) const
  17. {
  18. if (value < otherItem.value)
  19. return LESS;
  20. else if (value > otherItem.value)
  21. return GREATER;
  22. else return EQUAL;
  23. }
  24. void ItemType::Initialize(int number)
  25. {
  26. value = number;
  27. }
  28. void ItemType::Print() const
  29. {
  30. cout<<value<<" ";
  31. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
elsa87 is offline Offline
50 posts
since Oct 2008
Nov 24th, 2008
0

Re: problem in outstream

Is it succesfully compile?..
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Nov 24th, 2008
0

Re: problem in outstream

yes it compiles without errors and runs also
but the problem is that is the output file, the last item in the list is not printed there..
how can i print this?
the problem is here
C++ Syntax (Toggle Plain Text)
  1. outstream<<"the last item in the unsortedList is ";
  2. unsortedList.ReturnLastItem().Print();
  3. outstream<<endl;
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
elsa87 is offline Offline
50 posts
since Oct 2008
Nov 24th, 2008
0

Re: problem in outstream

c++ Syntax (Toggle Plain Text)
  1. outstream<<'get last item here';
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Nov 24th, 2008
0

Re: problem in outstream

lol
that's not what i meant..
what u gave me is already there but the value of that last item can't be printed..i just need to understand how to print the value
Last edited by elsa87; Nov 24th, 2008 at 11:57 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
elsa87 is offline Offline
50 posts
since Oct 2008
Nov 24th, 2008
0

Re: problem in outstream

I mean..Add this on ItemType.h
c++ Syntax (Toggle Plain Text)
  1. int ItemType::getval()
  2. {return value;}
then
c++ Syntax (Toggle Plain Text)
  1. outstream<<unsortedList.ReturnLastItem().getval();
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Nov 24th, 2008
0

Re: problem in outstream

Thanks alot cikara21..
glad it's finally solved
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
elsa87 is offline Offline
50 posts
since Oct 2008
Nov 24th, 2008
0

Re: problem in outstream

No problem
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: map of arrays?
Next Thread in C++ Forum Timeline: Problem using Pointer Functions





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


Follow us on Twitter


© 2011 DaniWeb® LLC