Awesome
correct_cpp_int_to_word
Branch | Travis CI | Codecov |
---|---|---|
master |
Correct C++ chapter 'Hello CLI'.
Goal
- Write an application with 100% code coverage
Prerequisites
- Understand how this course works
- Have written a correct 'is_odd' program
Exercise
Write a command-line interface (CLI) program that convert the number 1 to (and including 6) to their words, followed by a newline.
Fail if the user supplies no, two or more arguments
Call to hello_cli | Output | Exit status |
---|---|---|
./int_to_word | Any | 1 |
./int_to_word 1 | one (with newline) | 0 |
./int_to_word 2 | two (with newline) | 0 |
./int_to_word 3 | three (with newline) | 0 |
./int_to_word 4 | four (with newline) | 0 |
./int_to_word 5 | five (with newline) | 0 |
./int_to_word 6 | six (with newline) | 0 |
./int_to_word nonsense | Any | 1 |
./int_to_word 1 2 | Any | 1 |
This is the code you start with:
#include <iostream>
#include <string>
#include <vector>
int main(int argc, char* argv[])
{
if (argc != 2)
{
return 1;
}
try
{
const int i{std::stoi(argv[1])};
switch (i)
{
case 1: std::cout << "one\n"; break;
case 2: std::cout << "two\n"; break;
case 3: std::cout << "three\n"; break;
case 4: std::cout << "four\n"; break;
case 5: std::cout << "five\n"; break;
case 6: std::cout << "six\n"; break;
default: throw std::invalid_argument("i must be in range [1, 6]");
}
}
catch (const std::exception&)
{
return 1;
}
}
- Your code needs to have 100% code coverage, regardless how it is called (that is, with zero, one or more arguments), see how to get 100 percent code coverage
External links
- [none]