Homework 1, FizzBuzz

Motivation

The purpose of this assignment is to write a C++ program that demonstrates a basic understanding of programming using C++. The core concepts demonstrated by this assignment are the handling of command line arguments, looping, and conditionals. These are all concepts that you are expected to have already learned. I encourage you to attempt to complete this assignment without consulting any outside resources. However, should you need to look something up, please make a mental note of the topic in order to track your progress toward mastery of the topic.

Description

This assignment requires you to write a program, fizzbuzz.cpp, that produces a single-line output for each number, i, from 1 up to and including the given number NUM. Each line of output is one of the following:

  • i when i is not divisible by 3 and not divisible by 5
  • fizz when i is divisible by 3
  • buzz when i is divisible by 5
  • fizzbuzz when i is divisible by both 3 and 5

The number, NUM, should be read in as the first command line argument. Your program should check for the following error cases and produce the associated error output message. Note that each error message should be followed by a newline:

  • The number of command line arguments is not 1: Usage: fizzbuzz NUM
  • The number, NUM is too small to produce any output: NUM is too small

Keyword Restrictions

Given that this is a class in C++, you will be expected to write C++ specific code. While C++ is backwards compatible with C, you will be expected to use C++’s specific conventions over their C counterparts in your assignments for this class. This includes cout and cin over printf and scanf, as well as new and delete over malloc and free.

Compiling and running your program

In this class we will use the clang++ compiler for all assignments. The clang++ compiler has a number of benefits over the g++ compiler, most notably with respect to the usefulness of its error and warning messages.

To compile the project:

clang++ fizzbuzz.cpp

To run the project:

./a.out NUM

Example input and corresponding output

No command line arguments:

$ ./a.out
Usage: fizzbuzz NUM

Negative value command line argument:

$ ./a.out -1024
NUM is too small

A valid input:

$ ./a.out 15
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz

Submitting the project

~cs32/submit hw1@cs32 fizzbuzz.cpp

Please review the automated feedback instructions as needed.

See also

 

Template design by Andreas Viklund