Option 1: Write a program that will: 1) read an array of records from the keyboa
ID: 3546963 • Letter: O
Question
Option 1: Write a program that will: 1) read an array of records from the keyboard, 2) store this information to a binary file, 3) read from the binary file back to the array of records, 4) store this information to a textfile. Left justify the information for each field. Each record will consist of the following fields:
first name 15 characters last name 15 characters street address 30 characters city 20 characters state 5 characters zip long integer
You may assume a maximum of 20 records.
This assignment is very similar to the program found in Lab 12.3.
Sample Run:
Enter the following information Person
Explanation / Answer
#include <fstream>
#include <iostream>
#include <cctype> // for toupper function using namespace std;
const int NAMESIZE = 31;
struct Grades // declaring a structure
{
char name[NAMESIZE];
char city[15];
int zip;
};
int main()
{
char grade[NAMESIZE*2];
fstream tests("score.dat", ios::out | ios::binary);
// defines tests as an output binary file
Grades stu; // defines student as a record (struct)
char more; // used to determine if there is more input
do
{
cout << "Enter the following information" << endl; cout << " name: ";
cin.getline(stu.name,NAMESIZE);
cout << "City :";
cin.getline(stu.city,15);
cin.ignore(); // ignore rest of line
// write this record to the file
tests.write((char *) &stu, sizeof(stu));
cout << "Enter a y if you would like to input more data ";
cin >> more;
cin.ignore();
} while (toupper(more) == 'Y');
tests.close();
fstream tests("score.dat", ios::in | ios::binary);
fstream test2("score2.txt", ios::out | ios::binary);
test.read((char*) grade, sizeof(grade));
test2.write((char*)grade, sizeof(grade));
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.