larry and jen do roman numerals in c++

438
Larry and Jen do Roman Numerals in C++ by Jon Jagger and Olve Maudal http://www.slideshare.net/olvemaudal/deep-c Larry and Jen first appeared in the popular Deep C (and C++) presentation

Upload: jon-jagger

Post on 25-Jun-2015

7.494 views

Category:

Technology


3 download

DESCRIPTION

Larry and Jen from the popular Deep C (and C++) slide-deck are back. This time they practice C++ by doing do the Roman Numeral conversion problem.

TRANSCRIPT

Page 1: Larry and Jen do Roman Numerals in C++

Larry and Jen do Roman Numerals

in C++

by Jon Jagger and Olve Maudal

http://www.slideshare.net/olvemaudal/deep-c

Larry and Jen first appeared in the popular Deep C (and C++) presentation

Page 2: Larry and Jen do Roman Numerals in C++

Jen, will you help me do some C++ practice?

Page 3: Larry and Jen do Roman Numerals in C++

Jen, will you help me do some C++ practice?

Sure Larry.

Page 4: Larry and Jen do Roman Numerals in C++

Given a positive integer number (eg 42) determineits Roman numeral representation as a String (eg "XLII").The Roman 'letters' are:

1 -> "I" | 10 -> "X" | 100 -> "C" | 1000 -> "M"2 -> "II" | 20 -> "XX" | 200 -> "CC" | 2000 -> "MM"3 -> "III" | 30 -> "XXX" | 300 -> "CCC" | 3000 -> "MMM"4 -> "IV" | 40 -> "XL" | 400 -> "CD" | 4000 -> "MMMM"5 -> "V" | 50 -> "L" | 500 -> "D"6 -> "VI" | 60 -> "LX" | 600 -> "DC" 7 -> "VII" | 70 -> "LXX" | 700 -> "DCC" 8 -> "VIII" | 80 -> "LXXX" | 800 -> "DCCC" 9 -> "IX" | 90 -> "XC" | 900 -> "CM"

You cannot write numerals like IM for 999.Wikipedia states "Modern Roman numerals are written byexpressing each digit separately starting with theleftmost digit and skipping any digit with a value of zero."

Examples:o) 1990 -> "MCMXC" (1000 -> "M" + 900 -> "CM" + 90 -> "XC")o) 2008 -> "MMVIII" (2000 -> "MM" + 8 -> "VIII")o) 99 -> "XCIX" ( 90 -> "XC" + 9 -> "IX")o) 47 -> "XLVII" ( 40 -> "XL" + 7 -> "VII")

How about the roman numerals kata?

Page 5: Larry and Jen do Roman Numerals in C++

Given a positive integer number (eg 42) determineits Roman numeral representation as a String (eg "XLII").The Roman 'letters' are:

1 -> "I" | 10 -> "X" | 100 -> "C" | 1000 -> "M"2 -> "II" | 20 -> "XX" | 200 -> "CC" | 2000 -> "MM"3 -> "III" | 30 -> "XXX" | 300 -> "CCC" | 3000 -> "MMM"4 -> "IV" | 40 -> "XL" | 400 -> "CD" | 4000 -> "MMMM"5 -> "V" | 50 -> "L" | 500 -> "D"6 -> "VI" | 60 -> "LX" | 600 -> "DC" 7 -> "VII" | 70 -> "LXX" | 700 -> "DCC" 8 -> "VIII" | 80 -> "LXXX" | 800 -> "DCCC" 9 -> "IX" | 90 -> "XC" | 900 -> "CM"

You cannot write numerals like IM for 999.Wikipedia states "Modern Roman numerals are written byexpressing each digit separately starting with theleftmost digit and skipping any digit with a value of zero."

Examples:o) 1990 -> "MCMXC" (1000 -> "M" + 900 -> "CM" + 90 -> "XC")o) 2008 -> "MMVIII" (2000 -> "MM" + 8 -> "VIII")o) 99 -> "XCIX" ( 90 -> "XC" + 9 -> "IX")o) 47 -> "XLVII" ( 40 -> "XL" + 7 -> "VII")

How about the roman numerals kata?

ok

Page 6: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

int main(){ std::cout << "All tests passed" << std::endl;}

to_roman.tests.cppI'll start with a test.1 should be I.

Page 7: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

int main(){ std::cout << "All tests passed" << std::endl;}

to_roman.tests.cppI'll start with a test.1 should be I.#include "to_roman.hpp"

#include <cassert>#include <iostream>

int main(){ std::cout << "All tests passed" << std::endl;}

Page 8: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

int main(){ std::cout << "All tests passed" << std::endl;}

to_roman.tests.cppI'll start with a test.1 should be I.#include "to_roman.hpp"

#include <cassert>#include <iostream>

int main(){ std::cout << "All tests passed" << std::endl;}

static void test_1_is_I(){ assert(to_roman(1) == "I");}

Page 9: Larry and Jen do Roman Numerals in C++

Here's my header file, to_roman.hpp

Page 10: Larry and Jen do Roman Numerals in C++

#ifndef TO_ROMAN_INCLUDED#define TO_ROMAN_INCLUDED

#include <string>

std::string to_roman(int number);

#endif

Here's my header file, to_roman.hpp

Page 11: Larry and Jen do Roman Numerals in C++

#ifndef TO_ROMAN_INCLUDED#define TO_ROMAN_INCLUDED

#include <string>

std::string to_roman(int number);

#endif

Here's my header file, to_roman.hpp

Here's my source file, to_roman.cpp

Page 12: Larry and Jen do Roman Numerals in C++

#ifndef TO_ROMAN_INCLUDED#define TO_ROMAN_INCLUDED

#include <string>

std::string to_roman(int number);

#endif

Here's my header file, to_roman.hpp

Here's my source file, to_roman.cpp

#include "to_roman.hpp"

std::string to_roman(int number){ return "";}

Page 13: Larry and Jen do Roman Numerals in C++

#ifndef TO_ROMAN_INCLUDED#define TO_ROMAN_INCLUDED

#include <string>

std::string to_roman(int number);

#endif

Here's my header file, to_roman.hpp

ok

Here's my source file, to_roman.cpp

#include "to_roman.hpp"

std::string to_roman(int number){ return "";}

Page 14: Larry and Jen do Roman Numerals in C++

How are you building and running?

Page 15: Larry and Jen do Roman Numerals in C++

How are you building and running?

With this.

Page 16: Larry and Jen do Roman Numerals in C++

How are you building and running?

$ cc *.cpp && ./a.out

With this.

Page 17: Larry and Jen do Roman Numerals in C++

How are you building and running?

$ cc *.cpp && ./a.out

With this.

Try it.

Page 18: Larry and Jen do Roman Numerals in C++

How are you building and running?

$ cc *.cpp && ./a.out

With this.

Try it.

$ cc *.cpp && ./a.outAll tests passed

Page 19: Larry and Jen do Roman Numerals in C++

How are you building and running?

$ cc *.cpp && ./a.out

With this.

Try it.

$ cc *.cpp && ./a.outAll tests passed

Eh? That's not right. It should have failed!

Page 20: Larry and Jen do Roman Numerals in C++

How are you building and running?

$ cc *.cpp && ./a.out

With this.

Try it.

$ cc *.cpp && ./a.outAll tests passed

Eh? That's not right. It should have failed!

Let's see the tests again.

Page 21: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ std::cout << "All tests passed" << std::endl;}

Hmmmm.

Page 22: Larry and Jen do Roman Numerals in C++

Of course. We're not calling the test! #include "to_roman.hpp"

#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ std::cout << "All tests passed" << std::endl;}

Hmmmm.

Page 23: Larry and Jen do Roman Numerals in C++

Of course. We're not calling the test! #include "to_roman.hpp"

#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ std::cout << "All tests passed" << std::endl;}

Hmmmm.

That's easy to fix.

Page 24: Larry and Jen do Roman Numerals in C++

Of course. We're not calling the test! #include "to_roman.hpp"

#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ std::cout << "All tests passed" << std::endl;}

Hmmmm.

That's easy to fix.

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){

std::cout << "All tests passed" << std::endl;}

Page 25: Larry and Jen do Roman Numerals in C++

Of course. We're not calling the test! #include "to_roman.hpp"

#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ std::cout << "All tests passed" << std::endl;}

Hmmmm.

That's easy to fix.

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){

std::cout << "All tests passed" << std::endl;}

test_1_is_I();

Page 26: Larry and Jen do Roman Numerals in C++

Of course. We're not calling the test! #include "to_roman.hpp"

#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ std::cout << "All tests passed" << std::endl;}

Hmmmm.

$ cc *.cpp && ./a.out

That's easy to fix.

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){

std::cout << "All tests passed" << std::endl;}

test_1_is_I();

Page 27: Larry and Jen do Roman Numerals in C++

Of course. We're not calling the test! #include "to_roman.hpp"

#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ std::cout << "All tests passed" << std::endl;}

Hmmmm.

$ cc *.cpp && ./a.out$ cc *.cpp && ./a.outAssertion failed: (to_roman(1) == "I")

That's easy to fix.

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){

std::cout << "All tests passed" << std::endl;}

test_1_is_I();

Page 28: Larry and Jen do Roman Numerals in C++

Of course. We're not calling the test! #include "to_roman.hpp"

#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ std::cout << "All tests passed" << std::endl;}

Hmmmm.

$ cc *.cpp && ./a.out$ cc *.cpp && ./a.outAssertion failed: (to_roman(1) == "I")

That's easy to fix.

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){

std::cout << "All tests passed" << std::endl;}

test_1_is_I();

Now it fails.

Page 29: Larry and Jen do Roman Numerals in C++

I can simply change the the return value to

make it pass.

#include "to_roman.hpp"

std::string to_roman(int number){ return "";}

Page 30: Larry and Jen do Roman Numerals in C++

I can simply change the the return value to

make it pass.

#include "to_roman.hpp"

std::string to_roman(int number){ return "I";}

Page 31: Larry and Jen do Roman Numerals in C++

I can simply change the the return value to

make it pass.

$ cc *.cpp && ./a.out

#include "to_roman.hpp"

std::string to_roman(int number){ return "I";}

Page 32: Larry and Jen do Roman Numerals in C++

I can simply change the the return value to

make it pass.

$ cc *.cpp && ./a.out$ cc *.cpp && ./a.out All tests passed

#include "to_roman.hpp"

std::string to_roman(int number){ return "I";}

Page 33: Larry and Jen do Roman Numerals in C++

I can simply change the the return value to

make it pass.

$ cc *.cpp && ./a.out$ cc *.cpp && ./a.out All tests passed

ok

#include "to_roman.hpp"

std::string to_roman(int number){ return "I";}

Page 34: Larry and Jen do Roman Numerals in C++

Let's write another test. 2 should be II

Page 35: Larry and Jen do Roman Numerals in C++

I think there is a problem we should fix first.

Let's write another test. 2 should be II

Page 36: Larry and Jen do Roman Numerals in C++

I think there is a problem we should fix first.

Let's write another test. 2 should be II

What problem?

Page 37: Larry and Jen do Roman Numerals in C++

I think there is a problem we should fix first.

Let's write another test. 2 should be II

What problem?

We've fixed the immediate issue by calling the function. But what happens the next

time we forget?

Page 38: Larry and Jen do Roman Numerals in C++

I think there is a problem we should fix first.

Let's write another test. 2 should be II

What problem?

We've fixed the immediate issue by calling the function. But what happens the next

time we forget?

What do you suggest?

Page 39: Larry and Jen do Roman Numerals in C++

Could the compiler detect when we have a function that is defined

but not used?

Page 40: Larry and Jen do Roman Numerals in C++

Could the compiler detect when we have a function that is defined

but not used?

A command line option?

Page 41: Larry and Jen do Roman Numerals in C++

Yes. It's one of the smells the compiler can detect with the

-Wall flag.

Could the compiler detect when we have a function that is defined

but not used?

A command line option?

Page 42: Larry and Jen do Roman Numerals in C++

Yes. It's one of the smells the compiler can detect with the

-Wall flag.

ok. Let me try that.

Could the compiler detect when we have a function that is defined

but not used?

A command line option?

Page 43: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

I'll comment out the call

Page 44: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

I'll comment out the call #include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ /test_1_is_I(); std::cout << "All tests passed" << std::endl;}

Page 45: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

I'll comment out the call #include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ /test_1_is_I(); std::cout << "All tests passed" << std::endl;}

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ //test_1_is_I(); std::cout << "All tests passed" << std::endl;}

Page 46: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

I'll comment out the call

and add the -Wall flag

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ /test_1_is_I(); std::cout << "All tests passed" << std::endl;}

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ //test_1_is_I(); std::cout << "All tests passed" << std::endl;}

Page 47: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

I'll comment out the call

$ cc *.cpp && ./a.out

and add the -Wall flag

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ /test_1_is_I(); std::cout << "All tests passed" << std::endl;}

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ //test_1_is_I(); std::cout << "All tests passed" << std::endl;}

Page 48: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

I'll comment out the call

$ cc *.cpp && ./a.out

and add the -Wall flag

$ cc *.cpp && ./a.out

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ /test_1_is_I(); std::cout << "All tests passed" << std::endl;}

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ //test_1_is_I(); std::cout << "All tests passed" << std::endl;}

Page 49: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

I'll comment out the call

$ cc *.cpp && ./a.out

and add the -Wall flag

$ cc *.cpp && ./a.out

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ /test_1_is_I(); std::cout << "All tests passed" << std::endl;}

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ //test_1_is_I(); std::cout << "All tests passed" << std::endl;}

-Wall

Page 50: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

I'll comment out the call

$ cc *.cpp && ./a.out

and add the -Wall flag

$ cc *.cpp && ./a.out

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ /test_1_is_I(); std::cout << "All tests passed" << std::endl;}

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ //test_1_is_I(); std::cout << "All tests passed" << std::endl;}

-Wall$ cc -Wall *.cpp && ./a.outwarning: 'void test_1_is_I()' is defined but not usedAll tests passed

Page 51: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

I'll comment out the call

$ cc *.cpp && ./a.out

and add the -Wall flag

$ cc *.cpp && ./a.out

now I get the warning

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ /test_1_is_I(); std::cout << "All tests passed" << std::endl;}

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ //test_1_is_I(); std::cout << "All tests passed" << std::endl;}

-Wall$ cc -Wall *.cpp && ./a.outwarning: 'void test_1_is_I()' is defined but not usedAll tests passed

Page 52: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ //test_1_is_I(); std::cout << "All tests passed" << std::endl;}

Turning the warning into an error would be even better.

We can do that with the -Werror flag

Page 53: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ //test_1_is_I(); std::cout << "All tests passed" << std::endl;}

Turning the warning into an error would be even better.

We can do that with the -Werror flag

Agreed.

Page 54: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ //test_1_is_I(); std::cout << "All tests passed" << std::endl;}

$ cc -Wall *.cpp && ./a.out

Turning the warning into an error would be even better.

We can do that with the -Werror flag

Agreed.

Page 55: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ //test_1_is_I(); std::cout << "All tests passed" << std::endl;}

$ cc -Wall *.cpp && ./a.out

Turning the warning into an error would be even better.

We can do that with the -Werror flag

$ cc -Wall *.cpp && ./a.out

Agreed.

Page 56: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ //test_1_is_I(); std::cout << "All tests passed" << std::endl;}

$ cc -Wall *.cpp && ./a.out

Turning the warning into an error would be even better.

We can do that with the -Werror flag

$ cc -Wall *.cpp && ./a.out-Werror

Agreed.

Page 57: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ //test_1_is_I(); std::cout << "All tests passed" << std::endl;}

$ cc -Wall *.cpp && ./a.out

Turning the warning into an error would be even better.

We can do that with the -Werror flag

$ cc -Wall *.cpp && ./a.out-Werror$ cc -Wall -Werror *.cpp && ./a.outerror: 'void test_1_is_I()' is defined but not used

Agreed.

Page 58: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ //test_1_is_I(); std::cout << "All tests passed" << std::endl;}

I'll uncomment the comment so the test passes again.

Page 59: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ //test_1_is_I(); std::cout << "All tests passed" << std::endl;}

I'll uncomment the comment so the test passes again.

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ /test_1_is_I(); std::cout << "All tests passed" << std::endl;}

Page 60: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ //test_1_is_I(); std::cout << "All tests passed" << std::endl;}

I'll uncomment the comment so the test passes again.

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ /test_1_is_I(); std::cout << "All tests passed" << std::endl;}

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

Page 61: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ //test_1_is_I(); std::cout << "All tests passed" << std::endl;}

I'll uncomment the comment so the test passes again.

$ cc -Wall -Werror *.cpp && ./a.out

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ /test_1_is_I(); std::cout << "All tests passed" << std::endl;}

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

Page 62: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ //test_1_is_I(); std::cout << "All tests passed" << std::endl;}

I'll uncomment the comment so the test passes again.

$ cc -Wall -Werror *.cpp && ./a.out

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ /test_1_is_I(); std::cout << "All tests passed" << std::endl;}

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Page 63: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ //test_1_is_I(); std::cout << "All tests passed" << std::endl;}

I'll uncomment the comment so the test passes again.

Now we can write the next test.

$ cc -Wall -Werror *.cpp && ./a.out

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ /test_1_is_I(); std::cout << "All tests passed" << std::endl;}

#include "to_roman.hpp"#include <cassert>#include <iostream>

static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Page 64: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

2 should be II

Page 65: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

2 should be II

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I();

std::cout << "All tests passed" << std::endl;}

Page 66: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

2 should be II

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I();

std::cout << "All tests passed" << std::endl;}

test_2_is_II();

Page 67: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

2 should be II

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I();

std::cout << "All tests passed" << std::endl;}

test_2_is_II();

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); test_2_is_II(); std::cout << "All tests passed" << std::endl;}

Page 68: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

2 should be II

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I();

std::cout << "All tests passed" << std::endl;}

test_2_is_II();

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); test_2_is_II(); std::cout << "All tests passed" << std::endl;}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

Page 69: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

2 should be II

It should fail because to_roman still always

returns "I"

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I();

std::cout << "All tests passed" << std::endl;}

test_2_is_II();

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); test_2_is_II(); std::cout << "All tests passed" << std::endl;}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

Page 70: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

2 should be II

$ cc -Wall -Werror *.cpp && ./a.out

It should fail because to_roman still always

returns "I"

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I();

std::cout << "All tests passed" << std::endl;}

test_2_is_II();

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); test_2_is_II(); std::cout << "All tests passed" << std::endl;}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

Page 71: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

2 should be II

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outvoid test_2_is_II(): Assertion `to_roman(2) == "II"' failed.

It should fail because to_roman still always

returns "I"

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I();

std::cout << "All tests passed" << std::endl;}

test_2_is_II();

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); test_2_is_II(); std::cout << "All tests passed" << std::endl;}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

Page 72: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); std::cout << "All tests passed" << std::endl;}

2 should be II

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outvoid test_2_is_II(): Assertion `to_roman(2) == "II"' failed.

Let's make it pass.

It should fail because to_roman still always

returns "I"

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I();

std::cout << "All tests passed" << std::endl;}

test_2_is_II();

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

int main(){ test_1_is_I(); test_2_is_II(); std::cout << "All tests passed" << std::endl;}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

Page 73: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ return "I";}

How about this?

Page 74: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ return "I";}

How about this?#include "to_roman.hpp"

std::string to_roman(int number){

return "I";}

Page 75: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ return "I";}

How about this?#include "to_roman.hpp"

std::string to_roman(int number){

return "I";}

if (number == 1)

Page 76: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ return "I";}

How about this?#include "to_roman.hpp"

std::string to_roman(int number){

return "I";}

if (number == 1)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";}

Page 77: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ return "I";}

How about this?#include "to_roman.hpp"

std::string to_roman(int number){

return "I";}

if (number == 1)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";}

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";

}

Page 78: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ return "I";}

How about this?#include "to_roman.hpp"

std::string to_roman(int number){

return "I";}

if (number == 1)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";}

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";

} if (number == 2)

Page 79: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ return "I";}

How about this?#include "to_roman.hpp"

std::string to_roman(int number){

return "I";}

if (number == 1)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";}

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";

} if (number == 2)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2)

}

Page 80: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ return "I";}

How about this?#include "to_roman.hpp"

std::string to_roman(int number){

return "I";}

if (number == 1)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";}

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";

} if (number == 2)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2)

} return "II";

Page 81: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ return "I";}

How about this?

Try it!

#include "to_roman.hpp"

std::string to_roman(int number){

return "I";}

if (number == 1)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";}

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";

} if (number == 2)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2)

} return "II";

Page 82: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ return "I";}

How about this?

Try it!

$ cc -Wall -Werror *.cpp && ./a.out

#include "to_roman.hpp"

std::string to_roman(int number){

return "I";}

if (number == 1)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";}

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";

} if (number == 2)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2)

} return "II";

Page 83: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ return "I";}

How about this?

Try it!

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outerror: control reaches end of non-void function

#include "to_roman.hpp"

std::string to_roman(int number){

return "I";}

if (number == 1)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";}

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";

} if (number == 2)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2)

} return "II";

Page 84: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ return "I";}

How about this?

Try it!

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outerror: control reaches end of non-void function

Eh?

#include "to_roman.hpp"

std::string to_roman(int number){

return "I";}

if (number == 1)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";}

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";

} if (number == 2)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2)

} return "II";

Page 85: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ return "I";}

How about this?

Try it!

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outerror: control reaches end of non-void function

Eh?

Ah! It's saying that if number doesn't equal one or two then there is no

return statement.

#include "to_roman.hpp"

std::string to_roman(int number){

return "I";}

if (number == 1)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";}

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";

} if (number == 2)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2)

} return "II";

Page 86: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ return "I";}

How about this?

Try it!

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outerror: control reaches end of non-void function

Eh?

Exactly.

Ah! It's saying that if number doesn't equal one or two then there is no

return statement.

#include "to_roman.hpp"

std::string to_roman(int number){

return "I";}

if (number == 1)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";}

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";

} if (number == 2)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2)

} return "II";

Page 87: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ return "I";}

How about this?

Try it!

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outerror: control reaches end of non-void function

Eh?

Exactly.

That's easy to fix.

Ah! It's saying that if number doesn't equal one or two then there is no

return statement.

#include "to_roman.hpp"

std::string to_roman(int number){

return "I";}

if (number == 1)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";}

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I";

} if (number == 2)

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2)

} return "II";

Page 88: Larry and Jen do Roman Numerals in C++

How about this?#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2) return "II";}

Page 89: Larry and Jen do Roman Numerals in C++

How about this?#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2) return "II";}

#include "to_roman.hpp"

std::string to_roman(int number){

if (number == 1) return "I"; if (number == 2) return "II";}

Page 90: Larry and Jen do Roman Numerals in C++

How about this?#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2) return "II";}

#include "to_roman.hpp"

std::string to_roman(int number){

if (number == 1) return "I"; if (number == 2) return "II";}

std::string roman = "";

Page 91: Larry and Jen do Roman Numerals in C++

How about this?#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2) return "II";}

#include "to_roman.hpp"

std::string to_roman(int number){

if (number == 1) return "I"; if (number == 2) return "II";}

std::string roman = "";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) if (number == 2) return "II";}

Page 92: Larry and Jen do Roman Numerals in C++

How about this?#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2) return "II";}

#include "to_roman.hpp"

std::string to_roman(int number){

if (number == 1) return "I"; if (number == 2) return "II";}

std::string roman = "";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) if (number == 2) return "II";}

roman = "I";

Page 93: Larry and Jen do Roman Numerals in C++

How about this?#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2) return "II";}

#include "to_roman.hpp"

std::string to_roman(int number){

if (number == 1) return "I"; if (number == 2) return "II";}

std::string roman = "";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) if (number == 2) return "II";}

roman = "I";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2)

}

Page 94: Larry and Jen do Roman Numerals in C++

How about this?#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2) return "II";}

#include "to_roman.hpp"

std::string to_roman(int number){

if (number == 1) return "I"; if (number == 2) return "II";}

std::string roman = "";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) if (number == 2) return "II";}

roman = "I";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2)

} roman = "II";

Page 95: Larry and Jen do Roman Numerals in C++

How about this?#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2) return "II";}

#include "to_roman.hpp"

std::string to_roman(int number){

if (number == 1) return "I"; if (number == 2) return "II";}

std::string roman = "";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) if (number == 2) return "II";}

roman = "I";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2)

} roman = "II";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II";

}

Page 96: Larry and Jen do Roman Numerals in C++

How about this?#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2) return "II";}

#include "to_roman.hpp"

std::string to_roman(int number){

if (number == 1) return "I"; if (number == 2) return "II";}

std::string roman = "";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) if (number == 2) return "II";}

roman = "I";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2)

} roman = "II";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II";

} return roman;

Page 97: Larry and Jen do Roman Numerals in C++

How about this?

Looks good.

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2) return "II";}

#include "to_roman.hpp"

std::string to_roman(int number){

if (number == 1) return "I"; if (number == 2) return "II";}

std::string roman = "";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) if (number == 2) return "II";}

roman = "I";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2)

} roman = "II";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II";

} return roman;

Page 98: Larry and Jen do Roman Numerals in C++

How about this?

Looks good.

$ cc -Wall -Werror *.cpp && ./a.out

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2) return "II";}

#include "to_roman.hpp"

std::string to_roman(int number){

if (number == 1) return "I"; if (number == 2) return "II";}

std::string roman = "";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) if (number == 2) return "II";}

roman = "I";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2)

} roman = "II";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II";

} return roman;

Page 99: Larry and Jen do Roman Numerals in C++

How about this?

Looks good.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2) return "II";}

#include "to_roman.hpp"

std::string to_roman(int number){

if (number == 1) return "I"; if (number == 2) return "II";}

std::string roman = "";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) if (number == 2) return "II";}

roman = "I";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2)

} roman = "II";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II";

} return roman;

Page 100: Larry and Jen do Roman Numerals in C++

How about this?

Looks good.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Yessss.

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2) return "II";}

#include "to_roman.hpp"

std::string to_roman(int number){

if (number == 1) return "I"; if (number == 2) return "II";}

std::string roman = "";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) if (number == 2) return "II";}

roman = "I";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2)

} roman = "II";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II";

} return roman;

Page 101: Larry and Jen do Roman Numerals in C++

How about this?

Looks good.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Yessss.

Now another test.

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2) return "II";}

#include "to_roman.hpp"

std::string to_roman(int number){

if (number == 1) return "I"; if (number == 2) return "II";}

std::string roman = "";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) if (number == 2) return "II";}

roman = "I";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2)

} roman = "II";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II";

} return roman;

Page 102: Larry and Jen do Roman Numerals in C++

How about this?

Looks good.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Yessss.

Now another test.

ok

#include "to_roman.hpp"

std::string to_roman(int number){ if (number == 1) return "I"; if (number == 2) return "II";}

#include "to_roman.hpp"

std::string to_roman(int number){

if (number == 1) return "I"; if (number == 2) return "II";}

std::string roman = "";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) if (number == 2) return "II";}

roman = "I";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2)

} roman = "II";

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II";

} return roman;

Page 103: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II(); std::cout << "All tests passed" << std::endl;}

I'll add a test for 3 being III. That should fail.

Page 104: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II(); std::cout << "All tests passed" << std::endl;}

I'll add a test for 3 being III. That should fail.

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II();

std::cout << "All tests passed" << std::endl;}

Page 105: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II(); std::cout << "All tests passed" << std::endl;}

I'll add a test for 3 being III. That should fail.

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II();

std::cout << "All tests passed" << std::endl;}

test_3_is_III();

Page 106: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II(); std::cout << "All tests passed" << std::endl;}

I'll add a test for 3 being III. That should fail.

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II();

std::cout << "All tests passed" << std::endl;}

test_3_is_III();

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II(); test_3_is_III(); std::cout << "All tests passed" << std::endl;}

Page 107: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II(); std::cout << "All tests passed" << std::endl;}

I'll add a test for 3 being III. That should fail.

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II();

std::cout << "All tests passed" << std::endl;}

test_3_is_III();

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II(); test_3_is_III(); std::cout << "All tests passed" << std::endl;}

static void test_3_is_III(){ assert(to_roman(3) == "III");}

Page 108: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II(); std::cout << "All tests passed" << std::endl;}

I'll add a test for 3 being III. That should fail.

$ cc -Wall -Werror *.cpp && ./a.out

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II();

std::cout << "All tests passed" << std::endl;}

test_3_is_III();

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II(); test_3_is_III(); std::cout << "All tests passed" << std::endl;}

static void test_3_is_III(){ assert(to_roman(3) == "III");}

Page 109: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II(); std::cout << "All tests passed" << std::endl;}

I'll add a test for 3 being III. That should fail.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outvoid test_3_is_III(): Assertion `to_roman(3) == "III"' failed...Aborted

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II();

std::cout << "All tests passed" << std::endl;}

test_3_is_III();

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II(); test_3_is_III(); std::cout << "All tests passed" << std::endl;}

static void test_3_is_III(){ assert(to_roman(3) == "III");}

Page 110: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II(); std::cout << "All tests passed" << std::endl;}

I'll add a test for 3 being III. That should fail.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outvoid test_3_is_III(): Assertion `to_roman(3) == "III"' failed...Aborted

Now I'll make it pass.

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II();

std::cout << "All tests passed" << std::endl;}

test_3_is_III();

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II(); test_3_is_III(); std::cout << "All tests passed" << std::endl;}

static void test_3_is_III(){ assert(to_roman(3) == "III");}

Page 111: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II(); std::cout << "All tests passed" << std::endl;}

I'll add a test for 3 being III. That should fail.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outvoid test_3_is_III(): Assertion `to_roman(3) == "III"' failed...Aborted

Now I'll make it pass.

ok

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II();

std::cout << "All tests passed" << std::endl;}

test_3_is_III();

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

int main(){ test_1_is_I(); test_2_is_II(); test_3_is_III(); std::cout << "All tests passed" << std::endl;}

static void test_3_is_III(){ assert(to_roman(3) == "III");}

Page 112: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; return roman;}

How about this?

$ cc -Wall -Werror *.cpp && ./a.out

Page 113: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; return roman;}

How about this?

$ cc -Wall -Werror *.cpp && ./a.out

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II";

return roman;}

Page 114: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; return roman;}

How about this?

$ cc -Wall -Werror *.cpp && ./a.out

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II";

return roman;}

if (number == 3) roman = "III";

Page 115: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; return roman;}

How about this?

Aha...

$ cc -Wall -Werror *.cpp && ./a.out

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II";

return roman;}

if (number == 3) roman = "III";

Page 116: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; return roman;}

How about this?

Aha...

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II";

return roman;}

if (number == 3) roman = "III";

Page 117: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; return roman;}

How about this?

Aha...

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Yessss.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II";

return roman;}

if (number == 3) roman = "III";

Page 118: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; return roman;}

How about this?

Aha...

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Yessss.

Now another test.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II";

return roman;}

if (number == 3) roman = "III";

Page 119: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; return roman;}

How about this?

Aha...

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Yessss.

Now another test.

Hmmmm.....

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II";

return roman;}

if (number == 3) roman = "III";

Page 120: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

static void test_3_is_III(){ assert(to_roman(3) == "III");}

int main(){ test_1_is_I(); test_2_is_II(); test_3_is_III(); std::cout << "All tests passed" << std::endl;}

Are you thinking we should refactor first?

Page 121: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

static void test_3_is_III(){ assert(to_roman(3) == "III");}

int main(){ test_1_is_I(); test_2_is_II(); test_3_is_III(); std::cout << "All tests passed" << std::endl;}

Are you thinking we should refactor first?

Yes.

Page 122: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

static void test_3_is_III(){ assert(to_roman(3) == "III");}

int main(){ test_1_is_I(); test_2_is_II(); test_3_is_III(); std::cout << "All tests passed" << std::endl;}

Are you thinking we should refactor first?

Yes.

I'd start with the tests.

Page 123: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

static void test_3_is_III(){ assert(to_roman(3) == "III");}

int main(){ test_1_is_I(); test_2_is_II(); test_3_is_III(); std::cout << "All tests passed" << std::endl;}

What about the tests?

Page 124: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

static void test_3_is_III(){ assert(to_roman(3) == "III");}

int main(){ test_1_is_I(); test_2_is_II(); test_3_is_III(); std::cout << "All tests passed" << std::endl;}

What about the tests?

The name of each test repeats its body. In this case the test name adds

nothing. How about collapsing them all into a

single test.

Page 125: Larry and Jen do Roman Numerals in C++

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

static void test_3_is_III(){ assert(to_roman(3) == "III");}

int main(){ test_1_is_I(); test_2_is_II(); test_3_is_III(); std::cout << "All tests passed" << std::endl;}

What about the tests?

The name of each test repeats its body. In this case the test name adds

nothing. How about collapsing them all into a

single test.

Can you show me?

Page 126: Larry and Jen do Roman Numerals in C++

How about this.

...static void test_1_is_I(){ assert(to_roman(1) == "I");}

static void test_2_is_II(){ assert(to_roman(2) == "II");}

static void test_3_is_III(){ assert(to_roman(3) == "III");}

int main(){ test_1_is_I(); test_2_is_II(); test_3_is_III(); std::cout << "All tests passed" << std::endl;}

Page 127: Larry and Jen do Roman Numerals in C++

How about this.

...static void test_to_roman(){ assert(to_roman(1) == "I"); assert(to_roman(2) == "II"); assert(to_roman(3) == "III");}

int main(){ test_to_roman(); std::cout << "All tests passed" << std::endl;}

Page 128: Larry and Jen do Roman Numerals in C++

How about this.

$ cc -Wall -Werror *.cpp && ./a.out

...static void test_to_roman(){ assert(to_roman(1) == "I"); assert(to_roman(2) == "II"); assert(to_roman(3) == "III");}

int main(){ test_to_roman(); std::cout << "All tests passed" << std::endl;}

Page 129: Larry and Jen do Roman Numerals in C++

How about this.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

...static void test_to_roman(){ assert(to_roman(1) == "I"); assert(to_roman(2) == "II"); assert(to_roman(3) == "III");}

int main(){ test_to_roman(); std::cout << "All tests passed" << std::endl;}

Page 130: Larry and Jen do Roman Numerals in C++

How about this.

Everything still passes.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

...static void test_to_roman(){ assert(to_roman(1) == "I"); assert(to_roman(2) == "II"); assert(to_roman(3) == "III");}

int main(){ test_to_roman(); std::cout << "All tests passed" << std::endl;}

Page 131: Larry and Jen do Roman Numerals in C++

Next I'd refactor the code.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

Page 132: Larry and Jen do Roman Numerals in C++

Next I'd refactor the code.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

Yeah. An endless sequence of if statements isn't going to

be much of a solution.

Page 133: Larry and Jen do Roman Numerals in C++

Next I'd refactor the code.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

Yeah. An endless sequence of if statements isn't going to

be much of a solution.

Can I show you something neat?

Page 134: Larry and Jen do Roman Numerals in C++

Next I'd refactor the code.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

Yeah. An endless sequence of if statements isn't going to

be much of a solution.

Can I show you something neat?

Sure

Page 135: Larry and Jen do Roman Numerals in C++

First I'll refactor the code so that each if statement concatenates a single I.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "II"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

Page 136: Larry and Jen do Roman Numerals in C++

First I'll refactor the code so that each if statement concatenates a single I.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "II"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

Page 137: Larry and Jen do Roman Numerals in C++

First I'll refactor the code so that each if statement concatenates a single I.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "II"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

Page 138: Larry and Jen do Roman Numerals in C++

First I'll refactor the code so that each if statement concatenates a single I.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "II"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

Page 139: Larry and Jen do Roman Numerals in C++

First I'll refactor the code so that each if statement concatenates a single I.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "II"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "I"; if (number == 3) roman = "III"; return roman;}

Page 140: Larry and Jen do Roman Numerals in C++

First I'll refactor the code so that each if statement concatenates a single I.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "II"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number == 3) roman = "III"; return roman;}

Page 141: Larry and Jen do Roman Numerals in C++

First I'll refactor the code so that each if statement concatenates a single I.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "II"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "III"; return roman;}

Page 142: Larry and Jen do Roman Numerals in C++

First I'll refactor the code so that each if statement concatenates a single I.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "II"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "II"; return roman;}

Page 143: Larry and Jen do Roman Numerals in C++

First I'll refactor the code so that each if statement concatenates a single I.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "II"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "I"; return roman;}

Page 144: Larry and Jen do Roman Numerals in C++

First I'll refactor the code so that each if statement concatenates a single I.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "II"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman += "I"; return roman;}

Page 145: Larry and Jen do Roman Numerals in C++

First I'll refactor the code so that each if statement concatenates a single I.

$ cc -Wall -Werror *.cpp && ./a.out

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "II"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman += "I"; return roman;}

Page 146: Larry and Jen do Roman Numerals in C++

First I'll refactor the code so that each if statement concatenates a single I.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number == 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman = "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number == 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "II"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman = "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number == 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "III"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "II"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman = "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman += "I"; return roman;}

Page 147: Larry and Jen do Roman Numerals in C++

I add braces to the if statements.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman += "I"; return roman;}

Page 148: Larry and Jen do Roman Numerals in C++

I add braces to the if statements.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman += "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) roman += "I"; if (number >= 3) roman += "I"; return roman;}

Page 149: Larry and Jen do Roman Numerals in C++

I add braces to the if statements.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman += "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) roman += "I"; if (number >= 3) roman += "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) { roman += "I"; } if (number >= 3) roman += "I"; return roman;}

Page 150: Larry and Jen do Roman Numerals in C++

I add braces to the if statements.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman += "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) roman += "I"; if (number >= 3) roman += "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) { roman += "I"; } if (number >= 3) roman += "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

Page 151: Larry and Jen do Roman Numerals in C++

I add braces to the if statements.

$ cc -Wall -Werror *.cpp && ./a.out

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman += "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) roman += "I"; if (number >= 3) roman += "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) { roman += "I"; } if (number >= 3) roman += "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

Page 152: Larry and Jen do Roman Numerals in C++

I add braces to the if statements.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) roman += "I"; if (number >= 2) roman += "I"; if (number >= 3) roman += "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) roman += "I"; if (number >= 3) roman += "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) { roman += "I"; } if (number >= 3) roman += "I"; return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

Page 153: Larry and Jen do Roman Numerals in C++

Now I make the three ifs exactly the same.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

Page 154: Larry and Jen do Roman Numerals in C++

Now I make the three ifs exactly the same.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 2) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

Page 155: Larry and Jen do Roman Numerals in C++

Now I make the three ifs exactly the same.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 2) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

Page 156: Larry and Jen do Roman Numerals in C++

Now I make the three ifs exactly the same.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 2) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; number--; } if (number >= 3) { roman += "I"; } return roman;}

Page 157: Larry and Jen do Roman Numerals in C++

Now I make the three ifs exactly the same.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 2) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; number--; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; } return roman;}

Page 158: Larry and Jen do Roman Numerals in C++

Now I make the three ifs exactly the same.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 2) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; number--; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; number--; } return roman;}

Page 159: Larry and Jen do Roman Numerals in C++

Now I make the three ifs exactly the same.

$ cc -Wall -Werror *.cpp && ./a.out

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 2) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; number--; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; number--; } return roman;}

Page 160: Larry and Jen do Roman Numerals in C++

Now I make the three ifs exactly the same.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; } if (number >= 2) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 2) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; number--; } if (number >= 3) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; number--; } return roman;}

Page 161: Larry and Jen do Roman Numerals in C++

Finally I can collapse the three identical if statements into a single while statement.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; number--; } if (number >= 1) { roman += "I"; number--; } return roman;}

Page 162: Larry and Jen do Roman Numerals in C++

Finally I can collapse the three identical if statements into a single while statement.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; if (number >= 1) { roman += "I"; number--; } return roman;}

Page 163: Larry and Jen do Roman Numerals in C++

Finally I can collapse the three identical if statements into a single while statement.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; (number >= 1) { roman += "I"; number--; } return roman;}

Page 164: Larry and Jen do Roman Numerals in C++

Finally I can collapse the three identical if statements into a single while statement.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 1) { roman += "I"; number--; } return roman;}

Page 165: Larry and Jen do Roman Numerals in C++

Finally I can collapse the three identical if statements into a single while statement.

$ cc -Wall -Werror *.cpp && ./a.out

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 1) { roman += "I"; number--; } return roman;}

Page 166: Larry and Jen do Roman Numerals in C++

Finally I can collapse the three identical if statements into a single while statement.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 1) { roman += "I"; number--; } return roman;}

Page 167: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 1) { roman += "I"; number--; } return roman;}

Cool. The repeated if statements are just like an unrolled while statement.

Page 168: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 1) { roman += "I"; number--; } return roman;}

Cool. The repeated if statements are just like an unrolled while statement.

Exactly.

Page 169: Larry and Jen do Roman Numerals in C++

Any more refactoring?

Page 170: Larry and Jen do Roman Numerals in C++

What do you think?

Any more refactoring?

Page 171: Larry and Jen do Roman Numerals in C++

What do you think?

Any more refactoring?

I can't see anything.

Page 172: Larry and Jen do Roman Numerals in C++

What do you think?

Any more refactoring?

I can't see anything.

ok. Let's move on.

Page 173: Larry and Jen do Roman Numerals in C++

What do you think?

Any more refactoring?

I can't see anything.

ok. Let's move on.

Let's add a test for 4 being IV

Page 174: Larry and Jen do Roman Numerals in C++

What do you think?

Any more refactoring?

I can't see anything.

ok. Let's move on.

Let's add a test for 4 being IV

I'd do 10 being X next.

Page 175: Larry and Jen do Roman Numerals in C++

What do you think?

Any more refactoring?

I can't see anything.

ok. Let's move on.

Let's add a test for 4 being IV

I'd do 10 being X next.

ok

Page 176: Larry and Jen do Roman Numerals in C++

I'll add the test for 10 being X

...static void test_to_roman(){ assert(to_roman(1) == "I"); assert(to_roman(2) == "II"); assert(to_roman(3) == "III");}

Page 177: Larry and Jen do Roman Numerals in C++

I'll add the test for 10 being X

...static void test_to_roman(){ assert(to_roman(1) == "I"); assert(to_roman(2) == "II"); assert(to_roman(3) == "III");

}

Page 178: Larry and Jen do Roman Numerals in C++

I'll add the test for 10 being X

...static void test_to_roman(){ assert(to_roman(1) == "I"); assert(to_roman(2) == "II"); assert(to_roman(3) == "III");

} assert(to_roman(10) == "X");

Page 179: Larry and Jen do Roman Numerals in C++

I'll add the test for 10 being X

$ cc -Wall -Werror *.cpp && ./a.out

...static void test_to_roman(){ assert(to_roman(1) == "I"); assert(to_roman(2) == "II"); assert(to_roman(3) == "III");

} assert(to_roman(10) == "X");

Page 180: Larry and Jen do Roman Numerals in C++

I'll add the test for 10 being X

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAssertion `to_roman(10) == "X"' failed.Aborted

...static void test_to_roman(){ assert(to_roman(1) == "I"); assert(to_roman(2) == "II"); assert(to_roman(3) == "III");

} assert(to_roman(10) == "X");

Page 181: Larry and Jen do Roman Numerals in C++

I'll add the test for 10 being X

It fails as expected.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAssertion `to_roman(10) == "X"' failed.Aborted

...static void test_to_roman(){ assert(to_roman(1) == "I"); assert(to_roman(2) == "II"); assert(to_roman(3) == "III");

} assert(to_roman(10) == "X");

Page 182: Larry and Jen do Roman Numerals in C++

I can make it pass by repeating the same while statement but using 10 and X instead

of 1 and I.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 1) { roman += "I"; number--; } return roman;}

Page 183: Larry and Jen do Roman Numerals in C++

I can make it pass by repeating the same while statement but using 10 and X instead

of 1 and I.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 1) { roman += "I"; number--; }

return roman;}

Page 184: Larry and Jen do Roman Numerals in C++

I can make it pass by repeating the same while statement but using 10 and X instead

of 1 and I.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 1) { roman += "I"; number--; }

return roman;}

while (number >= 10) { roman += "X"; number -= 10; }

Page 185: Larry and Jen do Roman Numerals in C++

I can make it pass by repeating the same while statement but using 10 and X instead

of 1 and I.

$ cc -Wall -Werror *.cpp && ./a.out

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 1) { roman += "I"; number--; }

return roman;}

while (number >= 10) { roman += "X"; number -= 10; }

Page 186: Larry and Jen do Roman Numerals in C++

I can make it pass by repeating the same while statement but using 10 and X instead

of 1 and I.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAssertion `to_roman(10) == "X"' failed.Aborted

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 1) { roman += "I"; number--; }

return roman;}

while (number >= 10) { roman += "X"; number -= 10; }

Page 187: Larry and Jen do Roman Numerals in C++

I can make it pass by repeating the same while statement but using 10 and X instead

of 1 and I.

Eh? It still fails.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAssertion `to_roman(10) == "X"' failed.Aborted

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 1) { roman += "I"; number--; }

return roman;}

while (number >= 10) { roman += "X"; number -= 10; }

Page 188: Larry and Jen do Roman Numerals in C++

It would really help if the console output told us what to_roman is actually returning.

...static void test_to_roman(){ assert(to_roman(1) == "I"); assert(to_roman(2) == "II"); assert(to_roman(3) == "III"); assert(to_roman(10) == "X");}

$ cc -Wall -Werror *.cpp && ./a.outAssertion `to_roman(10) == "X"' failed.Aborted

Page 189: Larry and Jen do Roman Numerals in C++

It would really help if the console output told us what to_roman is actually returning.

Yes it would.

...static void test_to_roman(){ assert(to_roman(1) == "I"); assert(to_roman(2) == "II"); assert(to_roman(3) == "III"); assert(to_roman(10) == "X");}

$ cc -Wall -Werror *.cpp && ./a.outAssertion `to_roman(10) == "X"' failed.Aborted

Page 190: Larry and Jen do Roman Numerals in C++

It would really help if the console output told us what to_roman is actually returning.

Yes it would.

...static void test_to_roman(){ assert(to_roman(1) == "I"); assert(to_roman(2) == "II"); assert(to_roman(3) == "III"); assert(to_roman(10) == "X");}

Let's rework the test to do that.

$ cc -Wall -Werror *.cpp && ./a.outAssertion `to_roman(10) == "X"' failed.Aborted

Page 191: Larry and Jen do Roman Numerals in C++

It would really help if the console output told us what to_roman is actually returning.

Yes it would.

...static void test_to_roman(){ assert(to_roman(1) == "I"); assert(to_roman(2) == "II"); assert(to_roman(3) == "III"); assert(to_roman(10) == "X");}

Let's rework the test to do that.

ok

$ cc -Wall -Werror *.cpp && ./a.outAssertion `to_roman(10) == "X"' failed.Aborted

Page 192: Larry and Jen do Roman Numerals in C++

Let's make a function called assert_to_roman.

...static void test_to_roman(){ assert(to_roman(1) == "I"); assert(to_roman(2) == "II"); assert(to_roman(3) == "III"); assert(to_roman(10) == "X");}

Page 193: Larry and Jen do Roman Numerals in C++

Let's make a function called assert_to_roman.

ok

...static void test_to_roman(){ assert(to_roman(1) == "I"); assert(to_roman(2) == "II"); assert(to_roman(3) == "III"); assert(to_roman(10) == "X");}

Page 194: Larry and Jen do Roman Numerals in C++

Let's make a function called assert_to_roman.

ok

...static void test_to_roman(){ assert_to_roman(1,"I"); assert(to_roman(2) == "II"); assert(to_roman(3) == "III"); assert(to_roman(10) == "X");}

Page 195: Larry and Jen do Roman Numerals in C++

Let's make a function called assert_to_roman.

ok

...static void test_to_roman(){ assert_to_roman(1,"I"); assert_to_roman(2,"II"); assert(to_roman(3) == "III"); assert(to_roman(10) == "X");}

Page 196: Larry and Jen do Roman Numerals in C++

Let's make a function called assert_to_roman.

ok

...static void test_to_roman(){ assert_to_roman(1,"I"); assert_to_roman(2,"II"); assert_to_roman(3,"III"); assert(to_roman(10) == "X");}

Page 197: Larry and Jen do Roman Numerals in C++

Let's make a function called assert_to_roman.

ok

...static void test_to_roman(){ assert_to_roman(1,"I"); assert_to_roman(2,"II"); assert_to_roman(3,"III"); assert_to_roman(10,"X");}

Page 198: Larry and Jen do Roman Numerals in C++

Now we write assert_to_roman

Page 199: Larry and Jen do Roman Numerals in C++

static void assert_to_roman(int arabic, const std::string & expected){ const std::string actual = to_roman(arabic); if (expected != actual) { std::cerr << "expected: to_roman" << '(' << arabic << ") == " << '"' << expected << '"' << std::endl; std::cerr << " actual: to_roman" << '(' << arabic << ") == " << '"' << actual << '"' << std::endl; assert(false); }}...

Now we write assert_to_roman

Page 200: Larry and Jen do Roman Numerals in C++

static void assert_to_roman(int arabic, const std::string & expected){ const std::string actual = to_roman(arabic); if (expected != actual) { std::cerr << "expected: to_roman" << '(' << arabic << ") == " << '"' << expected << '"' << std::endl; std::cerr << " actual: to_roman" << '(' << arabic << ") == " << '"' << actual << '"' << std::endl; assert(false); }}...

Now we write assert_to_roman

$ cc -Wall -Werror *.cpp && ./a.out

Page 201: Larry and Jen do Roman Numerals in C++

static void assert_to_roman(int arabic, const std::string & expected){ const std::string actual = to_roman(arabic); if (expected != actual) { std::cerr << "expected: to_roman" << '(' << arabic << ") == " << '"' << expected << '"' << std::endl; std::cerr << " actual: to_roman" << '(' << arabic << ") == " << '"' << actual << '"' << std::endl; assert(false); }}...

Now we write assert_to_roman

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outexpected: to_roman(10) == "X" actual: to_roman(10) == "IIIIIIIIII"Assertion `false' failed.Aborted

Page 202: Larry and Jen do Roman Numerals in C++

static void assert_to_roman(int arabic, const std::string & expected){ const std::string actual = to_roman(arabic); if (expected != actual) { std::cerr << "expected: to_roman" << '(' << arabic << ") == " << '"' << expected << '"' << std::endl; std::cerr << " actual: to_roman" << '(' << arabic << ") == " << '"' << actual << '"' << std::endl; assert(false); }}...

Now we write assert_to_roman

$ cc -Wall -Werror *.cpp && ./a.out

That's a much more helpful message.

$ cc -Wall -Werror *.cpp && ./a.outexpected: to_roman(10) == "X" actual: to_roman(10) == "IIIIIIIIII"Assertion `false' failed.Aborted

Page 203: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 1) { roman += "I"; number--; } while (number >= 10) { roman += "X"; number -= 10; } return roman;}

$ cc -Wall -Werror *.cpp && ./a.outexpected: to_roman(10) == "X" actual: to_roman(10) == "IIIIIIIIII"Assertion `false' failed.Aborted

So why isn't to_roman working for X?

Page 204: Larry and Jen do Roman Numerals in C++

Ah of course. The while statements are the wrong way round.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 1) { roman += "I"; number--; } while (number >= 10) { roman += "X"; number -= 10; } return roman;}

$ cc -Wall -Werror *.cpp && ./a.outexpected: to_roman(10) == "X" actual: to_roman(10) == "IIIIIIIIII"Assertion `false' failed.Aborted

So why isn't to_roman working for X?

Page 205: Larry and Jen do Roman Numerals in C++

That's easy to fix.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = "";

return roman;}

while (number >= 1) { roman += "I"; number--; } while (number >= 10) { roman += "X"; number -= 10; }

Page 206: Larry and Jen do Roman Numerals in C++

That's easy to fix.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = "";

return roman;}

while (number >= 1) { roman += "I"; number--; }

while (number >= 10) { roman += "X"; number -= 10; }

Page 207: Larry and Jen do Roman Numerals in C++

That's easy to fix.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = "";

return roman;}

$ cc -Wall -Werror *.cpp && ./a.out

while (number >= 1) { roman += "I"; number--; }

while (number >= 10) { roman += "X"; number -= 10; }

Page 208: Larry and Jen do Roman Numerals in C++

That's easy to fix.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = "";

return roman;}

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

while (number >= 1) { roman += "I"; number--; }

while (number >= 10) { roman += "X"; number -= 10; }

Page 209: Larry and Jen do Roman Numerals in C++

That's easy to fix.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = "";

return roman;}

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Yes. We're green again.

while (number >= 1) { roman += "I"; number--; }

while (number >= 10) { roman += "X"; number -= 10; }

Page 210: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman(1,"I"); assert_to_roman(2,"II"); assert_to_roman(3,"III"); assert_to_roman(10,"X");}

I think tests for 20 and 30 should pass straight away.

Page 211: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman(1,"I"); assert_to_roman(2,"II"); assert_to_roman(3,"III"); assert_to_roman(10,"X");}

I agree.

I think tests for 20 and 30 should pass straight away.

Page 212: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman(1,"I"); assert_to_roman(2,"II"); assert_to_roman(3,"III"); assert_to_roman(10,"X");

}

I agree.

I think tests for 20 and 30 should pass straight away.

Page 213: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman(1,"I"); assert_to_roman(2,"II"); assert_to_roman(3,"III"); assert_to_roman(10,"X");

}

assert_to_roman(20,"XX"); assert_to_roman(30,"XXX");

I agree.

I think tests for 20 and 30 should pass straight away.

Page 214: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman(1,"I"); assert_to_roman(2,"II"); assert_to_roman(3,"III"); assert_to_roman(10,"X");

}

assert_to_roman(20,"XX"); assert_to_roman(30,"XXX");

I agree.

I think tests for 20 and 30 should pass straight away.

Page 215: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman(1,"I"); assert_to_roman(2,"II"); assert_to_roman(3,"III"); assert_to_roman(10,"X");

}

assert_to_roman(20,"XX"); assert_to_roman(30,"XXX");

I agree.

I think tests for 20 and 30 should pass straight away.

Page 216: Larry and Jen do Roman Numerals in C++

I think 33 should pass already.

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1,"I"); assert_to_roman( 2,"II"); assert_to_roman( 3,"III"); assert_to_roman(10,"X"); assert_to_roman(20,"XX"); assert_to_roman(30,"XXX");}

Page 217: Larry and Jen do Roman Numerals in C++

I think 33 should pass already.

I do too.

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1,"I"); assert_to_roman( 2,"II"); assert_to_roman( 3,"III"); assert_to_roman(10,"X"); assert_to_roman(20,"XX"); assert_to_roman(30,"XXX");}

Page 218: Larry and Jen do Roman Numerals in C++

I think 33 should pass already.

I do too.

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1,"I"); assert_to_roman( 2,"II"); assert_to_roman( 3,"III"); assert_to_roman(10,"X"); assert_to_roman(20,"XX"); assert_to_roman(30,"XXX");

}

Page 219: Larry and Jen do Roman Numerals in C++

I think 33 should pass already.

I do too.

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1,"I"); assert_to_roman( 2,"II"); assert_to_roman( 3,"III"); assert_to_roman(10,"X"); assert_to_roman(20,"XX"); assert_to_roman(30,"XXX");

} assert_to_roman(33,"XXXIII");

Page 220: Larry and Jen do Roman Numerals in C++

I think 33 should pass already.

$ cc -Wall -Werror *.cpp && ./a.out

I do too.

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1,"I"); assert_to_roman( 2,"II"); assert_to_roman( 3,"III"); assert_to_roman(10,"X"); assert_to_roman(20,"XX"); assert_to_roman(30,"XXX");

} assert_to_roman(33,"XXXIII");

Page 221: Larry and Jen do Roman Numerals in C++

I think 33 should pass already.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

I do too.

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1,"I"); assert_to_roman( 2,"II"); assert_to_roman( 3,"III"); assert_to_roman(10,"X"); assert_to_roman(20,"XX"); assert_to_roman(30,"XXX");

} assert_to_roman(33,"XXXIII");

Page 222: Larry and Jen do Roman Numerals in C++

What next?

Page 223: Larry and Jen do Roman Numerals in C++

What next?

How about 100 ?

Page 224: Larry and Jen do Roman Numerals in C++

What next?

How about 100 ?

That should be C

Page 225: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1,"I"); assert_to_roman( 2,"II"); assert_to_roman( 3,"III"); assert_to_roman(10,"X"); assert_to_roman(20,"XX"); assert_to_roman(30,"XXX"); assert_to_roman(33,"XXXIII");}

Page 226: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1,"I"); assert_to_roman( 2,"II"); assert_to_roman( 3,"III"); assert_to_roman(10,"X"); assert_to_roman(20,"XX"); assert_to_roman(30,"XXX"); assert_to_roman(33,"XXXIII");

}

Page 227: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1,"I"); assert_to_roman( 2,"II"); assert_to_roman( 3,"III"); assert_to_roman(10,"X"); assert_to_roman(20,"XX"); assert_to_roman(30,"XXX"); assert_to_roman(33,"XXXIII");

} assert_to_roman(100,"C");

Page 228: Larry and Jen do Roman Numerals in C++

This will fail with 10 X's

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1,"I"); assert_to_roman( 2,"II"); assert_to_roman( 3,"III"); assert_to_roman(10,"X"); assert_to_roman(20,"XX"); assert_to_roman(30,"XXX"); assert_to_roman(33,"XXXIII");

} assert_to_roman(100,"C");

Page 229: Larry and Jen do Roman Numerals in C++

This will fail with 10 X's

$ cc -Wall -Werror *.cpp && ./a.out

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1,"I"); assert_to_roman( 2,"II"); assert_to_roman( 3,"III"); assert_to_roman(10,"X"); assert_to_roman(20,"XX"); assert_to_roman(30,"XXX"); assert_to_roman(33,"XXXIII");

} assert_to_roman(100,"C");

Page 230: Larry and Jen do Roman Numerals in C++

This will fail with 10 X's

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outexpected: to_roman(100) == "C" actual: to_roman(100) == "XXXXXXXXXX"....int main(): Assertion `false' failed.Aborted

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1,"I"); assert_to_roman( 2,"II"); assert_to_roman( 3,"III"); assert_to_roman(10,"X"); assert_to_roman(20,"XX"); assert_to_roman(30,"XXX"); assert_to_roman(33,"XXXIII");

} assert_to_roman(100,"C");

Page 231: Larry and Jen do Roman Numerals in C++

This will fail with 10 X's

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outexpected: to_roman(100) == "C" actual: to_roman(100) == "XXXXXXXXXX"....int main(): Assertion `false' failed.Aborted

Excellent!

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1,"I"); assert_to_roman( 2,"II"); assert_to_roman( 3,"III"); assert_to_roman(10,"X"); assert_to_roman(20,"XX"); assert_to_roman(30,"XXX"); assert_to_roman(33,"XXXIII");

} assert_to_roman(100,"C");

Page 232: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

To make it pass I just need to add another while statement.

Page 233: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

To make it pass I just need to add another while statement.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = "";

while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

Page 234: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

To make it pass I just need to add another while statement.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = "";

while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

while (number >= 100) { roman += "C"; number -= 100; }

Page 235: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

To make it pass I just need to add another while statement.

$ cc -Wall -Werror *.cpp && ./a.out

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = "";

while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

while (number >= 100) { roman += "C"; number -= 100; }

Page 236: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

To make it pass I just need to add another while statement.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = "";

while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

while (number >= 100) { roman += "C"; number -= 100; }

Page 237: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 100) { roman += "C"; number -= 100; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

And now?

Page 238: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

The three while statements are duplicating each other. So let's

refactor those.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 100) { roman += "C"; number -= 100; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

And now?

Page 239: Larry and Jen do Roman Numerals in C++

First I'll introduce a simple struct to

represent a roman digit.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 100) { roman += "C"; number -= 100; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 100) { roman += "C"; number -= 100; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

Page 240: Larry and Jen do Roman Numerals in C++

First I'll introduce a simple struct to

represent a roman digit.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 100) { roman += "C"; number -= 100; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 100) { roman += "C"; number -= 100; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

Page 241: Larry and Jen do Roman Numerals in C++

First I'll introduce a simple struct to

represent a roman digit.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 100) { roman += "C"; number -= 100; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 100) { roman += "C"; number -= 100; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

struct digit{ int arabic; std::string roman;};

Page 242: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out

First I'll introduce a simple struct to

represent a roman digit.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 100) { roman += "C"; number -= 100; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 100) { roman += "C"; number -= 100; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

struct digit{ int arabic; std::string roman;};

Page 243: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

First I'll introduce a simple struct to

represent a roman digit.

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 100) { roman += "C"; number -= 100; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

#include "to_roman.hpp"

std::string to_roman(int number){ std::string roman = ""; while (number >= 100) { roman += "C"; number -= 100; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

struct digit{ int arabic; std::string roman;};

Page 244: Larry and Jen do Roman Numerals in C++

Now I introduce 100 and "C" as a digit.

#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

std::string to_roman(int number){ std::string roman = ""; while (number >= 100) { roman += "C"; number -= 100; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

Page 245: Larry and Jen do Roman Numerals in C++

Now I introduce 100 and "C" as a digit.

#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

std::string to_roman(int number){ std::string roman = ""; while (number >= 100) { roman += "C"; number -= 100; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

Page 246: Larry and Jen do Roman Numerals in C++

Now I introduce 100 and "C" as a digit.

#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

std::string to_roman(int number){ std::string roman = ""; while (number >= 100) { roman += "C"; number -= 100; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

const digit digits[] = { { 100, "C" },};

Page 247: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out

Now I introduce 100 and "C" as a digit.

#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

std::string to_roman(int number){ std::string roman = ""; while (number >= 100) { roman += "C"; number -= 100; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

const digit digits[] = { { 100, "C" },};

Page 248: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Now I introduce 100 and "C" as a digit.

#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

std::string to_roman(int number){ std::string roman = ""; while (number >= 100) { roman += "C"; number -= 100; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

const digit digits[] = { { 100, "C" },};

Page 249: Larry and Jen do Roman Numerals in C++

Now I use the first digit instead of 100

and "C".

#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" },};

std::string to_roman(int number){ std::string roman = ""; while (number >= 100) { roman += "C"; number -= 100; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

Page 250: Larry and Jen do Roman Numerals in C++

Now I use the first digit instead of 100

and "C".

#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" },};

std::string to_roman(int number){ std::string roman = ""; while (number >= { roman += number -= } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

Page 251: Larry and Jen do Roman Numerals in C++

Now I use the first digit instead of 100

and "C".

#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" },};

std::string to_roman(int number){ std::string roman = ""; while (number >= { roman += number -= } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

digits[0].arabic)

digits[0].roman; digits[0].arabic;

Page 252: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out

Now I use the first digit instead of 100

and "C".

#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" },};

std::string to_roman(int number){ std::string roman = ""; while (number >= { roman += number -= } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

digits[0].arabic)

digits[0].roman; digits[0].arabic;

Page 253: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Now I use the first digit instead of 100

and "C".

#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" },};

std::string to_roman(int number){ std::string roman = ""; while (number >= { roman += number -= } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

digits[0].arabic)

digits[0].roman; digits[0].arabic;

Page 254: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Now I use the first digit instead of 100

and "C".

#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" },};

std::string to_roman(int number){ std::string roman = ""; while (number >= { roman += number -= } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

digits[0].arabic)

digits[0].roman; digits[0].arabic;

Nice.

Page 255: Larry and Jen do Roman Numerals in C++

Now I repeat for the next digit, 10

and "X".

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" },};

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

Page 256: Larry and Jen do Roman Numerals in C++

Now I repeat for the next digit, 10

and "X".

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, };

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

Page 257: Larry and Jen do Roman Numerals in C++

Now I repeat for the next digit, 10

and "X".

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, };

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

{ 10, "X" },

Page 258: Larry and Jen do Roman Numerals in C++

Now I repeat for the next digit, 10

and "X".

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, };

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

{ 10, "X" },

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" },};

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= { roman += number -= } while (number >= 1) { roman += "I"; number--; } return roman;}

Page 259: Larry and Jen do Roman Numerals in C++

Now I repeat for the next digit, 10

and "X".

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, };

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

{ 10, "X" },

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" },};

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= { roman += number -= } while (number >= 1) { roman += "I"; number--; } return roman;}

digits[1].arabic)

digits[1].roman;digits[1].arabic;

Page 260: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out

Now I repeat for the next digit, 10

and "X".

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, };

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

{ 10, "X" },

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" },};

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= { roman += number -= } while (number >= 1) { roman += "I"; number--; } return roman;}

digits[1].arabic)

digits[1].roman;digits[1].arabic;

Page 261: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Now I repeat for the next digit, 10

and "X".

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, };

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= 10) { roman += "X"; number -= 10; } while (number >= 1) { roman += "I"; number--; } return roman;}

{ 10, "X" },

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" },};

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= { roman += number -= } while (number >= 1) { roman += "I"; number--; } return roman;}

digits[1].arabic)

digits[1].roman;digits[1].arabic;

Page 262: Larry and Jen do Roman Numerals in C++

I repeat again for the next

digit, 1 and "I".

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" }, };

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= 1) { roman += "I"; number--; } return roman;}

Page 263: Larry and Jen do Roman Numerals in C++

I repeat again for the next

digit, 1 and "I".

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" },

};

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= 1) { roman += "I"; number--; } return roman;}

Page 264: Larry and Jen do Roman Numerals in C++

I repeat again for the next

digit, 1 and "I".

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" },

};

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= 1) { roman += "I"; number--; } return roman;}

{ 1, "I" },

Page 265: Larry and Jen do Roman Numerals in C++

I repeat again for the next

digit, 1 and "I".

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" },

};

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= 1) { roman += "I"; number--; } return roman;}

{ 1, "I" },

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" },};

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= { roman += number } return roman;}

Page 266: Larry and Jen do Roman Numerals in C++

I repeat again for the next

digit, 1 and "I".

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" },

};

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= 1) { roman += "I"; number--; } return roman;}

{ 1, "I" },

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" },};

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= { roman += number } return roman;}

digits[2].arabic)

-= digits[2].arabic;digits[2].roman;

Page 267: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out

I repeat again for the next

digit, 1 and "I".

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" },

};

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= 1) { roman += "I"; number--; } return roman;}

{ 1, "I" },

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" },};

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= { roman += number } return roman;}

digits[2].arabic)

-= digits[2].arabic;digits[2].roman;

Page 268: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

I repeat again for the next

digit, 1 and "I".

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" },

};

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= 1) { roman += "I"; number--; } return roman;}

{ 1, "I" },

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" },};

std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= { roman += number } return roman;}

digits[2].arabic)

-= digits[2].arabic;digits[2].roman;

Page 269: Larry and Jen do Roman Numerals in C++

Now I can replace each use of 0 with

an introduced variable.

...std::string to_roman(int number){ std::string roman = ""; while (number >= digits[0].arabic) { roman += digits[0].roman; number -= digits[0].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= digits[2].arabic) { roman += digits[2].roman; number -= digits[2].arabic; } return roman;}

Page 270: Larry and Jen do Roman Numerals in C++

Now I can replace each use of 0 with

an introduced variable.

...std::string to_roman(int number){ std::string roman = "";

while (number >= digits[ ].arabic) { roman += digits[ ].roman; number -= digits[ ].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= digits[2].arabic) { roman += digits[2].roman; number -= digits[2].arabic; } return roman;}

Page 271: Larry and Jen do Roman Numerals in C++

Now I can replace each use of 0 with

an introduced variable.

...std::string to_roman(int number){ std::string roman = "";

while (number >= digits[ ].arabic) { roman += digits[ ].roman; number -= digits[ ].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= digits[2].arabic) { roman += digits[2].roman; number -= digits[2].arabic; } return roman;}

std::size_t i = 0;i

ii

Page 272: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out

Now I can replace each use of 0 with

an introduced variable.

...std::string to_roman(int number){ std::string roman = "";

while (number >= digits[ ].arabic) { roman += digits[ ].roman; number -= digits[ ].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= digits[2].arabic) { roman += digits[2].roman; number -= digits[2].arabic; } return roman;}

std::size_t i = 0;i

ii

Page 273: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Now I can replace each use of 0 with

an introduced variable.

...std::string to_roman(int number){ std::string roman = "";

while (number >= digits[ ].arabic) { roman += digits[ ].roman; number -= digits[ ].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= digits[2].arabic) { roman += digits[2].roman; number -= digits[2].arabic; } return roman;}

std::size_t i = 0;i

ii

Page 274: Larry and Jen do Roman Numerals in C++

And replace 1 with the introduced

variable....std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= digits[2].arabic) { roman += digits[2].roman; number -= digits[2].arabic; } return roman;}

Page 275: Larry and Jen do Roman Numerals in C++

And replace 1 with the introduced

variable....std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= digits[2].arabic) { roman += digits[2].roman; number -= digits[2].arabic; } return roman;}

...std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; }

while (number >= digits[ ].arabic) { roman += digits[ ].roman; number -= digits[ ].arabic; } while (number >= digits[2].arabic) { roman += digits[2].roman; number -= digits[2].arabic; } return roman;}

Page 276: Larry and Jen do Roman Numerals in C++

And replace 1 with the introduced

variable....std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= digits[2].arabic) { roman += digits[2].roman; number -= digits[2].arabic; } return roman;}

...std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; }

while (number >= digits[ ].arabic) { roman += digits[ ].roman; number -= digits[ ].arabic; } while (number >= digits[2].arabic) { roman += digits[2].roman; number -= digits[2].arabic; } return roman;}

i++i;

ii

Page 277: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out

And replace 1 with the introduced

variable....std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= digits[2].arabic) { roman += digits[2].roman; number -= digits[2].arabic; } return roman;}

...std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; }

while (number >= digits[ ].arabic) { roman += digits[ ].roman; number -= digits[ ].arabic; } while (number >= digits[2].arabic) { roman += digits[2].roman; number -= digits[2].arabic; } return roman;}

i++i;

ii

Page 278: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

And replace 1 with the introduced

variable....std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } while (number >= digits[1].arabic) { roman += digits[1].roman; number -= digits[1].arabic; } while (number >= digits[2].arabic) { roman += digits[2].roman; number -= digits[2].arabic; } return roman;}

...std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; }

while (number >= digits[ ].arabic) { roman += digits[ ].roman; number -= digits[ ].arabic; } while (number >= digits[2].arabic) { roman += digits[2].roman; number -= digits[2].arabic; } return roman;}

i++i;

ii

Page 279: Larry and Jen do Roman Numerals in C++

And replace 2 with the introduced

variable.

...std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } ++i; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } while (number >= digits[2].arabic) { roman += digits[2].roman; number -= digits[2].arabic; } return roman;}

Page 280: Larry and Jen do Roman Numerals in C++

And replace 2 with the introduced

variable.

...std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } ++i; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } while (number >= digits[2].arabic) { roman += digits[2].roman; number -= digits[2].arabic; } return roman;}

...std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } ++i; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; }

while (number >= digits[ ].arabic) { roman += digits[ ].roman; number -= digits[ ].arabic; } return roman;}

Page 281: Larry and Jen do Roman Numerals in C++

And replace 2 with the introduced

variable.

...std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } ++i; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } while (number >= digits[2].arabic) { roman += digits[2].roman; number -= digits[2].arabic; } return roman;}

...std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } ++i; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; }

while (number >= digits[ ].arabic) { roman += digits[ ].roman; number -= digits[ ].arabic; } return roman;}

++i;i

ii

Page 282: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out

And replace 2 with the introduced

variable.

...std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } ++i; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } while (number >= digits[2].arabic) { roman += digits[2].roman; number -= digits[2].arabic; } return roman;}

...std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } ++i; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; }

while (number >= digits[ ].arabic) { roman += digits[ ].roman; number -= digits[ ].arabic; } return roman;}

++i;i

ii

Page 283: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

And replace 2 with the introduced

variable.

...std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } ++i; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } while (number >= digits[2].arabic) { roman += digits[2].roman; number -= digits[2].arabic; } return roman;}

...std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } ++i; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; }

while (number >= digits[ ].arabic) { roman += digits[ ].roman; number -= digits[ ].arabic; } return roman;}

++i;i

ii

Page 284: Larry and Jen do Roman Numerals in C++

Now I can collapse the duplication into

a for statement.

...std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } ++i; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } ++i; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

Page 285: Larry and Jen do Roman Numerals in C++

Now I can collapse the duplication into

a for statement.

...std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } ++i; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } ++i; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

Page 286: Larry and Jen do Roman Numerals in C++

Now I can collapse the duplication into

a for statement.

...std::string to_roman(int number){ std::string roman = ""; std::size_t i = 0; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } ++i; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } ++i; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

for ( i != 3; ++i)

Page 287: Larry and Jen do Roman Numerals in C++

Now I can collapse the duplication into

a for statement.

...std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != 3; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } ++i; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } ++i; while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

Page 288: Larry and Jen do Roman Numerals in C++

Now I can collapse the duplication into

a for statement.

...std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != 3; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; }

return roman;}

Page 289: Larry and Jen do Roman Numerals in C++

Now I can collapse the duplication into

a for statement.

...std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != 3; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

Page 290: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out

Now I can collapse the duplication into

a for statement.

...std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != 3; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

Page 291: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Now I can collapse the duplication into

a for statement.

...std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != 3; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

Page 292: Larry and Jen do Roman Numerals in C++

Now I can replace 3 with the size of the

array.

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != 3; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

Page 293: Larry and Jen do Roman Numerals in C++

Now I can replace 3 with the size of the

array.

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != 3; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

Page 294: Larry and Jen do Roman Numerals in C++

Now I can replace 3 with the size of the

array.

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != 3; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

const std::size_t n = sizeof digits / sizeof digits[0];

Page 295: Larry and Jen do Roman Numerals in C++

Now I can replace 3 with the size of the

array.

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != ; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

Page 296: Larry and Jen do Roman Numerals in C++

Now I can replace 3 with the size of the

array.

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != ; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

n

Page 297: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out

Now I can replace 3 with the size of the

array.

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != ; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

n

Page 298: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Now I can replace 3 with the size of the

array.

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != ; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

n

Page 299: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Now I can replace 3 with the size of the

array.

#include "to_roman.hpp"...const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != ; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

n

Excellent.

Page 300: Larry and Jen do Roman Numerals in C++

Done.#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

Page 301: Larry and Jen do Roman Numerals in C++

Done.#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

Anything else?

Page 302: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

What do you see?

Page 303: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

What do you see?

digits and n have internal linkage

because they are const, but the digit type has external

linkage.

Page 304: Larry and Jen do Roman Numerals in C++

How can I give a type internal

linkage?

#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

Page 305: Larry and Jen do Roman Numerals in C++

How can I give a type internal

linkage?

#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

With an anonymous namespace.

Page 306: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

...

Can you Show me?

Page 307: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

...

Like this.

Can you Show me?

Page 308: Larry and Jen do Roman Numerals in C++

Like this.

#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

...

Can you Show me?

Page 309: Larry and Jen do Roman Numerals in C++

Like this.

#include "to_roman.hpp"

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

...

namespace {

Can you Show me?

Page 310: Larry and Jen do Roman Numerals in C++

Like this.

#include "to_roman.hpp"

namespace{ struct digit { int arabic; std::string roman; };

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

...

Can you Show me?

Page 311: Larry and Jen do Roman Numerals in C++

Like this.

#include "to_roman.hpp"

namespace{ struct digit { int arabic; std::string roman; };

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

...}

Can you Show me?

Page 312: Larry and Jen do Roman Numerals in C++

Like this.

#include "to_roman.hpp"

namespace{ struct digit { int arabic; std::string roman; };

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

...}

$ cc -Wall -Werror *.cpp && ./a.out

Can you Show me?

Page 313: Larry and Jen do Roman Numerals in C++

Like this.

#include "to_roman.hpp"

namespace{ struct digit { int arabic; std::string roman; };

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

...}

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Can you Show me?

Page 314: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

namespace{...}

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

Anything else?

Page 315: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

namespace{...}

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

Anything else?

roman += roman statement suggests renaming number

to arabic.

Page 316: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

namespace{...}

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

Anything else?

roman += roman statement suggests renaming number

to arabic.

#include "to_roman.hpp"

namespace{...}

std::string to_roman(int ){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while ( >= digits[i].arabic) { roman += digits[i].roman; -= digits[i].arabic; } return roman;}

Page 317: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

namespace{...}

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

Anything else?

roman += roman statement suggests renaming number

to arabic.

#include "to_roman.hpp"

namespace{...}

std::string to_roman(int ){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while ( >= digits[i].arabic) { roman += digits[i].roman; -= digits[i].arabic; } return roman;}

arabic

arabic

arabic

Page 318: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

namespace{...}

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

$ cc -Wall -Werror *.cpp && ./a.out

Anything else?

roman += roman statement suggests renaming number

to arabic.

#include "to_roman.hpp"

namespace{...}

std::string to_roman(int ){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while ( >= digits[i].arabic) { roman += digits[i].roman; -= digits[i].arabic; } return roman;}

arabic

arabic

arabic

Page 319: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"

namespace{...}

std::string to_roman(int number){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (number >= digits[i].arabic) { roman += digits[i].roman; number -= digits[i].arabic; } return roman;}

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Anything else?

roman += roman statement suggests renaming number

to arabic.

#include "to_roman.hpp"

namespace{...}

std::string to_roman(int ){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while ( >= digits[i].arabic) { roman += digits[i].roman; -= digits[i].arabic; } return roman;}

arabic

arabic

arabic

Page 320: Larry and Jen do Roman Numerals in C++

Here's our tests. #include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C");}

Page 321: Larry and Jen do Roman Numerals in C++

Here's our tests. #include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C");}

I think 200 and 300 should pass.

Page 322: Larry and Jen do Roman Numerals in C++

Here's our tests.

333 too.

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C");}

I think 200 and 300 should pass.

Page 323: Larry and Jen do Roman Numerals in C++

Here's our tests.

333 too.

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C");}

I think 200 and 300 should pass.

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C");

}

Page 324: Larry and Jen do Roman Numerals in C++

Here's our tests.

333 too.

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C");}

I think 200 and 300 should pass.

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C");

}

assert_to_roman(200, "CC"); assert_to_roman(300, "CCC"); assert_to_roman(333, "CCCXXXIII");

Page 325: Larry and Jen do Roman Numerals in C++

Here's our tests.

333 too.

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C");}

I think 200 and 300 should pass.

$ cc -Wall -Werror *.cpp && ./a.out

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C");

}

assert_to_roman(200, "CC"); assert_to_roman(300, "CCC"); assert_to_roman(333, "CCCXXXIII");

Page 326: Larry and Jen do Roman Numerals in C++

Here's our tests.

333 too.

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C");}

I think 200 and 300 should pass.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C");

}

assert_to_roman(200, "CC"); assert_to_roman(300, "CCC"); assert_to_roman(333, "CCCXXXIII");

Page 327: Larry and Jen do Roman Numerals in C++

Here's our tests.

333 too.

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C");}

I think 200 and 300 should pass.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C");

}

assert_to_roman(200, "CC"); assert_to_roman(300, "CCC"); assert_to_roman(333, "CCCXXXIII");Excellent.

Page 328: Larry and Jen do Roman Numerals in C++

We've got duplication in our tests. Let's refactor them.

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C"); assert_to_roman(200, "CC"); assert_to_roman(300, "CCC"); assert_to_roman(333, "CCCXXXIII");}

Page 329: Larry and Jen do Roman Numerals in C++

We've got duplication in our tests. Let's refactor them.

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C"); assert_to_roman(200, "CC"); assert_to_roman(300, "CCC"); assert_to_roman(333, "CCCXXXIII");}

I think it's questionable whether

the duplication in these tests is worth

refactoring.

Page 330: Larry and Jen do Roman Numerals in C++

We've got duplication in our tests. Let's refactor them.

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C"); assert_to_roman(200, "CC"); assert_to_roman(300, "CCC"); assert_to_roman(333, "CCCXXXIII");}

I think it's questionable whether

the duplication in these tests is worth

refactoring.

Let's try it and see if we think it's better.

Page 331: Larry and Jen do Roman Numerals in C++

I'll create an int string struct just like before...

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C"); assert_to_roman(200, "CC"); assert_to_roman(300, "CCC"); assert_to_roman(333, "CCCXXXIII");}

Page 332: Larry and Jen do Roman Numerals in C++

I'll create an int string struct just like before...

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C"); assert_to_roman(200, "CC"); assert_to_roman(300, "CCC"); assert_to_roman(333, "CCCXXXIII");}

#include "to_roman.hpp"#include <cassert>#include <iostream>...

static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C"); assert_to_roman(200, "CC"); assert_to_roman(300, "CCC"); assert_to_roman(333, "CCCXXXIII");}

Page 333: Larry and Jen do Roman Numerals in C++

I'll create an int string struct just like before...

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C"); assert_to_roman(200, "CC"); assert_to_roman(300, "CCC"); assert_to_roman(333, "CCCXXXIII");}

#include "to_roman.hpp"#include <cassert>#include <iostream>...

static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C"); assert_to_roman(200, "CC"); assert_to_roman(300, "CCC"); assert_to_roman(333, "CCCXXXIII");}

struct test_data{ int arabic; std::string roman;};

Page 334: Larry and Jen do Roman Numerals in C++

Now I'll create an array of them. I'll just

do two to start.

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C"); assert_to_roman(200, "CC"); assert_to_roman(300, "CCC"); assert_to_roman(333, "CCCXXXIII");}

Page 335: Larry and Jen do Roman Numerals in C++

Now I'll create an array of them. I'll just

do two to start.

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C"); assert_to_roman(200, "CC"); assert_to_roman(300, "CCC"); assert_to_roman(333, "CCCXXXIII");}

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C"); assert_to_roman(200, "CC"); assert_to_roman(300, "CCC"); assert_to_roman(333, "CCCXXXIII");}

Page 336: Larry and Jen do Roman Numerals in C++

Now I'll create an array of them. I'll just

do two to start.

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C"); assert_to_roman(200, "CC"); assert_to_roman(300, "CCC"); assert_to_roman(333, "CCCXXXIII");}

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C"); assert_to_roman(200, "CC"); assert_to_roman(300, "CCC"); assert_to_roman(333, "CCCXXXIII");}

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

Page 337: Larry and Jen do Roman Numerals in C++

Now I'll use the array.

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){ test_to_roman(); std::cout << "All tests passed" << std::endl;}

Page 338: Larry and Jen do Roman Numerals in C++

Now I'll use the array.

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){ test_to_roman(); std::cout << "All tests passed" << std::endl;}

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){

test_to_roman(); std::cout << "All tests passed" << std::endl;}

Page 339: Larry and Jen do Roman Numerals in C++

Now I'll use the array.

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){ test_to_roman(); std::cout << "All tests passed" << std::endl;}

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){

test_to_roman(); std::cout << "All tests passed" << std::endl;}

for (size_t i = 0; i != 2; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

Page 340: Larry and Jen do Roman Numerals in C++

Now I'll use the array.

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){ test_to_roman(); std::cout << "All tests passed" << std::endl;}

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){

test_to_roman(); std::cout << "All tests passed" << std::endl;}

for (size_t i = 0; i != 2; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

$ cc -Wall -Werror *.cpp && ./a.out

Page 341: Larry and Jen do Roman Numerals in C++

Now I'll use the array.

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){ test_to_roman(); std::cout << "All tests passed" << std::endl;}

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){

test_to_roman(); std::cout << "All tests passed" << std::endl;}

for (size_t i = 0; i != 2; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Page 342: Larry and Jen do Roman Numerals in C++

I'll get rid of the magic number 2.

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){ for (size_t i = 0; i != 2; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

test_to_roman(); std::cout << "All tests passed" << std::endl;}

Page 343: Larry and Jen do Roman Numerals in C++

I'll get rid of the magic number 2.

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){ for (size_t i = 0; i != 2; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

test_to_roman(); std::cout << "All tests passed" << std::endl;}

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){

for (size_t i = 0; i != 2; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

test_to_roman(); std::cout << "All tests passed" << std::endl;}

Page 344: Larry and Jen do Roman Numerals in C++

I'll get rid of the magic number 2.

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){ for (size_t i = 0; i != 2; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

test_to_roman(); std::cout << "All tests passed" << std::endl;}

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){

for (size_t i = 0; i != 2; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

test_to_roman(); std::cout << "All tests passed" << std::endl;}

const size_t n = sizeof tests / sizeof tests[0];

Page 345: Larry and Jen do Roman Numerals in C++

I'll get rid of the magic number 2.

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){ for (size_t i = 0; i != 2; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

test_to_roman(); std::cout << "All tests passed" << std::endl;}

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){

for (size_t i = 0; i != 2; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

test_to_roman(); std::cout << "All tests passed" << std::endl;}

const size_t n = sizeof tests / sizeof tests[0];

n;

Page 346: Larry and Jen do Roman Numerals in C++

I'll get rid of the magic number 2.

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){ for (size_t i = 0; i != 2; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

test_to_roman(); std::cout << "All tests passed" << std::endl;}

$ cc -Wall -Werror *.cpp && ./a.out

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){

for (size_t i = 0; i != 2; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

test_to_roman(); std::cout << "All tests passed" << std::endl;}

const size_t n = sizeof tests / sizeof tests[0];

n;

Page 347: Larry and Jen do Roman Numerals in C++

I'll get rid of the magic number 2.

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){ for (size_t i = 0; i != 2; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

test_to_roman(); std::cout << "All tests passed" << std::endl;}

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};

int main(){

for (size_t i = 0; i != 2; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

test_to_roman(); std::cout << "All tests passed" << std::endl;}

const size_t n = sizeof tests / sizeof tests[0];

n;

Page 348: Larry and Jen do Roman Numerals in C++

I'll fill in the rest of the array.

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};...

Page 349: Larry and Jen do Roman Numerals in C++

I'll fill in the rest of the array.

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};...

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },

};...

Page 350: Larry and Jen do Roman Numerals in C++

I'll fill in the rest of the array.

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};...

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },

};...

{ 3, "III" }, { 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" },

Page 351: Larry and Jen do Roman Numerals in C++

I'll fill in the rest of the array.

$ cc -Wall -Werror *.cpp && ./a.out

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};...

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },

};...

{ 3, "III" }, { 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" },

Page 352: Larry and Jen do Roman Numerals in C++

I'll fill in the rest of the array.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },};...

#include "to_roman.hpp"#include <cassert>#include <iostream>...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" },

};...

{ 3, "III" }, { 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" },

Page 353: Larry and Jen do Roman Numerals in C++

Now I can delete test_to_roman() and

its call.

...const test_data tests[] ={ { 1, "I" }, ... { 333, "CCCXXXIII" },};

static void test_to_roman(){ assert_to_roman( 1, "I"); ... assert_to_roman(333, "CCCXXXIII");}

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

test_to_roman(); std::cout << "All tests passed" << std::endl;}

Page 354: Larry and Jen do Roman Numerals in C++

Now I can delete test_to_roman() and

its call.

...const test_data tests[] ={ { 1, "I" }, ... { 333, "CCCXXXIII" },};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

test_to_roman(); std::cout << "All tests passed" << std::endl;}

Page 355: Larry and Jen do Roman Numerals in C++

Now I can delete test_to_roman() and

its call.

...const test_data tests[] ={ { 1, "I" }, ... { 333, "CCCXXXIII" },};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

std::cout << "All tests passed" << std::endl;}

Page 356: Larry and Jen do Roman Numerals in C++

Now I can delete test_to_roman() and

its call.

$ cc -Wall -Werror *.cpp && ./a.out

...const test_data tests[] ={ { 1, "I" }, ... { 333, "CCCXXXIII" },};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

std::cout << "All tests passed" << std::endl;}

Page 357: Larry and Jen do Roman Numerals in C++

Now I can delete test_to_roman() and

its call.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

...const test_data tests[] ={ { 1, "I" }, ... { 333, "CCCXXXIII" },};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

std::cout << "All tests passed" << std::endl;}

Page 358: Larry and Jen do Roman Numerals in C++

Now I can delete test_to_roman() and

its call.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

...const test_data tests[] ={ { 1, "I" }, ... { 333, "CCCXXXIII" },};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

std::cout << "All tests passed" << std::endl;}

ok

Page 359: Larry and Jen do Roman Numerals in C++

Can you see anything else?

...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, ... { 333, "CCCXXXIII" },};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

std::cout << "All tests passed" << std::endl;}

Page 360: Larry and Jen do Roman Numerals in C++

Can you see anything else?

...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, ... { 333, "CCCXXXIII" },};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) assert_to_roman( tests[i].arabic, tests[i].roman);

std::cout << "All tests passed" << std::endl;}

Perhaps make assert_to_roman()

a method of test_data?

Page 361: Larry and Jen do Roman Numerals in C++

I agree. I'll change the call first.

...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, ... { 333, "CCCXXXIII" },};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i)

std::cout << "All tests passed" << std::endl;}

assert_to_roman( tests[i].arabic, tests[i].roman);

Page 362: Larry and Jen do Roman Numerals in C++

I agree. I'll change the call first.

...struct test_data{ int arabic; std::string roman;};

const test_data tests[] ={ { 1, "I" }, ... { 333, "CCCXXXIII" },};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i)

std::cout << "All tests passed" << std::endl;}

tests[i].assert_equal();

Page 363: Larry and Jen do Roman Numerals in C++

Now I'll add a declaration.

...struct test_data{ int arabic; std::string roman;};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) tests[i].assert_equal();

std::cout << "All tests passed" << std::endl;}

Page 364: Larry and Jen do Roman Numerals in C++

Now I'll add a declaration.

...struct test_data{ int arabic; std::string roman;

};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) tests[i].assert_equal();

std::cout << "All tests passed" << std::endl;}

Page 365: Larry and Jen do Roman Numerals in C++

Now I'll add a declaration.

...struct test_data{ int arabic; std::string roman;

};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) tests[i].assert_equal();

std::cout << "All tests passed" << std::endl;}

void assert_equal() const;

Page 366: Larry and Jen do Roman Numerals in C++

Now I'll rework the definition.

...struct test_data{ int arabic; std::string roman; void assert_equal() const;};

{ const std::string actual = to_roman(arabic); if (expected != actual) { std::cerr << "expected: to_roman" << '(' << arabic << ") == " << '"' << expected << '"' << std::endl; std::cerr << " actual: to_roman" << '(' << arabic << ") == " << '"' << actual << '"' << std::endl; assert(false); }}

static void assert_to_roman(int arabic, const std::string & expected)

Page 367: Larry and Jen do Roman Numerals in C++

Now I'll rework the definition.

...struct test_data{ int arabic; std::string roman; void assert_equal() const;};

{ const std::string actual = to_roman(arabic); if (expected != actual) { std::cerr << "expected: to_roman" << '(' << arabic << ") == " << '"' << expected << '"' << std::endl; std::cerr << " actual: to_roman" << '(' << arabic << ") == " << '"' << actual << '"' << std::endl; assert(false); }}

Page 368: Larry and Jen do Roman Numerals in C++

Now I'll rework the definition.

...struct test_data{ int arabic; std::string roman; void assert_equal() const;};

{ const std::string actual = to_roman(arabic); if (expected != actual) { std::cerr << "expected: to_roman" << '(' << arabic << ") == " << '"' << expected << '"' << std::endl; std::cerr << " actual: to_roman" << '(' << arabic << ") == " << '"' << actual << '"' << std::endl; assert(false); }}

void test_data::assert_equal() const

Page 369: Larry and Jen do Roman Numerals in C++

I need to rename

roman to expected.

...struct test_data{ int arabic; std::string void assert_equal() const;};

void test_data::assert_equal() const{ const std::string actual = to_roman(arabic); if (expected != actual) { std::cerr << "expected: to_roman" << '(' << arabic << ") == " << '"' << expected << '"' << std::endl; std::cerr << " actual: to_roman" << '(' << arabic << ") == " << '"' << actual << '"' << std::endl; assert(false); }}

roman;

Page 370: Larry and Jen do Roman Numerals in C++

I need to rename

roman to expected.

...struct test_data{ int arabic; std::string void assert_equal() const;};

void test_data::assert_equal() const{ const std::string actual = to_roman(arabic); if (expected != actual) { std::cerr << "expected: to_roman" << '(' << arabic << ") == " << '"' << expected << '"' << std::endl; std::cerr << " actual: to_roman" << '(' << arabic << ") == " << '"' << actual << '"' << std::endl; assert(false); }}

Page 371: Larry and Jen do Roman Numerals in C++

I need to rename

roman to expected.

...struct test_data{ int arabic; std::string void assert_equal() const;};

void test_data::assert_equal() const{ const std::string actual = to_roman(arabic); if (expected != actual) { std::cerr << "expected: to_roman" << '(' << arabic << ") == " << '"' << expected << '"' << std::endl; std::cerr << " actual: to_roman" << '(' << arabic << ") == " << '"' << actual << '"' << std::endl; assert(false); }}

expected;

Page 372: Larry and Jen do Roman Numerals in C++

I need to rename

roman to expected.

...struct test_data{ int arabic; std::string void assert_equal() const;};

void test_data::assert_equal() const{ const std::string actual = to_roman(arabic); if (expected != actual) { std::cerr << "expected: to_roman" << '(' << arabic << ") == " << '"' << expected << '"' << std::endl; std::cerr << " actual: to_roman" << '(' << arabic << ") == " << '"' << actual << '"' << std::endl; assert(false); }}

expected;

$ cc -Wall -Werror *.cpp && ./a.out

Page 373: Larry and Jen do Roman Numerals in C++

I need to rename

roman to expected.

...struct test_data{ int arabic; std::string void assert_equal() const;};

void test_data::assert_equal() const{ const std::string actual = to_roman(arabic); if (expected != actual) { std::cerr << "expected: to_roman" << '(' << arabic << ") == " << '"' << expected << '"' << std::endl; std::cerr << " actual: to_roman" << '(' << arabic << ") == " << '"' << actual << '"' << std::endl; assert(false); }}

expected;

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Page 374: Larry and Jen do Roman Numerals in C++

I need to rename

roman to expected.

...struct test_data{ int arabic; std::string void assert_equal() const;};

void test_data::assert_equal() const{ const std::string actual = to_roman(arabic); if (expected != actual) { std::cerr << "expected: to_roman" << '(' << arabic << ") == " << '"' << expected << '"' << std::endl; std::cerr << " actual: to_roman" << '(' << arabic << ") == " << '"' << actual << '"' << std::endl; assert(false); }}

expected;

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

ok

Page 375: Larry and Jen do Roman Numerals in C++

Anything else?

Page 376: Larry and Jen do Roman Numerals in C++

Anything else?

The name test_data is a bit weak.

Page 377: Larry and Jen do Roman Numerals in C++

Anything else?

The name test_data is a bit weak.

And it should not have external linkage.

Page 378: Larry and Jen do Roman Numerals in C++

Anything else?

The name test_data is a bit weak.

Agreed.

And it should not have external linkage.

Page 379: Larry and Jen do Roman Numerals in C++

Anything else?

The name test_data is a bit weak.

Agreed.

And it should not have external linkage.

But we can come back to that later.

Page 380: Larry and Jen do Roman Numerals in C++
Page 381: Larry and Jen do Roman Numerals in C++

Let's compare the tests before and after the

refactoring

Page 382: Larry and Jen do Roman Numerals in C++

Let's compare the tests before and after the

refactoring

Yeah! I forgot about that.

Page 383: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C"); assert_to_roman(200, "CC"); assert_to_roman(300, "CCC"); assert_to_roman(333, "CCCXXXIII");}

int main(){ test_to_roman(); std::cout << "All tests passed" << std::endl;}

Here's the tests before we

refactored them.

Page 384: Larry and Jen do Roman Numerals in C++

#include "to_roman.hpp"#include <cassert>#include <iostream>...static void test_to_roman(){ assert_to_roman( 1, "I"); assert_to_roman( 2, "II"); assert_to_roman( 3, "III"); assert_to_roman( 10, "X"); assert_to_roman( 20, "XX"); assert_to_roman( 30, "XXX"); assert_to_roman( 33, "XXXIII"); assert_to_roman(100, "C"); assert_to_roman(200, "CC"); assert_to_roman(300, "CCC"); assert_to_roman(333, "CCCXXXIII");}

int main(){ test_to_roman(); std::cout << "All tests passed" << std::endl;}

Its directness is quite elegant.

Here's the tests before we

refactored them.

Page 385: Larry and Jen do Roman Numerals in C++

...struct test_data{ int arabic; std::string expected; void assert_equal() const;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" }, { 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" },};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) tests[i].assert_equal();

std::cout << "All tests passed" << std::endl;}

Here's the tests after we refactored

them.

Page 386: Larry and Jen do Roman Numerals in C++

It feels quite different. And

there's more lines of code! I'm not sure

which I prefer.

...struct test_data{ int arabic; std::string expected; void assert_equal() const;};

const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" }, { 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" },};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) tests[i].assert_equal();

std::cout << "All tests passed" << std::endl;}

Here's the tests after we refactored

them.

Page 387: Larry and Jen do Roman Numerals in C++

What now?

Page 388: Larry and Jen do Roman Numerals in C++

What now?

How about adding a test for 5 being V.

Page 389: Larry and Jen do Roman Numerals in C++

What now?

How about adding a test for 5 being V.

ok

Page 390: Larry and Jen do Roman Numerals in C++

I just have to add one more entry to the

tests array.

...const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" }, { 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" },};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) tests[i].assert_equal();

std::cout << "All tests passed" << std::endl;}

Page 391: Larry and Jen do Roman Numerals in C++

I just have to add one more entry to the

tests array.

...const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" },

{ 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" },};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) tests[i].assert_equal();

std::cout << "All tests passed" << std::endl;}

Page 392: Larry and Jen do Roman Numerals in C++

I just have to add one more entry to the

tests array.

...const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" },

{ 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" },};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) tests[i].assert_equal();

std::cout << "All tests passed" << std::endl;}

{ 5, "V" },

Page 393: Larry and Jen do Roman Numerals in C++

I just have to add one more entry to the

tests array.

...const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" },

{ 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" },};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) tests[i].assert_equal();

std::cout << "All tests passed" << std::endl;}

{ 5, "V" },

$ cc -Wall -Werror *.cpp && ./a.out

Page 394: Larry and Jen do Roman Numerals in C++

I just have to add one more entry to the

tests array.

...const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" },

{ 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" },};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) tests[i].assert_equal();

std::cout << "All tests passed" << std::endl;}

{ 5, "V" },

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outexpected: to_roman(5) == "V" actual: to_roman(5) == "IIIII"Assertion failed: (false), ...

Page 395: Larry and Jen do Roman Numerals in C++

I just have to add one more entry to the

tests array.

It fails as expected.

...const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" },

{ 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" },};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) tests[i].assert_equal();

std::cout << "All tests passed" << std::endl;}

{ 5, "V" },

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outexpected: to_roman(5) == "V" actual: to_roman(5) == "IIIII"Assertion failed: (false), ...

Page 396: Larry and Jen do Roman Numerals in C++

Now to make it pass.

...namespace{

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

}

std::string to_roman(int arabic){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (arabic >= digits[i].arabic) { roman += digits[i].roman; arabic -= digits[i].arabic; } return roman;}

Page 397: Larry and Jen do Roman Numerals in C++

Now to make it pass.

...namespace{

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

}

std::string to_roman(int arabic){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (arabic >= digits[i].arabic) { roman += digits[i].roman; arabic -= digits[i].arabic; } return roman;}

I just need to say the digit

5 is V.

Page 398: Larry and Jen do Roman Numerals in C++

Now to make it pass.

I just need to say the digit

5 is V.

...namespace{

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" },

{ 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

}

std::string to_roman(int arabic){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (arabic >= digits[i].arabic) { roman += digits[i].roman; arabic -= digits[i].arabic; } return roman;}

Page 399: Larry and Jen do Roman Numerals in C++

Now to make it pass.

I just need to say the digit

5 is V.

...namespace{

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" },

{ 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

}

std::string to_roman(int arabic){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (arabic >= digits[i].arabic) { roman += digits[i].roman; arabic -= digits[i].arabic; } return roman;}

{ 5, "V" },

Page 400: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out

Now to make it pass.

I just need to say the digit

5 is V.

...namespace{

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" },

{ 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

}

std::string to_roman(int arabic){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (arabic >= digits[i].arabic) { roman += digits[i].roman; arabic -= digits[i].arabic; } return roman;}

{ 5, "V" },

Page 401: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out

Now to make it pass.

I just need to say the digit

5 is V.

...namespace{

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" },

{ 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

}

std::string to_roman(int arabic){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (arabic >= digits[i].arabic) { roman += digits[i].roman; arabic -= digits[i].arabic; } return roman;}

{ 5, "V" },

$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Page 402: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out

Now to make it pass.

Excellent

I just need to say the digit

5 is V.

...namespace{

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" },

{ 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

}

std::string to_roman(int arabic){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (arabic >= digits[i].arabic) { roman += digits[i].roman; arabic -= digits[i].arabic; } return roman;}

{ 5, "V" },

$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

Page 403: Larry and Jen do Roman Numerals in C++

Numbers like 6 and 338 should now pass.

...const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" }, { 5, "V" }, { 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" },};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) tests[i].assert_equal();

std::cout << "All tests passed" << std::endl;}

Page 404: Larry and Jen do Roman Numerals in C++

Numbers like 6 and 338 should now pass.

Agreed.

...const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" }, { 5, "V" }, { 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" },};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) tests[i].assert_equal();

std::cout << "All tests passed" << std::endl;}

Page 405: Larry and Jen do Roman Numerals in C++

Numbers like 6 and 338 should now pass.

Agreed.

...const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" }, { 5, "V" },

{ 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" },

};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) tests[i].assert_equal();

std::cout << "All tests passed" << std::endl;}

Page 406: Larry and Jen do Roman Numerals in C++

Numbers like 6 and 338 should now pass.

Agreed.

...const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" }, { 5, "V" },

{ 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" },

};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) tests[i].assert_equal();

std::cout << "All tests passed" << std::endl;}

{ 6, "VI" },

{ 338, "CCCXXXVIII" },

Page 407: Larry and Jen do Roman Numerals in C++

Numbers like 6 and 338 should now pass.

Agreed.

$ cc -Wall -Werror *.cpp && ./a.out

...const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" }, { 5, "V" },

{ 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" },

};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) tests[i].assert_equal();

std::cout << "All tests passed" << std::endl;}

{ 6, "VI" },

{ 338, "CCCXXXVIII" },

Page 408: Larry and Jen do Roman Numerals in C++

Numbers like 6 and 338 should now pass.

Agreed.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

...const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" }, { 5, "V" },

{ 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" },

};

int main(){ const size_t n = sizeof tests / sizeof tests[0];

for (size_t i = 0; i != n; ++i) tests[i].assert_equal();

std::cout << "All tests passed" << std::endl;}

{ 6, "VI" },

{ 338, "CCCXXXVIII" },

Page 409: Larry and Jen do Roman Numerals in C++

Let's do 4 is IV now.

Page 410: Larry and Jen do Roman Numerals in C++

Let's do 4 is IV now.

ok

Page 411: Larry and Jen do Roman Numerals in C++

Let's do 4 is IV now.

ok

We already have 1 is I and 5 is V. Perhaps we could

somehow combine them to make IV.

Page 412: Larry and Jen do Roman Numerals in C++

Let's do 4 is IV now.

ok

We already have 1 is I and 5 is V. Perhaps we could

somehow combine them to make IV.

Hmmm...

Page 413: Larry and Jen do Roman Numerals in C++

Let's do 4 is IV now.

ok

We already have 1 is I and 5 is V. Perhaps we could

somehow combine them to make IV.

Hmmm...

I think it will get quite tricky.

Page 414: Larry and Jen do Roman Numerals in C++

Let's do 4 is IV now.

ok

We already have 1 is I and 5 is V. Perhaps we could

somehow combine them to make IV.

Hmmm...

I think it will get quite tricky.

Yes...

Page 415: Larry and Jen do Roman Numerals in C++

What are you thinking?

Page 416: Larry and Jen do Roman Numerals in C++

What are you thinking?

I'm thinking that when things get difficult it can be a sign that

you're doing it wrong.

Page 417: Larry and Jen do Roman Numerals in C++

What are you thinking?

I'm thinking that when things get difficult it can be a sign that

you're doing it wrong.

We could just add a new 4 is IV entry in the digits array! It

seems like a hack though.

Page 418: Larry and Jen do Roman Numerals in C++

What are you thinking?

I'm thinking that when things get difficult it can be a sign that

you're doing it wrong.

We could just add a new 4 is IV entry in the digits array! It

seems like a hack though.

I think that's a great idea. I don't think it's a hack at all! It's the simplest thing that works.

Page 419: Larry and Jen do Roman Numerals in C++

What are you thinking?

I'm thinking that when things get difficult it can be a sign that

you're doing it wrong.

We could just add a new 4 is IV entry in the digits array! It

seems like a hack though.

I think that's a great idea. I don't think it's a hack at all! It's the simplest thing that works.

ok!

Page 420: Larry and Jen do Roman Numerals in C++

Here's the 4 is IV test.

...const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" }, { 5, "V" }, { 6, "VI" }, { 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" }, { 338, "CCCXXXVIII" },};

Page 421: Larry and Jen do Roman Numerals in C++

Here's the 4 is IV test.

...const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" },

{ 5, "V" }, { 6, "VI" }, { 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" }, { 338, "CCCXXXVIII" },};

Page 422: Larry and Jen do Roman Numerals in C++

Here's the 4 is IV test.

...const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" },

{ 5, "V" }, { 6, "VI" }, { 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" }, { 338, "CCCXXXVIII" },};

{ 4, "IV" },

Page 423: Larry and Jen do Roman Numerals in C++

Here's the 4 is IV test.

$ cc -Wall -Werror *.cpp && ./a.out

...const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" },

{ 5, "V" }, { 6, "VI" }, { 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" }, { 338, "CCCXXXVIII" },};

{ 4, "IV" },

Page 424: Larry and Jen do Roman Numerals in C++

Here's the 4 is IV test.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outexpected: to_roman(4) == "IV" actual: to_roman(4) == "IIII"Assertion failed: (false), ...

...const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" },

{ 5, "V" }, { 6, "VI" }, { 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" }, { 338, "CCCXXXVIII" },};

{ 4, "IV" },

Page 425: Larry and Jen do Roman Numerals in C++

Here's the 4 is IV test.

$ cc -Wall -Werror *.cpp && ./a.out$ cc -Wall -Werror *.cpp && ./a.outexpected: to_roman(4) == "IV" actual: to_roman(4) == "IIII"Assertion failed: (false), ...

...const test_data tests[] ={ { 1, "I" }, { 2, "II" }, { 3, "III" },

{ 5, "V" }, { 6, "VI" }, { 10, "X" }, { 20, "XX" }, { 30, "XXX" }, { 33, "XXXIII" }, { 100, "C" }, { 200, "CC" }, { 300, "CCC" }, { 333, "CCCXXXIII" }, { 338, "CCCXXXVIII" },};

{ 4, "IV" },

It fails as expected.

Page 426: Larry and Jen do Roman Numerals in C++

Now to make it pass.

...namespace{

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 5, "V" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

}

std::string to_roman(int arabic){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (arabic >= digits[i].arabic) { roman += digits[i].roman; arabic -= digits[i].arabic; } return roman;}

Page 427: Larry and Jen do Roman Numerals in C++

Now to make it pass.

...namespace{

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 5, "V" }, { 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

}

std::string to_roman(int arabic){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (arabic >= digits[i].arabic) { roman += digits[i].roman; arabic -= digits[i].arabic; } return roman;}

I just need to say the digit

4 is IV.

Page 428: Larry and Jen do Roman Numerals in C++

Now to make it pass.

I just need to say the digit

4 is IV.

...namespace{

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 5, "V" },

{ 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

}

std::string to_roman(int arabic){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (arabic >= digits[i].arabic) { roman += digits[i].roman; arabic -= digits[i].arabic; } return roman;}

Page 429: Larry and Jen do Roman Numerals in C++

Now to make it pass.

I just need to say the digit

4 is IV.

...namespace{

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 5, "V" },

{ 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

}

std::string to_roman(int arabic){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (arabic >= digits[i].arabic) { roman += digits[i].roman; arabic -= digits[i].arabic; } return roman;}

{ 4, "IV" },

Page 430: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out

Now to make it pass.

I just need to say the digit

4 is IV.

...namespace{

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 5, "V" },

{ 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

}

std::string to_roman(int arabic){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (arabic >= digits[i].arabic) { roman += digits[i].roman; arabic -= digits[i].arabic; } return roman;}

{ 4, "IV" },

Page 431: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out

Now to make it pass.

I just need to say the digit

4 is IV.

$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

...namespace{

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 5, "V" },

{ 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

}

std::string to_roman(int arabic){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (arabic >= digits[i].arabic) { roman += digits[i].roman; arabic -= digits[i].arabic; } return roman;}

{ 4, "IV" },

Page 432: Larry and Jen do Roman Numerals in C++

$ cc -Wall -Werror *.cpp && ./a.out

Now to make it pass.

Excellent

I just need to say the digit

4 is IV.

$ cc -Wall -Werror *.cpp && ./a.outAll tests passed

...namespace{

struct digit{ int arabic; std::string roman;};

const digit digits[] = { { 100, "C" }, { 10, "X" }, { 5, "V" },

{ 1, "I" }, };

const std::size_t n = sizeof digits / sizeof digits[0];

}

std::string to_roman(int arabic){ std::string roman = ""; for (std::size_t i = 0; i != n; ++i) while (arabic >= digits[i].arabic) { roman += digits[i].roman; arabic -= digits[i].arabic; } return roman;}

{ 4, "IV" },

Page 433: Larry and Jen do Roman Numerals in C++

We can repeat that idea for all the digits such as 9 being IX and 40 being XL.

Page 434: Larry and Jen do Roman Numerals in C++

We can repeat that idea for all the digits such as 9 being IX and 40 being XL.

Agreed.

Page 435: Larry and Jen do Roman Numerals in C++

We can repeat that idea for all the digits such as 9 being IX and 40 being XL.

Agreed.

So we've done it. It's just more of the same now.

Page 436: Larry and Jen do Roman Numerals in C++

We can repeat that idea for all the digits such as 9 being IX and 40 being XL.

Agreed.

So we've done it. It's just more of the same now.

Yes. I'm sure there'll be a few more small refactorings though. And we've an external linkage

todo from earlier.

Page 437: Larry and Jen do Roman Numerals in C++

We can repeat that idea for all the digits such as 9 being IX and 40 being XL.

Agreed.

So we've done it. It's just more of the same now.

Yes. I'm sure there'll be a few more small refactorings though. And we've an external linkage

todo from earlier.

Thanks for your help.

Page 438: Larry and Jen do Roman Numerals in C++

We can repeat that idea for all the digits such as 9 being IX and 40 being XL.

Agreed.

So we've done it. It's just more of the same now.

Yes. I'm sure there'll be a few more small refactorings though. And we've an external linkage

todo from earlier.

Thanks for your help.

Any time.