Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
coding-streams
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
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
coding_tug
coding-streams
Commits
5600e939
Commit
5600e939
authored
4 years ago
by
Alexander Steinmaurer
Browse files
Options
Downloads
Plain Diff
Merge branch 'master' of
https://gitlab.tugraz.at/coding_tug/coding-streams
parents
04a640c8
e17b59a2
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
2021-oop1/ku/stream-05-19-2021/input_parsing.cpp
+89
-0
89 additions, 0 deletions
2021-oop1/ku/stream-05-19-2021/input_parsing.cpp
with
89 additions
and
0 deletions
2021-oop1/ku/stream-05-19-2021/input_parsing.cpp
0 → 100644
+
89
−
0
View file @
5600e939
#include
<iostream>
#include
<algorithm>
// VALID COMMANDS:
// "quit"
// "set x"
enum
Command
{
QUIT
,
SET
};
std
::
string
readInput
()
{
std
::
string
input_string
;
std
::
cout
<<
"> "
;
std
::
getline
(
std
::
cin
,
input_string
);
if
(
input_string
.
length
()
>
10
)
throw
std
::
length_error
(
"[ERROR] Input too long"
);
if
(
input_string
.
length
()
<
4
)
throw
std
::
length_error
(
"[ERROR] Input too short"
);
std
::
transform
(
input_string
.
begin
(),
input_string
.
end
(),
input_string
.
begin
(),[](
unsigned
char
c
){
return
std
::
tolower
(
c
);
});
return
input_string
;
}
size_t
validateAndGetNrOfWords
(
std
::
string
&
input
)
{
size_t
nr_of_words
=
std
::
count
(
input
.
cbegin
(),
input
.
cend
(),
' '
)
+
1
;
if
(
nr_of_words
!=
1
&&
nr_of_words
!=
2
)
throw
std
::
out_of_range
(
"[ERROR] too many words"
);
return
nr_of_words
;
}
Command
getValidCommand
(
std
::
string
&
input
)
{
size_t
nr_of_words
=
validateAndGetNrOfWords
(
input
);
if
(
nr_of_words
==
1
)
{
if
(
input
==
"quit"
)
return
Command
::
QUIT
;
else
throw
std
::
invalid_argument
(
"[ERROR] invalid command with one word"
);
}
else
{
if
(
input
==
"set x"
)
return
Command
::
SET
;
else
throw
std
::
invalid_argument
(
"[ERROR] invalid command with two words"
);
}
}
Command
getCommand
()
{
while
(
true
)
{
try
{
std
::
string
input
=
readInput
();
Command
current_command
=
getValidCommand
(
input
);
return
current_command
;
}
catch
(
std
::
length_error
&
e
)
{
std
::
cout
<<
e
.
what
()
<<
std
::
endl
;
}
catch
(
std
::
out_of_range
&
e
)
{
std
::
cout
<<
e
.
what
()
<<
std
::
endl
;
}
catch
(
std
::
invalid_argument
&
e
)
{
std
::
cout
<<
e
.
what
()
<<
std
::
endl
;
}
}
}
int
main
()
{
std
::
cout
<<
"Insane OOP Game - enter set x or quit"
<<
std
::
endl
;
Command
current_command
=
SET
;
while
(
current_command
!=
QUIT
)
{
current_command
=
getCommand
();
std
::
cout
<<
(
current_command
==
SET
?
"SET X COMMAND"
:
"QUIT COMMAND"
)
<<
std
::
endl
;
}
return
0
;
}
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