a. Write a program that accepts keyboard input of the five-digit stock number, p
ID: 3625545 • Letter: A
Question
a. Write a program that accepts keyboard input of the five-digit stock number, plus length, width, and height (inches) of various cardboard boxes, then stores this data in a disk file named box.dat. Write the program in such a way that you may append new data after echoing old data that may have been stored previously.b. Write a second program that retrieves stock number plus box length, width, and height from the box.dat file, and, neglecting cardboard thickness, computes box volume and outside surface area (assume that the box has a closed top). Display on the computer monitor all six data fields (stock number, length, width, height, volume, and surface area) in appropriately labeled columns.
c. To the program written in part (b) add code that will also store the six data fields in a new file named during execution.
Explanation / Answer
a)
//Header file section
#include<iostream>
using namespace std;
void main()
{
int stockNumber;
int width,length,height;
fstream outputFile("box.dat",ios::out);
char ch;
if(outputFile.fail())
{
cout<<"Error Canot open file. ";
return 0;
}
do
{
cout<<"Enter stock number:";
cin>>stockNumber;
cout<<"Enter width:";
cin>>width;
cout<<"Enter length:";
cin>>length;
cout<<"Enter height:";
cin>>height;
outputFile<<stckNumber<<" "<<width<<" "<<length<<" "<<height;
cout<<"To repeat press y/n";
cin>>y;
}while(ch=='y');
cout<<"Data return to file:";
//pause system for a while
system("pause");
}//end main
b)
//Header file section
#include<iostream>
using namespace std;
void main()
{
int stockNumber;
int width,length,height;
int volume,sArea;
fstream inputFile("box.dat",ios::in);
char ch;
if(outputFile.fail())
{
cout<<"Error Canot open file. ";
return 0;
}
while(inputFile>>stockNumber)
{
inputFile>>width;
inputFile>>length;
inputFile>>height;
cout<<"Stock Number:"<<stckNumber<<" width: "<<width
<<" Length:"<<length<<"Height:"<<height;
volume=length*width*height;
sArea=2*(length*width+width*height+height*length);
cout<<"Volume:"<<volume<<endl;
cout<<"Surface Area:"<<sArea<<endl;
}
cout<<"Data return to file:";
//pause system for a while
system("pause");
}//end main
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.