Selection
This page is under construction. Please come back later.
#include <iostream> #include <string> using namespace std; int main(int argc, char **argv) { cout << "Start..." << endl; if (argc != 3) { cout << "You need at least two command line arguments!" << endl; } cout << "Finish..." << endl; return 0; }
Output
$ g++ -std=c++17 Selection1.cpp -o Selection1 -lfmt
$ ./Selection1 abc
Start...
You need at least two command line arguments!
Finish...
$ g++ -std=c++17 Selection1.cpp -o Selection1 -lfmt
$ ./Selection1 abc 123
Start...
Finish...
#include "Utils.hpp" #include <iostream> #include <string> using namespace std; int main(int argc, char **argv) { int const x = Utils::stoiWithDefault(string(argv[1]), 0); cout << "Start..." << endl; if (x > 10) { cout << "Greater than 10" << endl; } else { cout << "Less than or equal to 10" << endl; } cout << "Finish..." << endl; return 0; }
Output
$ g++ -std=c++17 Selection2.cpp -o Selection2 -lfmt
$ ./Selection2 8
Start...
Less than or equal to 10
Finish...
$ g++ -std=c++17 Selection2.cpp -o Selection2 -lfmt
$ ./Selection2 12
Start...
Greater than 10
Finish...
#include "Utils.hpp" #include <iostream> #include <string> using namespace std; int main(int argc, char **argv) { int const x = Utils::stoiWithDefault(string(argv[1]), 0); cout << "Start..." << endl; if (x > 1000) { cout << "Greater than 1000" << endl; } else if (x > 100) { cout << "Greater than 100 but less than or equal to 1000" << endl; } else { cout << "Less than or equal to 100" << endl; } cout << "Finish..." << endl; return 0; }
Output
$ g++ -std=c++17 Selection3.cpp -o Selection3 -lfmt
$ ./Selection3 12
Start...
Less than or equal to 100
Finish...
$ g++ -std=c++17 Selection3.cpp -o Selection3 -lfmt
$ ./Selection3 120
Start...
Greater than 100 but less than or equal to 1000
Finish...
$ g++ -std=c++17 Selection3.cpp -o Selection3 -lfmt
$ ./Selection3 1200
Start...
Greater than 1000
Finish...
#include "Utils.hpp" #include <iostream> #include <string> using namespace std; int main(int argc, char **argv) { int const x = Utils::stoiWithDefault(string(argv[1]), 0); switch (x) { case 1: cout << "One" << endl; break; case 2: cout << "Two" << endl; break; case 3: case 4: cout << "Three or Four" << endl; break; default: cout << "Just don't know!" << endl; break; } return 0; }
Output
$ g++ -std=c++17 Selection4.cpp -o Selection4 -lfmt
$ ./Selection4 0
Just don't know!
$ g++ -std=c++17 Selection4.cpp -o Selection4 -lfmt
$ ./Selection4 1
One
$ g++ -std=c++17 Selection4.cpp -o Selection4 -lfmt
$ ./Selection4 2
Two
$ g++ -std=c++17 Selection4.cpp -o Selection4 -lfmt
$ ./Selection4 3
Three or Four
$ g++ -std=c++17 Selection4.cpp -o Selection4 -lfmt
$ ./Selection4 4
Three or Four
#include "Utils.hpp" #include <cctype> #include <fmt/format.h> #include <iostream> #include <string> using namespace std; int main(int argc, char **argv) { char x = string(argv[1])[0]; x = (char)tolower(x); switch (x) { case 'a': case 'e': case 'i': case 'o': case 'u': cout << fmt::format("{0:c} is a vowel", int(x)) << endl; break; case 'y': cout << "y is sometimes a vowel" << endl; break; default: cout << fmt::format("{0:c} is a consonant", int(x)) << endl; break; } return 0; }
Output
$ g++ -std=c++17 Selection5.cpp -o Selection5 -lfmt
$ ./Selection5 a
a is a vowel
$ g++ -std=c++17 Selection5.cpp -o Selection5 -lfmt
$ ./Selection5 g
g is a consonant
$ g++ -std=c++17 Selection5.cpp -o Selection5 -lfmt
$ ./Selection5 I
i is a vowel
$ g++ -std=c++17 Selection5.cpp -o Selection5 -lfmt
$ ./Selection5 T
t is a consonant
$ g++ -std=c++17 Selection5.cpp -o Selection5 -lfmt
$ ./Selection5 y
y is sometimes a vowel
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- FICA Taxes (Revisited)
- FICA Taxes (Empoyee and Employer)
- Football Passer Rating (NFL and NCAA)
- Income Tax (Single Taxpayer)
- Income Tax
- Pythagorean Theorem
- Retirement Calculator
References
-
[[C++ Programming Language]], 4th Edition, Bjarne Stroustrup, Addison-Wesley, 2013, ISBN 978-0321563842.
- [[C++ Language Reference]]
- [[cplusplus.com]]
- [[Cprogramming.com]]