I have to create a linked list overloading the << and >> operators and output the list, I then have to sort it, but I keep having issues creating the list.
ostream& operator<<(ostream& outFile, bubble& outList)
{
nodeType *current;
current = outList.head_ptr;
while (current != NULL) //outputs the info of each node in list
{
outFile << current->info;
current = current->link;
}
outFile << endl; //creates nwln for each list
return outFile;
}
My issue is when I call the code in my main program the list is empty, I think this is because it is reading the actual EOF and putting it in the list, thus destroying it.
My issue is when I call the code in my main program the list is empty, I think this is because it is reading the actual EOF and putting it in the list, thus destroying it.
Nope. As far as I can see your reading is fine.
Your writing is wrong. You need a blank to separate your numbers on line 11. Like so:
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cctype>
usingnamespace std;
#include "program3_classbubble.cpp"
int main ()
{
ifstream data;
ofstream out;
bubble numberList;
data.open ("bubbleinput.txt");
if (!data)
{
cout << "failure to open bubbleinput.txt" << endl;
return 1;
}
out.open ("bubbleoutput.txt");
if (!out)
{
cout << "failure to open bubbleoutput.txt" << endl;
return 1;
}
// main body program, reads in values in an EOF loop then adds the lists
if (data)
{
data >> numberList; // priming read
//numberList.bubblesort();
}
while (data)
{
out << numberList;
data >> numberList;
//numberList.bubblesort();
}
data.close(); out.close();
system("pause");
return 0;
}
I had to do this before but we did a character read using the get(character) function and converted each individual number to from ASCII to dec, my professor said we didnt need to worry about all of that this time.
This way works but its neot reading in the last number.
void bubble::bubblesort()
{
nodeType *current, *next, *previous;
bool swap;
//loop only if there are elements in list
swap = (head_ptr != NULL);
while (swap)
{
//only continue loop if a swap is made
swap = false;
current = head_ptr;
next = current->link;
previous = NULL;
while (next != NULL)
{
if (current->info > next->info)
{
swap = true;
//swap elements
if (current == head_ptr)
{
nodeType *temp;
head_ptr = next;
temp = next->link;
next->link = current;
current->link = temp;
current = head_ptr;
}
else
{
previous->link = current->link;
current->link = next->link;
next->link = current;
current = next;
}
}
//increment elements
previous = current;
current = current->link;
next = current->link;
}
}
}
So with string it works and with int not? Surprising. It certainly should
This way works but its neot reading in the last number.
Then put line 21 before line 15. insert only if the string is not empty(). It seems it reads the value and sets the eof at the same time (it shouldn't)
To determine surprising behaviour like this you should debug (if you can)
Thats excactly what my dad told me, but we really never touched up on debugging outside of the obvious warnings. We use Bloodshed Dev C++.
I think the problem with the int is it is reading in the EOF and basically inserting NULL into the head_ptr of the list, and inside the constructor for the list if head is NULL it destroys the entire list thinking the whole thing is empty.
Your solution did work, I was getting close to it myself but was forgetting to move the read in to be first.
I think the problem with the int is it is reading in the EOF and basically inserting NULL into the head_ptr of the list, and inside the constructor for the list if head is NULL it destroys the entire list thinking the whole thing is empty.
I don't think so (because the string/getline version works). It rather looks like that there's a non numeric value somewhere at the beginning of the file that makes the stream go bad.
If you don't debug then I'd recommend that you spread much more 'cout' to see what at a particular position happens.
You are right i told it to stop before it reaches the 91 and it worked fine. My file in notepad ++ reads as:
(list of numbers) CR LF
the problem using the string is the sort. It puts 103 before 12 because 0 comes before 2. It seems I need to properly mark EOF or somehow tell it to only insert into the list if the int is not equal to a newline character.
ok so I spread the couts around and found out that with this code it does print out the entire list in cout, but my function for the ostream is not being called to write it to the file
istream& operator>>(istream& inFile, bubble& inList)
{
nodeType *newNode;
int num;
char ch; //exists only to control the EOF
ch = ' '; // priming read for control
inList.head_ptr = NULL;
while (ch != nwln && inFile) // read until eof
{
inFile >> num; // updates at beginning to not lose last number
if(!isdigit(ch)) // only insert if ch is not a number
{
newNode = new nodeType;
newNode->info = num;
newNode->link = inList.head_ptr;
inList.head_ptr = newNode;
}
inFile.get(ch); // update for EOF control
}
return inFile;
}