Help me with this question, it is for C++ Write a program that will: 1) read an
ID: 664905 • Letter: H
Question
Help me with this question, it is for C++
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.
Explanation / Answer
working c++ code
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
#include<cstdio>
int main()
{
using namespace std;
struct personsinfo
{
char firstname[15];
char lastname [15];
char streetaddress[30];
char city [20];
char state[5];
int zipcode;
};
personsinfo pr;
fstream personsinfo("greacon.txt", ios::out|ios::binary);
cout<< " Enter the following information ";
char response;
do
{
cout<< "Enter person's FirstName: ";
cin.getline(pr.firstname, 15);
cout<<"Enter person's LastName: ";
cin.getline(pr.lastname, 15);
cout<<"Enter Street Address: ";
cin.getline(pr.streetaddress, 30);
cout<<"Enter City: ";
cin.getline(pr.city, 20);
cout<<"Enter State: ";
cin.getline(pr.state, 5);
cout<<"Enter Zipcode: ";
cin >> pr.zipcode;
personsinfo.write(reinterpret_cast<char *> (&pr), sizeof(pr));
cout << "press Y if you like to input more data ";
cin >> response;
getchar();
}
while(response== 'Y' | response== 'y');
personsinfo.close();
personsinfo.open("greacon.txt", ios::in|ios::binary);
personsinfo.read(reinterpret_cast<char *> (&pr), sizeof(pr));
cout<<"Thats all the information ";
cout<<"The output file contains the following: ";
while(!personsinfo.eof())
{
cout<< pr.firstname<< " ";
cout<< pr.lastname<< " ";
cout<< pr.streetaddress<<" ";
cout<< pr.city<< " ";
cout<< pr.state<< " ";
cout<< pr.zipcode;
cout<<endl;
cin.get();
personsinfo.read(reinterpret_cast<char *> (&pr), sizeof(pr));
}
personsinfo.close();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.