Write a program to ask for Part number, part name, partclass, number in stock an
ID: 3849268 • Letter: W
Question
Write a program to ask for Part number, part name, partclass, number in stock and unit price. The data should be read into a struct and passed to a function which writes it out to a data file. Part number and name will be strings, partclass is a character, number in stock is an integer and unit price is a double. Enclose your program in a do while which asks the user if he/she wants to enter more parts.
Typical run
Enter part information (part number, name, class, on hand balance and price):
P-14376 Widget B 120 34.95
More parts? Y or N y
P-16543 Wodget C 80 15.75
More parts? Y or N n
Explanation / Answer
#include <fstream>
#include <iostream>
#include <string.h>
using namespace std;
struct Part
{
char partNo[20];
char partName[20];
char partClass;
int numStocks;
double unitPrice;
};
void writeToFile(Part part)
{
// Serializing struct to file.data
ofstream output_file("file.data", ios::binary);
output_file.write((char*)&part, sizeof(part));
output_file.close();
}
int main()
{
Part part[10];
char option;
char partno[20],partname[20];
double unitprice;
int stocks;
char partclass;
int i = 0;
do
{
cout<<" Enter part information (part number, name, class, on hand balance and price):";
cin>>partno>>partname>>partclass>>stocks>>unitprice;
strcpy(part[0].partNo,partno);
strcpy(part[0].partName,partname);
part[0].partClass = partclass;
part[0].numStocks = stocks;
part[0].unitPrice = unitprice;
writeToFile(part[i]);
i++;
cout<<" More parts? Y or N ";
cin>>option;
if(option != 'y' || option != 'Y')
break;
}while(option == 'Y' || option == 'y');
return 0;
}
output:
File.data
P-14376"@hWidgetBtxyA@`%P@C+@<XvX`@[/0`vX
``BI/HJ``X @0@X @`
`
@
@ @
@@
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.