I need to add a method readFile() which will read integer values from a file named as numbers.txt. The method will open, check and close numbers.txt. The integers are put in the list using the appendToList() method. Read the file one integer at a time; if there are too many integers for the size of the array, simply ignore the extras. The code appendToList was given to me and I created the readFile(). It's not appending it and I don't know if I'm calling it wrong or what
ItemList::ItemList(int arraySize = 10)
{
// dynamically allocate the list array
listItem = newint[arraySize];
maxSize = arraySize;
// be sure to note that listItem[0] is undefined at this point
// set all node links to indicate node available for insert
clearList();
}
bool ItemList::appendToList(int value)
{
bool itWorked = false; // safest default is insert failed
if ( isEmpty() ) // this will be the first item
{
// this "wakes up" all the list pointers
topItem = listItem;
currentItem = listItem;
nextAvailable = currentItem + 1;
*currentItem = value;
itWorked = true;
}
elseif ( (nextAvailable - listItem) < maxSize ) // there is space available
{
currentItem = nextAvailable;
++nextAvailable;
*currentItem = value;
itWorked = true;
} // there is space available
else
{
itWorked = false;
}
return itWorked;
}
void ItemList::readFile()
{
ifstream inFile;
inFile.open("numbers.txt"); //open the file numbers.txt
if(!inFile)//if file doesn't open
{
cout << "Could not open file!" << endl;
}
else
{
int i = 0;
while(inFile >> listItem[i])//reads the ints int the array of listItem
{
appendToList(i); //appends to lsit
i++;
}
}
inFile.close();
}