| | |
problem in outstream
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 50
Reputation:
Solved Threads: 0
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:
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)
#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; }
•
•
Join Date: Oct 2008
Posts: 50
Reputation:
Solved Threads: 0
here is the ItemType.h code:
C++ Syntax (Toggle Plain Text)
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<<" "; }
•
•
Join Date: Oct 2008
Posts: 50
Reputation:
Solved Threads: 0
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
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)
outstream<<"the last item in the unsortedList is "; unsortedList.ReturnLastItem().Print(); outstream<<endl;
I mean..Add this on ItemType.h
then
c++ Syntax (Toggle Plain Text)
int ItemType::getval() {return value;}
c++ Syntax (Toggle Plain Text)
outstream<<unsortedList.ReturnLastItem().getval();
![]() |
Similar Threads
- Chatting Program - Throws IO Exception (Java)
- Menu Problem (C++)
- very basic (Java)
- text file manipulations (Visual Basic 4 / 5 / 6)
- Problem with Client-Server Socket Connection (Java)
- getline function acts extrangly (C++)
- file query (C++)
- Using Vectors and Arrays with fstream (C++)
Other Threads in the C++ Forum
- Previous Thread: map of arrays?
- Next Thread: Problem using Pointer Functions
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





