Lab 14: Introduction to C++
Due by the end of class
Introduction
The goal of this lab is to introduce you to C++. C++ is an object-oriented language, but we will not use any of the OOP features in this lab. For this lab, use the C++ style input and output objects cin
and cout
instead of scanf()
and printf()
. Also, you must use string
instead of char[]
or char*
variables to hold string data. Don't worry! Using string
makes the lab easier. Please use g++
to compile your code instead of gcc
.
Lab Exercise
Computer scientists love numbers. How could we get through the day without them? Typically, it's easier to represent numbers as base 10 numerals like 281. However, our colleagues in other disciplines may prefer to write numbers out in their English equivalents, such as "two hundred eighty one" in this case.
Your job is to write a C++ program that will read in a number in decimal and convert it into the equivalent English version.
You can save the program in any file you want, but its name must end with .cpp
instead of .c
. When you've completed your code, create a makefile which will compile it into an executable called numbers
. This makefile will be formatted exactly like all the other makefiles you've made. The only difference is that you will replace gcc
with g++
.
C++ Considerations
First, make sure that you have the following lines at the very top of your source code file.
#include <iostream> #include <string> using namespace std;
You'll use cin
to read in a number as an int
and then cout
to print out the English version as a string
once you're done generating it. Refer to the lecture notes for information on how to use cin
and cout
.
The string
class is a powerful class that is similar to the String
class in Java. You can store a string literal directly into objects of this type. You can even use +
and +=
to concatenate string
objects together with each other or with string literals. The one thing you can't do is concatenate two string literals together.
string word = "hey"; // Legal word += " "; // Legal string otherWord = "playa"; // Legal word += otherWord; // Legal (word contains "hey playa" now) otherWord = "help " + "me"; // Illegal (+ is not permitted between literals)
Attacking the Problem
Once you've read in the number, I recommend finding out what value is in the ones, the tens, and the hundreds place. You can easily do so with the division and modulus operators. For this lab, all numbers are guaranteed to be in the range 0-999, inclusive.
Think about how you should combine these values into a final number. I found it helpful to create three helper functions that return an appropriate string
object. These functions are prototyped below.
string getName(int value); string getTens(int value); string getTeens(int value);
Out of my incredibly generous heart, I'm even providing one of these for you below. It returns the name of a digit. Note that this function can be useful in assembling the name for both the ones place and the hundreds place.
string getName(int value) { switch( value ) { case 1: return "one"; case 2: return "two"; case 3: return "three"; case 4: return "four"; case 5: return "five"; case 6: return "six"; case 7: return "seven"; case 8: return "eight"; case 9: return "nine"; } return ""; }
You will need to write the other two functions yourself as well as the appropriate code to use them. The zero case is a special case, but there are many others, including those that involve the so-called teens, from 11 to 19.
Sample Output
Below are several examples of the output your program should have. I have provided many examples since there are a number of special cases to watch out for. As usual, user input is given in green
.
Enter a number: (0-999) 76 Your number in words: seventy six
Enter a number: (0-999) 300 Your number in words: three hundred
Enter a number: (0-999) 429 Your number in words: four hundred twenty nine
Enter a number: (0-999) 13 Your number in words: thirteen
Enter a number: (0-999) 117 Your number in words: one hundred seventeen
Enter a number: (0-999) 905 Your number in words: nine hundred five
Enter a number: (0-999) 0 Your number in words: zero
Turn In
Zip the contents of your lab directory, including the makefile and the source C++ files. Upload this zip file to Brightspace. Do not include any object files or executables. Running the make
command must compile the required C++ source code files and generate an executable named numbers
.
All work must be done individually. Never look at someone else's code. Please refer to the course policies if you have any questions about academic integrity. If you have trouble with the assignment, I am always available for assistance.