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

Add sample with custom types and more complex logic, reading and writing data

parent 97a0a4bd
No related branches found
No related tags found
No related merge requests found
#include <iostream>
#include "sample_3.h"
void city::copy_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;
}
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
#ifndef __SAMPLE_3_H__
#define __SAMPLE_3_H__
#include <string>
namespace city {
// A custom type with a fixed number of permitted values
enum HouseColor {
red,
blue,
green,
white,
black
};
// A custom type with data fields
struct House {
// Our custom color
HouseColor color;
// A house number
uint32_t number;
// The name of the owner
std::string owner;
};
// A function to illustrate call by value
void copy_repaint(House torepaint);
// A function to illustrate call by referene
void copy_free_repaint(House &torepaint);
// A function to print out a description of the house
void describe_house(House &house);
};
#endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment