Write a program that will read the radii of circles. Use an array of records whe
ID: 3546949 • Letter: W
Question
Write a program that will read the radii of circles. Use an array of records where each record will have the radius of the circle read from the keyboard and the diameter and area of the circle will be calculated by the program. This information (radius, diameter and area) is stored in a binary file. The information in the binary file is then read back into the records and stored in a text output file. Left justify the information for each field. Each record will consist of the following fields:
radius float diameter float area float
You may assume a maximum of 20 records. You may want to include the
cmath library.
You need to know the formulas for finding the area and circumference of a circle.
This assignment is very similar to the program in Lab 12.3.
Sample Run:
Enter the following information: Radius of circle: 5
Enter a Y if you would like to input more data
y
Enter the following information: Radius of circle: 4
Enter a Y if you would like to input more data
y
Enter the following information: Radius of circle: 7
Enter a Y if you would like to input more data
n
That
Explanation / Answer
#include <fstream>
#include <iostream>
#include <cctype> // for toupper function using namespace std;
struct Grades // declaring a structure
{
float radius;
};
int main()
{
char ch;
float area;
float circum;
fstream tests("circle.dat", ios::out | ios::binary);
// defines tests as an output binary file
Grades student; // defines student as a record (struct)
char more; // used to determine if there is more input
do
{
cout << "Enter the radius: ";
cin >> student.radius;
cin.ignore();
// write this record to the file
tests.write((char *) &student, sizeof(student));
cout << "Enter a y if you would like to input more data ";
cin >> more;
cin.ignore();
}
while (toupper(more) == 'Y');
tests.close();
fstream in("circle.dat", ios::in | ios::binary);
fstream out("circle_output.dat", ios::out | ios::binary);
out << "Radius Area Circumferene" << endl;
while ((ch = in.peek()) != EOF) // no need to prime the read
{
infile >> radius;
infile.ignore(10,' '); // ignores the rest of the line
// and consumes end of line marker
area = 3.14 * radius*radius;
circum = 2*3.14*radius;
outfile << radius << setw(3) << area << setw(2) << circum << endl;
}
in.close();
out.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.