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;
}
void city::copy_free_repaint(city::House &torepaint) {
torepaint.color = black;
}
void city::describe_house(city::House &house) {
std::cout << "The resident " << house.owner;
std::cout << " is currently living in their ";
switch (house.color)
{
case red:
std::cout << "red";
break;
case green:
std::cout << "green";
break;
case blue:
std::cout << "blue";
break;
case black:
std::cout << "black";
break;
case white:
std::cout << "white";
break;
default:
std::cout << "mystey-colored";
break;
}
std::cout << " house at number " << house.number << std::endl;
void city::describe_house(city::House& house) {
std::cout << "The resident " << house.owner;
std::cout << " is currently living in their ";
switch (house.color) {
case red:
std::cout << "red";
break;
case green:
std::cout << "green";
break;
case blue:
std::cout << "blue";
break;
case black:
std::cout << "black";
break;
case white:
std::cout << "white";
break;
default:
std::cout << "mystey-colored";
break;
}
std::cout << " house at number " << house.number << std::endl;
}
int main(int argc, char** argv) {
using std::string;
using std::cout;
using std::cin;
using std::endl;
using namespace city;
// Declare a variable to hold the name of the user
string owner;
// Write out static text
cout << "Dear new resident of the city. Please tell us your name:" << endl;
// An infinite loop
while (1) {
// Read in a string
cin >> owner;
// 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;
// Skip to next run of loop
continue;
}
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
break;
}
}
// Let's ask the user for a color of the house
cout << "Which color should your house have? (r|g|b|w)" << endl;
HouseColor houseColor;
char color;
while (1) {
// Read one character from the input
cin >> color;
// Discard the rest of the line
cin.ignore();
// Add a variable to keep track of an error
bool unknown_color = false;
// Handle different cases of the entered color
switch (color) {
case 'r':
houseColor = red;
break;
case 'g':
houseColor = green;
break;
case 'b':
houseColor = blue;
break;
case 'w':
houseColor = white;
break;
default:
unknown_color = true;
break;
}
// 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;
// Retry
continue;
}
else {
cout << "The color you chose was " << color << ". Your house will be painted in this color." << endl;
// End loop
break;
}
}
cout << "Do you have a favorite house number?" << endl;
// Define a variable for the house number
int32_t house_number;
// Another infinite loop for the reading of the house number
while (1) {
// Read the number
cin >> house_number;
if (house_number <= 0) {
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;
// Stop the trying loop
break;
}
}
// Declare a house of custom type and assign its member fields.
House designed_house;
designed_house.color = houseColor;
designed_house.number = house_number;
designed_house.owner = owner;
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;
copy_repaint(designed_house);
describe_house(designed_house);
std::cout << "Let us try it again a little differently:" << endl;
copy_free_repaint(designed_house);
describe_house(designed_house);
return 0;
}
\ No newline at end of file
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
string owner;
// Write out static text
cout << "Dear new resident of the city. Please tell us your name:" << endl;
// An infinite loop
while (1) {
// Read in a string
cin >> owner;
// 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;
// Skip to next run of loop
continue;
} 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
break;
}
}
// Let's ask the user for a color of the house
cout << "Which color should your house have? (r|g|b|w)" << endl;
HouseColor houseColor;
char color;
while (1) {
// Read one character from the input
cin >> color;
// Discard the rest of the line
cin.ignore();
// Add a variable to keep track of an error
bool unknown_color = false;
// Handle different cases of the entered color
switch (color) {
case 'r':
houseColor = red;
break;
case 'g':
houseColor = green;
break;
case 'b':
houseColor = blue;
break;
case 'w':
houseColor = white;
break;
default:
unknown_color = true;
break;
}
// 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;
// Retry
continue;
} else {
cout << "The color you chose was " << color
<< ". Your house will be painted in this color." << endl;
// End loop
break;
}
}
cout << "Do you have a favorite house number?" << endl;
// Define a variable for the house number
int32_t house_number;
// Another infinite loop for the reading of the house number
while (1) {
// Read the number
cin >> house_number;
if (house_number <= 0) {
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;
// Stop the trying loop
break;
}
}
// Declare a house of custom type and assign its member fields.
House designed_house;
designed_house.color = houseColor;
designed_house.number = house_number;
designed_house.owner = owner;
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.
Finish editing this message first!
Please register or to comment