problem in outstream

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 50
Reputation: elsa87 is an unknown quantity at this point 
Solved Threads: 0
elsa87 elsa87 is offline Offline
Junior Poster in Training

problem in outstream

 
0
  #1
Nov 24th, 2008
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:
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 14
Reputation: ALAZHARY is an unknown quantity at this point 
Solved Threads: 1
ALAZHARY's Avatar
ALAZHARY ALAZHARY is offline Offline
Newbie Poster

Re: problem in outstream

 
0
  #2
Nov 24th, 2008
post ItemType.h code.
I'd give my right arm to Improve my english
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 50
Reputation: elsa87 is an unknown quantity at this point 
Solved Threads: 0
elsa87 elsa87 is offline Offline
Junior Poster in Training

Re: problem in outstream

 
0
  #3
Nov 24th, 2008
here is the ItemType.h code:
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 338
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 66
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: problem in outstream

 
0
  #4
Nov 24th, 2008
Is it succesfully compile?..
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 50
Reputation: elsa87 is an unknown quantity at this point 
Solved Threads: 0
elsa87 elsa87 is offline Offline
Junior Poster in Training

Re: problem in outstream

 
0
  #5
Nov 24th, 2008
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
  1. outstream<<"the last item in the unsortedList is ";
  2. unsortedList.ReturnLastItem().Print();
  3. outstream<<endl;
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 338
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 66
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: problem in outstream

 
0
  #6
Nov 24th, 2008
  1. outstream<<'get last item here';
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 50
Reputation: elsa87 is an unknown quantity at this point 
Solved Threads: 0
elsa87 elsa87 is offline Offline
Junior Poster in Training

Re: problem in outstream

 
0
  #7
Nov 24th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 338
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 66
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: problem in outstream

 
0
  #8
Nov 24th, 2008
I mean..Add this on ItemType.h
  1. int ItemType::getval()
  2. {return value;}
then
  1. outstream<<unsortedList.ReturnLastItem().getval();
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 50
Reputation: elsa87 is an unknown quantity at this point 
Solved Threads: 0
elsa87 elsa87 is offline Offline
Junior Poster in Training

Re: problem in outstream

 
0
  #9
Nov 24th, 2008
Thanks alot cikara21..
glad it's finally solved
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 338
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 66
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: problem in outstream

 
0
  #10
Nov 24th, 2008
No problem
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC