# Thread: Jumping To C++ - Chapter 8 Problem

1. ## Jumping To C++ - Chapter 8 Problem

Hello I Am New In This Forum
Name : Mohamed
Age : 16
And A C++ Beginner

I Am Learning C++ From Jumping To C++ Book
I Am In Chapter 8 Practice Problem 2 It Says
-----------------------------------------------------------
Write a program that outputs the results of the 12 days of Christmas20 using switch-case (hint:
you might want to take advantage of fall-through cases)
-----------------------------------------------------------
So I Wrote This Program I Need To Know Is That What The Above Practice Problem Means ? Is My Code Right ?

Code:
```// Chapter 8 - Practical Problems[1]
#include <iostream>
#include <string>
using namespace std;
// Lyrics

string lyric_start = "my true love sent to me:" , lyric_twelve = "12 Drummers Drumming" , lyric_eleven = "Eleven Pipers Piping";
string lyric_ten = "Ten Lords a Leaping" , lyric_nine = "Nine Ladies Dancing" , lyric_eight = "Eight Maids a Milking";
string lyric_seven = "Seven Swans a Swimming" , lyric_six = "Six Geese a Laying" , lyric_five = "Five Golden Rings";
string lyric_four = "Four Calling Birds" , lyric_three = "Three French Hens" , lyric_two = "Two Turtle Doves" , lyric_end = "and a Partridge in a Pear Tree" ;

enum xmas {frist_day , second_day , third_day , fourth_day , fifth_day , sixth_day , seventh_day , eighth_day, nineth_day , tenth_day, eleven_day , twelve_day};
xmas xmas_choose = frist_day;

void Frist_day(){ // New Function To Display 1st Day Result [Note The Capital F In Frist]
cout << "On the frist day of Christmas\n";
cout << lyric_start << endl;
cout << lyric_end << endl;
}
void Second_day(){ // New Function To Display 2nd Day Result [Note The Capital S In Frist]
cout << "\nOn the second day of Christmas\n";
cout << lyric_start << endl;
cout << lyric_two << endl;
cout << lyric_end << endl;
}
void Third_day(){ // New Function To Display 3rd Day Result [Note The Capital T In Frist]
cout << "\nOn the three day of Christmas\n";
cout << lyric_start << endl;
cout << lyric_three << endl;
cout << lyric_two << endl;
cout << lyric_end << endl;
}
void Fourth_day(){ // New Function To Display 4th Day Result [Note The Capital F In Frist]
cout << "\nOn the fourth day of Christmas\n";
cout << lyric_start << endl;
cout << lyric_four << endl;
cout << lyric_three << endl;
cout << lyric_two << endl;
cout << lyric_end << endl;
}
void Fifth_day(){ // New Function To Display 5th Day Result [Note The Capital F In Frist]
cout << "\nOn the fifth day of Christmas\n";
cout << lyric_start << endl;
cout << lyric_five << endl;
cout << lyric_four << endl;
cout << lyric_three << endl;
cout << lyric_two << endl;
cout << lyric_end << endl;
}
void Sixth_day(){ // New Function To Display 6th Day Result [Note The Capital S In Frist]
cout << "\nOn the sixth day of Christmas\n";
cout << lyric_start << endl;
cout << lyric_six << endl;
cout << lyric_five << endl;
cout << lyric_four << endl;
cout << lyric_three << endl;
cout << lyric_two << endl;
cout << lyric_end << endl;
}
void Seventh_day(){ // New Function To Display 7th Day Result [Note The Capital S In Frist]
cout << "\nOn the seventh day of Christmas\n";
cout << lyric_start << endl;
cout << lyric_seven << endl;
cout << lyric_six << endl;
cout << lyric_five << endl;
cout << lyric_four << endl;
cout << lyric_three << endl;
cout << lyric_two << endl;
cout << lyric_end << endl;
}
void Eighth_day(){ // New Function To Display 8th Day Result [Note The Capital E In Frist]
cout << "\nOn the eighth day of Christmas\n";
cout << lyric_start << endl;
cout << lyric_eight << endl;
cout << lyric_seven << endl;
cout << lyric_six << endl;
cout << lyric_five << endl;
cout << lyric_four << endl;
cout << lyric_three << endl;
cout << lyric_two << endl;
cout << lyric_end << endl;
}
void Nineth_day(){ // New Function To Display 9th Day Result [Note The Capital N In Frist]
cout << "\nOn the nineth day of Christmas\n";
cout << lyric_start << endl;
cout << lyric_nine << endl;
cout << lyric_eight << endl;
cout << lyric_seven << endl;
cout << lyric_six << endl;
cout << lyric_five << endl;
cout << lyric_four << endl;
cout << lyric_three << endl;
cout << lyric_two << endl;
cout << lyric_end << endl;
}
void Tenth_day(){ // New Function To Display 10th Day Result [Note The Capital T In Frist]
cout << "\nOn the tenth day of Christmas\n";
cout << lyric_start << endl;
cout << lyric_ten << endl;
cout << lyric_nine << endl;
cout << lyric_eight << endl;
cout << lyric_seven << endl;
cout << lyric_six << endl;
cout << lyric_five << endl;
cout << lyric_four << endl;
cout << lyric_three << endl;
cout << lyric_two << endl;
cout << lyric_end << endl;
}
void Eleven_day(){ // New Function To Display 11 Day Result [Note The Capital E In Frist]
cout << "\nOn the eleventh day of Christmas\n";
cout << lyric_start << endl;
cout << lyric_eleven << endl;
cout << lyric_ten << endl;
cout << lyric_nine << endl;
cout << lyric_eight << endl;
cout << lyric_seven << endl;
cout << lyric_six << endl;
cout << lyric_five << endl;
cout << lyric_four << endl;
cout << lyric_three << endl;
cout << lyric_two << endl;
cout << lyric_end << endl;
}
void Twelve_day(){ // New Function To Display 12 Day Result [Note The Capital T In Frist]
cout << "\nOn the twelveth of Christmas\n";
cout << lyric_start << endl;
cout << lyric_twelve << endl;
cout << lyric_eleven << endl;
cout << lyric_ten << endl;
cout << lyric_nine << endl;
cout << lyric_eight << endl;
cout << lyric_seven << endl;
cout << lyric_six << endl;
cout << lyric_five << endl;
cout << lyric_four << endl;
cout << lyric_three << endl;
cout << lyric_two << endl;
cout << lyric_end << endl;
}

// Main Function
int main ()
{
switch (xmas_choose){
case frist_day : Frist_day();
case second_day : Second_day();
case third_day : Third_day();
case fourth_day : Fourth_day();
case fifth_day : Fifth_day();
case sixth_day : Sixth_day();
case seventh_day : Seventh_day();
case eighth_day : Eighth_day();
case nineth_day : Nineth_day();
case tenth_day : Tenth_day();
case eleven_day : Eleven_day();
case twelve_day : Twelve_day();
}
// Extra Code For Pasuing The Program
cin.ignore();
cin.get();
}```
Sorry For My Bad English

2. It looks pretty good to me.

3. Originally Posted by whiteflags
It looks pretty good to me.
except that the output for the first day will be:

Code:
```On the frist day of Christmas
my true love sent to me:
and a Partridge in a Pear Tree```

4. There also seems to be a lot of code duplication. I'd recommend a loop to increment the "day". Remember each "day" you add another item from the list the first day starts as:

On the first day of Christmas
My true love sent to me
A partridge in a pear tree.

The second day:

On the second day of Christmas
My true love sent to me
Two turtle doves
and a partridge in a pear tree.

Third day:

On the second day of Christmas
My true love sent to me
Three french hens.
Two turtle doves.
and a partridge in a pear tree.

And so forth until you reach the last day and every thing is printed.

Jim

5. Well considering that the interesting answer is only 12 lines of code, IF the programmer pays attention to the assignment and makes a careful note about switch statements and fall-through.

Popular pages Recent additions