Below is the assignment and below that is the program I wrote. I am not sure if
ID: 3622051 • Letter: B
Question
Below is the assignment and below that is the program I wrote. I am not sure if I did it correctly. If I did not do it correctly can you please show me how to fix it. Thank-you.In this assignment, you use structure, pointer, string comparison to build a simple linked list, and summarize average, min, max on the prices on the linked list.
The structure is defined as
struct Node {
char parts_name[100];
long ID;
float price;
Node * next;
};
This assignment includes:
(1) Write a function that will read from a file with the filename passed as the
parameter of the function, and the read information are about the parts and their prices.
Each part has its name, ID, and price. The information should be stored in a linked list,
sequentially from read-in. The linked list does not need to be sorted. Each node is a
structure (a Node, as defined above). This function should return a pointer that is the
head of the linked list. This function should print out (cout) the information stored in the
nodes in the linked list, sequentially, just as they are in a8.txt. Note the print out must be
from the linked list. (3pts, if works correctly.)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Node {
string parts_named;
long ID;
float price;
Node * next;
};
int read (Node machines[]);
void print(Node machines[], int num);
int main ()
{
Node machines[10];
int i = read(machines);
print(machines, i);
system ("PAUSE");
return 0;
}
int read (Node machines[])
{
ifstream myfile;
myfile.open("a8.txt");
if(!myfile.is_open())
{
cout << "Cannot open " << "a7.txt" << "." << endl << endl;
return -1;
}
int i = 0;
string garbage; // for creating an empty line in between data of each person.
while (!myfile.fail() && i < 10)
{
getline(myfile, machines[i].parts_named);
myfile >> machines[i].ID;
myfile >> machines[i].price;
getline(myfile, garbage);
getline(myfile, garbage);
i++;
}
myfile.close();
return i;
}
//This function prints the text file using the array setup.
void print(Node machines[], int num)
{
for (int i = 0; i < num; i++)
{
cout << "Part Name: " << machines[i].parts_named << endl;
cout << "ID Number: " << machines[i].ID << endl;
cout << "Price: " << machines[i].price << endl << endl;
}
}