I have created code that can generates random numbers between 0 and 99, then converts them to Binary. I now have to create a singly-linked list with the nodes in the same order they were generated. When you are done close your file. Now you will call a function to print the list, and then call a function to delete the list. Confused into how. Here is my code:
//define a link list building block, commonly called a node. When the list is created, next will hold the address of the next node in the list or it will be NULL
//if there are no more nodes. In this way, we can create a connectef chain of nodes called a linked list.
struct node
{
int number;
node* next;
};
int GetSize(int);
void DoWork(int);
int WriteData(int);
int ReadData(int);
int insertNode(int, node*);
void deleteList(node*);
void printList(node*);
int main(int argc, char* argv[])
{
//checking to see if size was passed in through command line, just in case
if (argc == 2)
{
//we passed in number to represent the size of the data. Currently in char, we want to cast it to an int
int Size = atoi(argv[1]);
//see if data was passed in
if ((Size >= 20) && (Size <= 100))
{
//we have a valid size, process it
DoWork(Size);
}
//if arg is not a valid number
else
{
//call getSize function to get the number from the user
int Size = 0;
Size = GetSize(Size);
//we have a valid size, process it
DoWork(Size);
}
}
//if size was not passed in
else
{
//call the GetSize function to get the number form the user
int Size = 0;
Size = GetSize(Size);
//we have a valid size, process it
DoWork(Size);
}
return (0);
}
//Prompt the user to enter a number (int) between 20 and 100 (inclusive).
int GetSize(int Size)
{
while (Size < 20 || Size > 100)
{
cout << endl;
cout << "What size do you want to generate (20 to 100)? ";
cin >> Size;
//here we will check to make sure we are in the range
if (cin.fail() || Size < 20 && Size > 100)
{
//this clears the cin and what is in the buffer. If this is not cleared the program
//would continue in an infinite loop
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
//here we notify the user they have given bad info
cout << endl << "invalid option. Please enter number between 20 and 100.";
}
//return the value the user has given us
return(Size);
}
//this is used to avoid having to put it in multiple places
void DoWork(int Size)
{
//call the WriteData function to create data files provided by the user
WriteData(Size);
//call the readData function to read in the data from the created files
ReadData(Size);
}
//function used to write data to file
int WriteData(int Size)
{
//here we create an out stream writer and name it writeData
ofstream WriteData;
//here we tell the out of the stream writer to open a file named “numbers.txt”. If file does not exist it will be
//created. If it alreday exists it will be overwritten.
WriteData.open("numbers.txt");
//if problem opening or creating file, notify user
if (!WriteData)
{
cout << "Could not open/create text file for writing" << endl << endl;
return (1);
}
//here we create an outstream writer and name it WriteBinary
ofstream WriteBinary;
//here we tell the out stream writer to open a file named numbers.bin
WriteBinary.open("numbers.bin", ios::out | ios::binary);
//if problem opening or creating file, notify user
if (!WriteBinary)
{
cout << "Could not open/create binary file for writing" << endl << endl;
return (2);
}
//Here we are seeding the random fuction with the time of the computer.
srand((unsigned)time(NULL));
//this notifies the user we are going to begin writing the data to the files
cout << "Generating " << Size << " ints." << endl;
cout << "Start Writing Data to Files" << endl << endl;
//here we are creating a variable to store rand generated int
int Number;
//we will use this for loop to generate some rand data and store it in our Number variable
for (int i = 0; i < Size; i++)
{
//generate random number between 0 and 99.
Number = (rand() * rand()) % 100;
//write this number to the console so the user can see what number has been generated
cout << Number << endl;
//this writes the number to the text file
WriteData << Number << endl;
//this writes to the binary file
WriteBinary.write(reinterpret_cast<const char*>(&Number), sizeof(int));
}
//when the write loop has completed, we close our two files to prevent errors and data loss
WriteData.close();
WriteBinary.close();
}
int ReadData(int Size)
{
//creating two dynamic arrays to hold data and info read in from the files
int *TextReadArray = new int[Size];
//check to see if it was successful
if (TextReadArray == nullptr)
{
cout << endl << "---Memory not allocated for TextReadArray---" << endl << endl;
return(1);
}
int *BinaryReadArray = new int[Size];
//check to see if it was successful
if (BinaryReadArray == nullptr)
{
cout << endl << "---Memory not allocated for BinaryReadArray---" << endl << endl;
return(1);
}
//create stream reader name it ReadBinary
ifstream ReadText;
//here we point the stream reader to the file we created and open it
ReadText.open("numbers.txt");
//check to see if it was successful
if (!ReadText)
{
cout << "Could not open text file for reading." << endl << endl;
return(1);
}
//create stream reader name it ReadText
ifstream ReadBinary;
//here we point the stream reader to the file we created and open it
ReadBinary.open("numbers.bin", ios::in | ios::binary);
//check to see if it was successful
if (!ReadBinary)
{
cout << "Could not open text file for reading." << endl << endl;
return(2);
}
//tell the user to read back the data
cout << endl << "Begin reading data from files" << endl << endl;