Question: My program will not open the text file ? ? I keep getting this error message. I have included a screenshot of the information that
My program will not open the text file I keep getting this error message. I have included a screenshot of the information that is in the textfile that should be read. Should I make seperate cpp or h files for the code
#include
#include
#include For random numbers
#include For srand, rand
using namespace std;
Node struture for D linked list grid
struct Node
int value;
Node right;
Node down;
Nodeint val : valueval rightnullptr downnullptr
;
LinkedList class that manags the D grid
class LinkedList
private:
Node head;
int rows, cols;
public:
LinkedList : headnullptr rows cols
Destrctor to free memory when the object is destroyd
~LinkedList
clearList;
Initilize the list from a file input
void inputListconst string& filename
ifstream filepluginFiletxt;
if file
cout "Error: Unable to open file." endl;
return;
file rows cols;
Node prevRowHead nullptr;
Node prev nullptr;
for int i ; i rows; i
Node currentRowHead nullptr;
for int j ; j cols; j
int value;
file value;
Node newNode new Nodevalue;
if j currentRowHead newNode;
if i && j head newNode;
if prev prevright newNode;
if prevRowHead
prevRowHeaddown newNode;
prevRowHead prevRowHeadright;
prev newNode;
prev nullptr;
prevRowHead currentRowHead;
file.close;
Print the grid neatly
void printList const
Node row head;
while row
Node col row;
while col
cout colvalue ;
col colright;
cout endl;
row rowdown;
Get the value at a speciific row & column
int getItemint row, int col const
Node current head;
for int i ; i row && current; i current currentdown;
for int j ; j col && current; j current currentright;
if current return currentvalue;
return ; Invaliid index
Set the value at a specifiic row & column
void setItemint row, int col, int value
Node current head;
for int i ; i row && current; i current currentdown;
for int j ; j col && current; j current currentright;
if current currentvalue value;
Clear entire grid & free memory
void clearList
Node row head;
while row
Node col row;
while col
Node temp col;
col colright;
delete temp;
Node temp row;
row rowdown;
delete temp;
head nullptr;
rows ;
cols ;
Get no of rows
int getRows const return rows;
Get no of columns
int getCols const return cols;
;
Driver function to test the LinkedList class
int main
srandtime; Seed for random number generation
LinkedList grid;
cout "Loading the grid:
;
grid.inputListGridNumberstxt;
grid.printList;
cout
Testing getItem:
;
for int i ; i ; i
int row rand grid.getRows;
int col rand grid.getCols;
cout "The element at row col is
grid.getItemrow col endl;
cout
Testing setItem:
;
for int i ; i ; i
int row rand grid.getRows;
int col rand grid.getCols;
int newValue rand;
cout "Changing element at row col to
newValue endl;
grid.setItemrow col, newValue;
grid.printList;
cout
Deleting the grid:
;
grid.clearList;
grid.printList;
return ;
textfile information
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
