Hide

Problem A
Hello World

You shall upload a single source file named hello.cpp, containing the definition of two functions;

  • parse_args (int argc, char * argv[]), and;

  • hello (char const * name, int count).

Requirements

  • parse_args is responsible for parsing/interpreting the command line arguments passed to the application, and shall return a std::pair with what to print, and how many times to print it.

    parse_args shall handle all input errors by returning -1 as the second value of the returned std::pair, as well as printing a suitable error message on stderr.

  • hello is responsible for printing the hello world-string on stdout with the following semantics;

    • If the value zero is passed as the second parameter, nothing shall be printed.

    • If a value greater than zero is passed as the second argument (count), "Hello, " shall be printed followed by count space-delimited occurrences of the first argument.

      The output shall end with an exclamation mark, followed by a new-line.

      Note: hello ("KTH", 3) shall print Hello, KTH KTH KTH! (see sample invocations of an example program below) -

  • Correct output from your program shall be printed through std::cout, whereas potential error diagnostics (detected by parse_args) shall be printed through std::cerr. The error messages shall start with the word “error”, followed by the error message.

Sample output

 
    > ./hello
    Hello, world!
    > ./hello "DD1387"
    Hello, DD1387!
    > ./hello "KTH" 3
    Hello, KTH KTH KTH!
    > ./hello "KTH" 0
    > ./hello "KTH" 2
    Hello, KTH KTH!
    > ./hello "Malcom X" NaN
    error: 2nd argument must be an integral greater than zero!
    > ./hello kth dd1387 3
    error: Too many arguments!

Please log in to submit a solution to this problem

Log in