Need help converting from array of structures to array of classes. Please keep i
ID: 3723031 • Letter: N
Question
Need help converting from array of structures to array of classes. Please keep it simple, thank you. using namespace std; #include <fstream> #include <iostream> #include <string> #include <iomanip> struct houserecord { string address; string city; string state; string zip; double price; double bathrooms; int bedrooms; }; void loadarray(houserecord house[], int size); void printfile(); void sortarray(houserecord houses[], int size); void printreport(houserecord houses[], int size); void printreportreverse(houserecord house[], int size); void printhouse(houserecord house); void houseselect(houserecord house[], int size); void printheader(); void printform(houserecord house); void addhouse(houserecord & house); //------------------------------------------------------------------------------------ int main() { ifstream fin; ofstream fout; int i,size, max = 10; houserecord house[20]; printfile(); loadarray(house, 10); printreport(house, 10); addhouse(house[10]); printreport(house, 11); sortarray(house, 11); printreport(house, 11); printreportreverse(house, 11); houseselect(house, 11); cout <<"Press return key to continue. "; cin.get(); fin.close(); return 0; } //------------------------------------------------------------------------ void sortarray(houserecord house[], int size) { int i, swapped; houserecord temp; do { swapped = 0; for (i = 0; i < size-1; i++) { if (house[i].price > house[i + 1].price) { temp = house[i]; house[i] = house[i + 1]; house[i + 1] = temp; swapped = 1; } } } while (swapped != 0); } //------------------------------------------------------------------------ void printreport(houserecord house[], int size) { int i; printheader(); for (i = 0; i < size; i++) { printhouse(house[i]); } } //------------------------------------------------------------------------ void printheader() { int i; cout <<" "<< setw(23) << left << "Address" << setw(15) << left << "City" << setw(10) << left << "State" << setw(10) << left << "Zip" << setw(9) << right << "Price " << setw(6) << right << "Baths" << setw(6) << right << "Beds" << endl << endl; for (i = 0; i < 80; i++) cout << "-"; cout << endl; } //------------------------------------------------------------------------ void printreportreverse(houserecord house[], int size) { int i; printheader(); for (i = size-1; i >= 0; i--) { printhouse(house[i]); } } //------------------------------------------------------------------------ void printhouse (houserecord house) { cout << setw(23) << left << house.address << setw(15) << left << house.city << setw(10) << left << house.state << setw(10) << left << house.zip << setw(8) << right << house.price << setw(6) << right << house.bathrooms << setw(6) << right << house.bedrooms << endl << endl; return; } //------------------------------------------------------------------------ void houseselect(houserecord house[], int size) { int i; string choice; cin.ignore(); cout << "Enter City Name: " ; getline(cin, choice); cout <<endl; for (i = 0; i < size; i++) { if (choice==house[i].city) printform(house[i]); } } //------------------------------------------------------------------------ void printfile() { ifstream fin; ofstream fout; cout << "Input data file as read "; char ch; fin.open("documentation.txt"); fin.get(ch); while (!fin.eof()) { cout << ch; fin.get(ch); } fin.close(); cin.get(); } //------------------------------------------------------------------------ void loadarray(houserecord house[], int size) { ifstream fin; int i; fin.open("house.txt"); for (i = 0; i < size; i++) { getline(fin, house[i].address); getline(fin, house[i].city); getline(fin, house[i].state); getline(fin, house[i].zip); fin >> house[i].price; fin >> house[i].bathrooms; fin >> house[i].bedrooms; fin.ignore(); // printhouse(house[i]); } } //------------------------------------------------------------------------ void addhouse(houserecord & house) { cout << "Enter Address "; getline(cin, house.address); cout << "Enter city "; getline(cin, house.city); cout << "Enter state "; getline(cin, house.state); cout << "Enter zip "; getline(cin, house.zip); cout << "Enter price "; cin >> house.price; cout << "Enter bathrooms "; cin >> house.bathrooms; cout << "Enter bedrooms "; cin >> house.bedrooms; cout << " "; } //------------------------------------------------------------------------ void printform(houserecord house) { cout <<" ------------------------------------------------------------------ "; cout << "Address "<< setw(23) << left << house.address << " City "<< setw(15) << left << house.city << "State "<< setw(10) << left << house.state << "Zip "<< setw(10) << left << house.zip << " Price "<< setw(8) << left << house.price << " Bathrooms "<< setw(6) << house.bathrooms << "Bedrooms "<< setw(6) << house.bedrooms << endl; cout <<" ------------------------------------------------------------------ "; }
Explanation / Answer
Hi.. I have changed your code to array of classes. Please check below.
using namespace std;
#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
class houserecord
{
public:
string address;
string city;
string state;
string zip;
double price;
double bathrooms;
int bedrooms;
};
void loadarray(houserecord house[], int size);
void printfile();
void sortarray(houserecord houses[], int size);
void printreport(houserecord houses[], int size);
void printreportreverse(houserecord house[], int size);
void printhouse(houserecord house);
void houseselect(houserecord house[], int size);
void printheader();
void printform(houserecord house);
void addhouse(houserecord & house);
//------------------------------------------------------------------------------------
int main()
{ ifstream fin;
ofstream fout;
int i,size, max = 10;
houserecord house[20];
printfile();
loadarray(house, 10);
printreport(house, 10);
addhouse(house[10]);
printreport(house, 11);
sortarray(house, 11);
printreport(house, 11);
printreportreverse(house, 11);
houseselect(house, 11);
cout <<"Press return key to continue. ";
cin.get();
fin.close();
return 0;
}
//------------------------------------------------------------------------
void sortarray(houserecord house[], int size)
{
int i, swapped;
houserecord temp;
do {
swapped = 0;
for (i = 0; i < size-1; i++)
{
if (house[i].price > house[i + 1].price)
{
temp = house[i];
house[i] = house[i + 1];
house[i + 1] = temp;
swapped = 1;
}
}
} while (swapped != 0);
}
//------------------------------------------------------------------------
void printreport(houserecord house[], int size)
{
int i;
printheader();
for (i = 0; i < size; i++)
{ printhouse(house[i]);
}
}
//------------------------------------------------------------------------
void printheader()
{ int i;
cout <<" "<< setw(23) << left << "Address"
<< setw(15) << left << "City"
<< setw(10) << left << "State"
<< setw(10) << left << "Zip"
<< setw(9) << right << "Price "
<< setw(6) << right << "Baths"
<< setw(6) << right << "Beds" << endl
<< endl;
for (i = 0; i < 80; i++)
cout << "-";
cout << endl;
}
//------------------------------------------------------------------------
void printreportreverse(houserecord house[], int size)
{
int i;
printheader();
for (i = size-1; i >= 0; i--)
{
printhouse(house[i]);
}
}
//------------------------------------------------------------------------
void printhouse (houserecord house)
{
cout << setw(23) << left << house.address
<< setw(15) << left << house.city
<< setw(10) << left << house.state
<< setw(10) << left << house.zip
<< setw(8) << right << house.price
<< setw(6) << right << house.bathrooms
<< setw(6) << right << house.bedrooms << endl
<< endl;
return;
}
//------------------------------------------------------------------------
void houseselect(houserecord house[], int size)
{
int i;
string choice;
cin.ignore();
cout << "Enter City Name: " ;
getline(cin, choice);
cout <<endl;
for (i = 0; i < size; i++)
{ if (choice==house[i].city)
printform(house[i]);
}
}
//------------------------------------------------------------------------
void printfile()
{
ifstream fin;
ofstream fout;
cout << "Input data file as read ";
char ch;
fin.open("documentation.txt");
fin.get(ch);
while (!fin.eof())
{
cout << ch;
fin.get(ch);
}
fin.close();
cin.get();
}
//------------------------------------------------------------------------
void loadarray(houserecord house[], int size)
{
ifstream fin;
int i;
fin.open("house.txt");
for (i = 0; i < size; i++)
{
getline(fin, house[i].address);
getline(fin, house[i].city);
getline(fin, house[i].state);
getline(fin, house[i].zip);
fin >> house[i].price;
fin >> house[i].bathrooms;
fin >> house[i].bedrooms;
fin.ignore();
// printhouse(house[i]);
}
}
//------------------------------------------------------------------------
void addhouse(houserecord & house)
{
cout << "Enter Address ";
getline(cin, house.address);
cout << "Enter city ";
getline(cin, house.city);
cout << "Enter state ";
getline(cin, house.state);
cout << "Enter zip ";
getline(cin, house.zip);
cout << "Enter price ";
cin >> house.price;
cout << "Enter bathrooms ";
cin >> house.bathrooms;
cout << "Enter bedrooms ";
cin >> house.bedrooms;
cout << " ";
}
//------------------------------------------------------------------------
void printform(houserecord house)
{
cout <<" ------------------------------------------------------------------ ";
cout << "Address "<< setw(23) << left << house.address
<< " City "<< setw(15) << left << house.city
<< "State "<< setw(10) << left << house.state
<< "Zip "<< setw(10) << left << house.zip
<< " Price "<< setw(8) << left << house.price
<< " Bathrooms "<< setw(6) << house.bathrooms
<< "Bedrooms "<< setw(6) << house.bedrooms
<< endl;
cout <<" ------------------------------------------------------------------ ";
}
Please test the code and let me know any issues. Thank you. All the best.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.