I was told that I could compact this code in 2 ways. I can't use loops. Can anyone see it?

#include <iostream>
#include <string>
using namespace std;
void daysOfChristmas (int days, char switcher, string b[12]){
    (days > 0 && switcher == 'y') ? daysOfChristmas(days - 1, 'y', b): false; 
    (days > 0) ? daysOfChristmas(days - 1, 'n', b): false;
    cout << "on the " << days << " day of Christmas my true love gave me " << b[(days-1)] << endl;
}
int main() {
    string gifts [12] = {"a partridge in a Pear Tree", "two turtle doves", "three french hens", "four colly birds", "five golden rings", "six gees-a-laying", "seven swans-a-swimming", "eight maids a miliking", "nine ladies dancing", "ten lords a leaping", "eleven pipiers piping", "drummers drumming"};
    daysOfChristmas(12, 'y', gifts);}

Recommended Answers

All 2 Replies

Welcome to DaniWeb. To help us help you most effectively, please use a descriptive thread title. Please also use code tags when you post code.

It looks pretty "compact" (aka hard to read!) already to me!

David

I switched made the function return a bool because you are returning false on a void function and ran it and it "broke" and made my motherboard speaker go off like a christmas song for like 3 min.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.