Consider the declaration of the struct houseType given in Chapter 9 of your text
ID: 3877485 • Letter: C
Question
Consider the declaration of the struct houseType given in Chapter 9 of your textbook. Write C++ statements to do the following 6. a) Declare variables oldHouse and newHouse of type houseType b) Store the following information into oldHouse: style Two-story, number of bedrooms - 5, number of bathrooms 3, number of cars in garage -4, year built - 1975, finished square footage 3500, price - 675,000, and tax 12500 c)Copy the values of the components of oldHouse into the corresponding components of newHouseExplanation / Answer
//copies the content of struct variable to another struct variable of same type
#include <iostream>
#include <string>
using namespace std;
int main() {
// declare the houseType as struct
struct houseType{
string style;
int noofbedroom;
int noofbathroom;
int noofcaringarage;
int yearbuilt;
double squarefootage;
double price;
double tax;
};
//declare the variable oldhouse and newhouse as struct housetype
houseType oldHouse, newHouse;
//assign the values to oldhouse struct variable
oldHouse.style = "Two-Story";
oldHouse.noofbedroom = 5;
oldHouse.noofbathroom =3;
oldHouse.noofcaringarage = 4;
oldHouse.yearbuilt = 1975;
oldHouse.squarefootage = 3500;
oldHouse.price = 675000;
oldHouse.tax = 12500;
//display the values stored in toldhouse
cout<<" oldHouse style :"<<oldHouse.style<<" BedRooms :"<<oldHouse.noofbedroom<<" Bathrooms :";
cout<<oldHouse.noofbathroom<<" Car in Garage : "<<oldHouse.noofcaringarage<<" Year built :"<<oldHouse.yearbuilt;
cout<<" Finished square footage :"<<oldHouse.squarefootage<<" Price :"<<oldHouse.price<<" Tax :"<<oldHouse.tax;
newHouse = oldHouse; //copies the content of the oldhouse to newhouse
//displays the new house values
cout<<" newHouse style :"<<newHouse.style<<" BedRooms :"<<newHouse.noofbedroom<<" Bathrooms :";
cout<<newHouse.noofbathroom<<" Car in Garage : "<<newHouse.noofcaringarage<<" Year built :"<<newHouse.yearbuilt;
cout<<" Finished square footage :"<<newHouse.squarefootage<<" Price :"<<newHouse.price<<" Tax :"<<newHouse.tax;
return 0;
}
output:
oldHouse
style :Two-Story
BedRooms :5
Bathrooms :3
Car in Garage : 4
Year built :1975
Finished square footage :3500
Price :675000
Tax :12500
newHouse
style :Two-Story
BedRooms :5
Bathrooms :3
Car in Garage : 4
Year built :1975
Finished square footage :3500
Price :675000
Tax :12500
RUN SUCCESSFUL (total time: 200ms)