Jumping To C++ - Chapter 8 Problem

This is a discussion on Jumping To C++ - Chapter 8 Problem within the C++ Programming forums, part of the General Programming Boards category; Hello I Am New In This Forum Name : Mohamed Age : 16 And A C++ Beginner I Am Learning ...

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    3

    Question 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. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    It looks pretty good to me.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,292
    Quote Originally Posted by whiteflags View Post
    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
    Code:
    namespace life
    {
        const bool change = true;
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,693
    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. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Jumping into C++ chapter 7 help
    By DarthOrmus in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2013, 01:48 AM
  2. Jumping into C++ ebook problem
    By tonyk in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2012, 04:19 AM
  3. Jumping into C++ PracticeH Problem
    By Sid_TheBeginner in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2012, 09:00 AM
  4. Replies: 6
    Last Post: 08-20-2012, 07:09 AM
  5. Jumping into C++ Chapter 5 problem 6 - Critique please
    By Kranky in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2012, 04:44 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21