/correct_cpp_bool_to_coin

Correct C++ chapter 'bool_to_coin'

Primary LanguageC++GNU General Public License v3.0GPL-3.0

correct_cpp_bool_to_coin

Branch Travis CI Codecov
master Build Status codecov.io

Correct C++ chapter 'Hello CLI'.

Goal

  • Write an application with 100% code coverage

Prerequisites

Exercise

Write a command-line interface (CLI) program that converts a boolean (true or false) to a coin's side (heads or tails respectively), followed by a newline. Fail if the user supplies no, two or more arguments.

Call to bool_to_coin Output Exit status
./bool_to_coin Any 1
./bool_to_coin true heads (with newline) 0
./bool_to_coin false tails (with newline) 0
./bool_to_coin nonsense Any 1
./bool_to_coin true true Any 1

This is the code you start with:

#include <iostream>
#include <string>

int main(int argc, char* argv[]) 
{
  if (argc != 2) 
  {
    return 1;
  }
  if (std::string(argv[1]) == "true") 
  { 
    std::cout << "heads\n";   
  }
  else if (std::string(argv[1]) == "false") 
  { 
    std::cout << "tails\n"; 
  }
  else 
  {
    return 1;
  }
}

External links

  • [none]