Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cpp_introduction
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cp1_ws19
cpp_introduction
Commits
9f574587
Commit
9f574587
authored
5 years ago
by
Kevin Höllring
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/sample_3.cpp
+166
-0
166 additions, 0 deletions
src/sample_3.cpp
src/sample_3.h
+37
-0
37 additions, 0 deletions
src/sample_3.h
with
203 additions
and
0 deletions
src/sample_3.cpp
0 → 100644
+
166
−
0
View file @
9f574587
#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
This diff is collapsed.
Click to expand it.
src/sample_3.h
0 → 100644
+
37
−
0
View file @
9f574587
#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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment