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;
}

post ItemType.h code.

here is the ItemType.h code:

const int MAX = 20;
enum RelationType {LESS, GREATER, EQUAL};
class ItemType {
public:
ItemType();
RelationType ComparedTo(ItemType) const;
void Print() const;
void Initialize(int number);
private:
int value;
};
ItemType::ItemType()
{
value=0;
}
RelationType ItemType::ComparedTo(ItemType otherItem) const
{
if (value < otherItem.value)
return LESS;
else if (value > otherItem.value)
return GREATER;
else return EQUAL;
}
void ItemType::Initialize(int number)
{
value = number;
}
void ItemType::Print() const
{
cout<<value<<" ";
}

Is it succesfully compile?..

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

outstream<<"the last item in the unsortedList is ";
	unsortedList.ReturnLastItem().Print();
	outstream<<endl;
outstream<<'get last item here';

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

I mean..Add this on ItemType.h

int ItemType::getval()
{return value;}

then

outstream<<unsortedList.ReturnLastItem().getval();

Thanks alot cikara21..
glad it's finally solved

No problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.