Home

Awesome

correct_cpp_bool_to_coin

BranchTravis CICodecov
masterBuild Statuscodecov.io

Correct C++ chapter 'Hello CLI'.

Goal

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_coinOutputExit status
./bool_to_coinAny1
./bool_to_coin trueheads (with newline)0
./bool_to_coin falsetails (with newline)0
./bool_to_coin nonsenseAny1
./bool_to_coin true trueAny1

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