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:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <cstdlib>
#include"ItemType.h"
#include <fstream>
using std::ifstream;
using std::ofstream;
class UnsortedListType
{
public:
UnsortedListType();
int LengthIs() const;
void Push(ItemType item);
void ResetList();
void GetNextItem(ItemType& item);
int GreaterThanItem(ItemType givenItem);
ItemType ReturnLastItem();
private:
int length;
ItemType info[MAX_ITEMS];
int currentPosition;
};
UnsortedListType::UnsortedListType()
{
length=0;
}
int UnsortedListType::LengthIs() const
{
return length;
}
void UnsortedListType::Push(ItemType item)
{
info[length]= item;
length++;
}
void UnsortedListType::ResetList()
{
currentPosition = -1;
}
void UnsortedListType::GetNextItem(ItemType& item)
{
currentPosition++;
item = info[currentPosition];
}
void PrintUnsortedList(UnsortedListType unsortedList)
{
int length;
ItemType item;
unsortedList.ResetList();
length = unsortedList.LengthIs();
for( int i = 1; i <= length; i++)
{
unsortedList.GetNextItem(item);
item.Print();
}
cout<<endl;
}
int UnsortedListType::GreaterThanItem(ItemType givenItem)
{
int counter=0;
int location=0;
while (location<length)
{
if (givenItem.ComparedTo(info[location])==LESS)
{
counter++;
location++;
}
else
location++;
} return counter;
}
ItemType UnsortedListType::ReturnLastItem()
{
return (info[length-1]);
}
int main()
{
ItemType item;
UnsortedListType unsortedList;
int data;
ifstream instream;
ofstream outstream;
instream.open("input.txt");
if(instream.fail()){
cout<<"Error opening file\n";
exit(1);
}
instream>> data;
item.Initialize(data);
unsortedList.Push(item);
while ( !instream.eof() )
{
instream>> data;
item.Initialize(data);
unsortedList.Push(item);
}
instream.close();
PrintUnsortedList(unsortedList);
cout<<"the last item in the unsortedList is ";
unsortedList.ReturnLastItem().Print();
cout<<endl;
cout<<endl;
cout<<"insert a number "<<endl;
cin>>data;
item.Initialize(data);
cout<<"the number of elements greater than the numbered you entered is "<<unsortedList.GreaterThanItem(item)<<endl;
cout<< endl;
outstream.open("output.txt");
if (!outstream)
return 0;
outstream<<"the last item in the unsortedList is ";
unsortedList.ReturnLastItem().Print();
outstream<<endl;
outstream<<"the number of elements greater than the numbered you entered is "<<unsortedList.GreaterThanItem(item)<<endl;
outstream.close();
return 0;
}