\' can someone edit this code where it will output to a file #include <iostream>
ID: 3790932 • Letter: #
Question
'
can someone edit this code where it will output to a file
#include <iostream>
#include <fstream> //For file operations
#include <string.h> //For string comparisions
using namespace std;
struct StockNode{ //Creating of linked list node structure
char itemname[20];
char itemnumber[5];
char quantity[3];
char price[6];
char safestock[3];
struct StockNode *link;
};
class InventoryStock //Class for operations and members
{
StockNode *head;
public:
InventoryStock() //Constructor default
{
head=NULL;
}
void addStockNode(StockNode *nn) //Add node method
{
StockNode *temp=NULL;
if(head==NULL || strcmp(head->itemname,nn->itemname)>=0) // Adding first node whether null or head node smaller than nn
{
nn->link=head;
head=nn;
return;
}
temp=head;
while(temp->link!=NULL && strcmp(temp->itemname,nn->itemname)<0) //Traverse upto null before or find a node which is > than new node nn
temp=temp->link;
nn->link=temp->link; //If found insert in its sorted location
temp->link=nn;
}
void traverse() //To display sorted list
{
StockNode *temp=head;
cout<<" Names of Items : "<<endl;
while(temp!=NULL)
cout<<temp->itemname<<"->";
}
};
int main()
{
InventoryStock i1;
StockNode readstock;
ifstream ifile;
ifile.open("Invent.txt"); //Open file for reading
while(!ifile.eof()) //Read until eof
{
ifile>>readstock.itemname>>readstock.itemnumber>>readstock.quantity>>readstock.price>>readstock.safestock; //Read each record of file
i1.addStockNode(&readstock); //Add to sorted linked list
}
ifile.close(); //Close the file
i1.traverse(); // Traverse list
return 0;
}
Explanation / Answer
#include <iostream>
#include<stdio.h>
#include <fstream> //For file operations
#include <string.h> //For string comparisions
using namespace std;
struct StockNode{ //Creating of linked list node structure
char itemname[20];
char itemnumber[5];
char quantity[3];
char price[6];
char safestock[3];
struct StockNode *link;
};
class InventoryStock //Class for operations and members
{
StockNode *head;
public:
InventoryStock() //Constructor default
{
head=NULL;
}
void addStockNode(StockNode *nn) //Add node method
{
StockNode *temp=NULL;
if(head==NULL || strcmp(head->itemname,nn->itemname)>=0) // Adding first node whether null or head node smaller than nn
{
nn->link=head;
head=nn;
return;
}
temp=head;
while(temp->link!=NULL && strcmp(temp->itemname,nn->itemname)<0) //Traverse upto null before or find a node which is > than new node nn
temp=temp->link;
nn->link=temp->link; //If found insert in its sorted location
temp->link=nn;
}
void traverse() //To display sorted list
{
StockNode *temp=head;
cout<<" Names of Items : "<<endl;
while(temp!=NULL)
cout<<temp->itemname<<"->";
}
};
int main()
{
InventoryStock i1;
StockNode readstock;
ifstream ifile;
ifile.open("Invent.txt"); //Open file for reading
while(!ifile.eof()) //Read until eof
{
ifile>>readstock.itemname>>readstock.itemnumber>>readstock.quantity>>readstock.price>>readstock.safestock; //Read each record of file
i1.addStockNode(&readstock); //Add to sorted linked list
}
ifile.close();
i1.traverse(); // Traverse list
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.