954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

problem in outstream

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;
}
elsa87
Junior Poster in Training
50 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

post ItemType.h code.

ALAZHARY
Newbie Poster
14 posts since Jul 2006
Reputation Points: 10
Solved Threads: 1
 

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<<" ";
}
elsa87
Junior Poster in Training
50 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

Is it succesfully compile?..

cikara21
Posting Whiz
340 posts since Jul 2008
Reputation Points: 47
Solved Threads: 69
 

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;
elsa87
Junior Poster in Training
50 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 
outstream<<'get last item here';
cikara21
Posting Whiz
340 posts since Jul 2008
Reputation Points: 47
Solved Threads: 69
 

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

elsa87
Junior Poster in Training
50 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

I mean..Add this on ItemType.h

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

then

outstream<<unsortedList.ReturnLastItem().getval();
cikara21
Posting Whiz
340 posts since Jul 2008
Reputation Points: 47
Solved Threads: 69
 

Thanks alot cikara21..
glad it's finally solved

elsa87
Junior Poster in Training
50 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

No problem

cikara21
Posting Whiz
340 posts since Jul 2008
Reputation Points: 47
Solved Threads: 69
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You