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;
}
}
Explanation / Answer
Hope this helps. Let me know if you have any questions. Please rate. :) There were a few things that were being done wrong. Your read function should be able to read any number of items in the file - not just 10. To do this, you use a linked list of the Nodes, not an array of size 10. The read function takes a string parameter for the input file name. If you can't open the file, this function will return NULL. Otherwise, it reads theoretically infinite entries from the file and builds a linked list. The print function should then print theoretically infinite entries from the linked list, so it takes the root of the list as the parameter and prints them out as long as the "current" node is not NULL. Here ya go: #include #include #include using namespace std; struct Node { string parts_named; long ID; float price; Node * next; }; Node * read (string fileName); void print(Node * list); int main () { Node * list; string fileName; cout > fileName; list = read(fileName); print(list); system ("PAUSE"); return 0; } Node * read (string fileName) { Node * list = NULL; Node * cur = NULL; ifstream myfile; myfile.open(fileName); if(!myfile.is_open()) { cout price; getline(myfile, garbage); getline(myfile, garbage); temp->next = NULL; if (list == NULL) { list = temp; } if (cur == NULL) { cur = temp; } else { cur->next = temp; cur = cur->next; } } myfile.close(); return list; } //This function prints the text file using the array setup. void print(Node * list) { Node * cur = list; while(cur != NULL) { coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.