Skip to content
Snippets Groups Projects
Commit 515e228a authored by Kevin Höllring's avatar Kevin Höllring
Browse files

Add explanatory comments to sample 3

parent d3dd5829
No related branches found
No related tags found
No related merge requests found
#include <iostream>
#include "sample_3.h"
#include <iostream>
// This function is passed a copy of the original house (no &)
// Then the copy is painted black but the original still has its original color
void city::copy_repaint(city::House torepaint) { torepaint.color = black; }
void city::copy_repaint(city::House torepaint) {
torepaint.color = black;
}
// This function is passed a reference to the original house (&)
// Then this house is painted black
void city::copy_free_repaint(city::House& torepaint) {
torepaint.color = black;
}
......@@ -13,8 +14,7 @@ void city::copy_free_repaint(city::House &torepaint) {
void city::describe_house(city::House& house) {
std::cout << "The resident " << house.owner;
std::cout << " is currently living in their ";
switch (house.color)
{
switch (house.color) {
case red:
std::cout << "red";
break;
......@@ -38,10 +38,10 @@ void city::describe_house(city::House &house) {
}
int main(int argc, char** argv) {
using std::string;
using std::cout;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using namespace city;
// Declare a variable to hold the name of the user
......@@ -58,12 +58,14 @@ int main(int argc, char** argv) {
// Check if the name has at least 3 characters
if (owner.length() < 3) {
// Print out hint
cout << "Your name is suspiciously short. Please enter at least 3 characters" << endl;
cout << "Your name is suspiciously short. Please enter at least 3 "
"characters"
<< endl;
// Skip to next run of loop
continue;
}
else {
// Write out a mix of a static string and the string previously read in
} else {
// Write out a mix of a static string and the string previously read
// in
cout << "So your name is " << owner << "." << endl;
// Let us break free of this infinite loop
......@@ -71,7 +73,6 @@ int main(int argc, char** argv) {
}
}
// Let's ask the user for a color of the house
cout << "Which color should your house have? (r|g|b|w)" << endl;
......@@ -107,12 +108,14 @@ int main(int argc, char** argv) {
// Deal with an unknown color
if (unknown_color) {
cout << "The color you chose was not known. Please choose one out of r,g,b,w" << endl;
cout << "The color you chose was not known. Please choose one out "
"of r,g,b,w"
<< endl;
// Retry
continue;
}
else {
cout << "The color you chose was " << color << ". Your house will be painted in this color." << endl;
} else {
cout << "The color you chose was " << color
<< ". Your house will be painted in this color." << endl;
// End loop
break;
}
......@@ -129,18 +132,19 @@ int main(int argc, char** argv) {
cin >> house_number;
if (house_number <= 0) {
cout << "Oh no! Your favorite house number should certainly be positive. Please consider this again." << endl;
cout << "Oh no! Your favorite house number should certainly be "
"positive. Please consider this again."
<< endl;
// Retry
continue;
}
else {
cout << "Oh, so your house will have the number " << house_number << " just for you." << endl;
} else {
cout << "Oh, so your house will have the number " << house_number
<< " just for you." << endl;
// Stop the trying loop
break;
}
}
// Declare a house of custom type and assign its member fields.
House designed_house;
designed_house.color = houseColor;
......@@ -150,17 +154,16 @@ int main(int argc, char** argv) {
std::cout << "Let us summarize your registration:" << endl;
describe_house(designed_house);
std::cout << "Let us summarize your registration:" << endl;
describe_house(designed_house);
std::cout << "We will now try to repaint your house black:" << endl;
// Demonstrate that passing the copy of a house will not change the original
copy_repaint(designed_house);
describe_house(designed_house);
std::cout << "Let us try it again a little differently:" << endl;
// Demonstrate that passing a reference to the original will cause its color
// to change
copy_free_repaint(designed_house);
describe_house(designed_house);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment