Thread: If/Else Switch Case problem

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    If/Else Switch Case problem

    Hello! My brother recommended this site to me, he seemed to learn a lot from here. I'm working on If/Else statements for a class, and I thought I had it down until I was thrown this curve ball.

    "Create an application that prints the verses of the song “The Twelve Days of Christmas”, in which the user enters the day (1-12) and each verse is printed. Use switch/case statements to break each pick into the verse. Also, make sure to use the “default” with the case statements if the user types an invalid number. On the twelfth day of Christmas, my true love sent to me, Twelve drummers drumming,
    On the eleventh day of Christmas, my true love sent to me, Eleven pipers piping,
    On the tenth day of Christmas, my true love sent to me, Ten lords a-leaping,
    On the ninth day of Christmas, my true love sent to me, Nine ladies dancing,
    On the eighth day of Christmas, my true love sent to me, Eight maids a-milking,
    On the seventh day of Christmas, my true love sent to me, Seven swans a-swimming,
    On the sixth day of Christmas, my true love sent to me, Six geese a-laying,
    On the fifth day of Christmas, my true love sent to me, Five golden rings,
    On the fourth day of Christmas, my true love sent to me, Four calling birds,
    On the third day of Christmas, my true love sent to me, Three French hens,
    On the second day of Christmas, my true love sent to me, Two turtle doves,
    On the first day of Christmas, my true love sent to me, A partridge in a pear tree.

    By the way, when a user hits 5, it should display phrases 5 – 1. When a user hits 11, it should display lines 11 – 1.

    Hint 1: Inside the case structure, stick with the SAME order of phrases that I have above.
    Hint 2: Loose the “break”.
    Hint 3: case 10: System.out.println(“On the tenth day of Christmas, my true love sent to me, Ten lords a-leaping, “);"


    I was never taught anything about case 10 or system.out.println. So I'm kinda lost. If anyone could just point me in the right direction I would be very grateful!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Pfinferno
    I was never taught anything about case 10 or system.out.println.
    The case 10 thing is just an example, similiar to what you have seen with switch statements and case labels. The system.out.println is likely to be the result of your instructor "porting" over this question from another class he/she taught, but forgetting to change the text. Use puts or printf instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Here's a hint for you... this program is real easy and it can work because of a common error people make when writing switch()-case statements. You can actually break out of the mold and take advantage of it.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    2
    Ah yes this turned out to be a lot easier than I thought. Thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch and case problem.
    By jaja009 in forum C Programming
    Replies: 6
    Last Post: 03-29-2010, 08:15 AM
  2. switch/case problem
    By maxilog1009 in forum C Programming
    Replies: 3
    Last Post: 09-07-2009, 08:35 PM
  3. switch/case problem?
    By Wiiplayer12 in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2008, 11:13 PM
  4. case switch problem
    By digdug4life in forum C++ Programming
    Replies: 10
    Last Post: 10-08-2005, 09:25 AM
  5. Switch/case problem
    By Ivan! in forum C++ Programming
    Replies: 4
    Last Post: 01-31-2003, 11:48 AM