Results 1 to 22 of 22

Thread: Need some help with a program...

Hybrid View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2001
    Posts
    23

    Need some help with a program...

    I am pretty new to C++ and the whole programming bit. I am trying to write a program that will display the "Twelve Days of Christmas" song, and need some help... I see there is a problem using apstring to intialize day_of_christmas, and don't know what else to use. Any help is appreciated! Thanks in advance!

    Code:
    /*
    Josh
    christmas.cpp
    */
    
    #include <iostream.h>
    #include <apstring.h>
    
    int main()
    {
    	apstring day_of_christmas;
    	
    	switch(day_of_christmas) {
    	case 12:
    		cout << "Twelth";
    	case 11:
    		cout << "Eleventh";
    	case 10:
    		cout << "Tenth";
    	case 9:
    		cout << "Ninth";
    	case 8:
    		cout << "Eighth";
    	case 7:
    		cout << "Seventh";
    	case 6:
    		cout << "Sixth";
    	case 5:
    		cout << "Fifth";
    	case 4:
    		cout << "Fourth";
    	case 3:
    		cout << "Third";
    	case 2:
    		cout << "Second";
    	case 1:
    		cout << "First";
    	}
    
    	cout << "On the " << day_of_christmas << " day of Christmas, " << endl;
    	cout << "My true love gave to me " << endl;
    
    	switch(day_of_christmas) {
    	case 12:
    		cout << "Twelve drummers drumming";
    	case 11:
    		cout << "Eleven pipers piping";
    	case 10:
    		cout << "Ten lords a-leaping";
    	case 9:
    		cout << "Nine ladies dancing";
    	case 8:
    		cout << "Eight maids a-milking";
    	case 7:
    		cout << "Seven swans a-swimming";
    	case 6:
    		cout << "Six geese a laying";
    	case 5:
    		cout << "Five golden rings!";
    	case 4:
    		cout << "Four calling birds";
    	case 3:
    		cout << "Three french hens";
    	case 2:
    		cout << "Two turtle doves";
    	case 1:
    		cout << "A partridge in a pear tree";
    	}
    
    	return 0;
    }

  2. #2
    You can't do a switch on an apstring; you've got to do a bunch of ifs and else ifs.

  3. #3
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800

    Re: Need some help with a program...

    Well first of all...day_of_christmas never changes. you have switch statement, but it will never run....like saying if day of christmas is 12 then....when it hasn't been set to anything. Put this in a loop and have it increment each time
    Originally posted by mysteryman
    Code:
    /*
    Josh
    christmas.cpp
    */
    
    #include <iostream.h>
    #include <apstring.h>
    
    int main()
    {
    	apstring day_of_christmas;
    	
    	switch(day_of_christmas) {
    	case 12:
    		cout << "Twelth";
    	case 11:
    		cout << "Eleventh";
    	case 10:
    		cout << "Tenth";
    	case 9:
    		cout << "Ninth";
    	case 8:
    		cout << "Eighth";
    	case 7:
    		cout << "Seventh";
    	case 6:
    		cout << "Sixth";
    	case 5:
    		cout << "Fifth";
    	case 4:
    		cout << "Fourth";
    	case 3:
    		cout << "Third";
    	case 2:
    		cout << "Second";
    	case 1:
    		cout << "First";
    	}
    
    	cout << "On the " << day_of_christmas << " day of Christmas, " << endl;
    	cout << "My true love gave to me " << endl;
    
    	switch(day_of_christmas) {
    	case 12:
    		cout << "Twelve drummers drumming";
    	case 11:
    		cout << "Eleven pipers piping";
    	case 10:
    		cout << "Ten lords a-leaping";
    	case 9:
    		cout << "Nine ladies dancing";
    	case 8:
    		cout << "Eight maids a-milking";
    	case 7:
    		cout << "Seven swans a-swimming";
    	case 6:
    		cout << "Six geese a laying";
    	case 5:
    		cout << "Five golden rings!";
    	case 4:
    		cout << "Four calling birds";
    	case 3:
    		cout << "Three french hens";
    	case 2:
    		cout << "Two turtle doves";
    	case 1:
    		cout << "A partridge in a pear tree";
    	}
    
    	return 0;
    }

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2001
    Posts
    23
    I can only use repetition and switch structures to do this... I need to use one switch structure for the day (First, Second, etc), and one switch structure for the rest of the verse. Thats where I am stuck, it is possible to do it with an if/else or for loop, but I can't figure out how to do it with two switch structures...

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2001
    Posts
    23
    Apstring is like a regular string, but is easier to use (in my opinion).

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2001
    Posts
    23
    Okay, here is what I have so far... Still need some help to get it working...

    Code:
    #include <iostream.h>
    
    int main()
    {
    	char day_of_christmas[10];
    	
    	switch(day_of_christmas) {
    	case 12:
    		cout << "Twelth";
    	case 11:
    		cout << "Eleventh";
    	case 10:
    		cout << "Tenth";
    	case 9:
    		cout << "Ninth";
    	case 8:
    		cout << "Eighth";
    	case 7:
    		cout << "Seventh";
    	case 6:
    		cout << "Sixth";
    	case 5:
    		cout << "Fifth";
    	case 4:
    		cout << "Fourth";
    	case 3:
    		cout << "Third";
    	case 2:
    		cout << "Second";
    	case 1:
    		cout << "First";
    	}
    
    	cout << "On the " << day_of_christmas << " day of Christmas, " << endl;
    	cout << "My true love gave to me " << endl;
    
    	switch(day_of_christmas) {
    	case 12:
    		cout << "Twelve drummers drumming";
    	case 11:
    		cout << "Eleven pipers piping";
    	case 10:
    		cout << "Ten lords a-leaping";
    	case 9:
    		cout << "Nine ladies dancing";
    	case 8:
    		cout << "Eight maids a-milking";
    	case 7:
    		cout << "Seven swans a-swimming";
    	case 6:
    		cout << "Six geese a laying";
    	case 5:
    		cout << "Five golden rings!";
    	case 4:
    		cout << "Four calling birds";
    	case 3:
    		cout << "Three french hens";
    	case 2:
    		cout << "Two turtle doves";
    	case 1:
    		cout << "A partridge in a pear tree";
    	}
    
    	return 0;
    }

  7. #7
    You don't need to make an array of chars, just use a single char. A char is really just a number that happens to represent an ASCII character.

  8. #8
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    well you can't store "Eleventh" in one character!

  9. #9
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    put it in a loop..you aren't setting it to anything..

  10. #10
    Originally posted by SteveCRM
    well you can't store "Eleventh" in one character!
    He's not storing a string, only a number.

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Just as a note, apstring is now offically deprecated in favour of string
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    woops sorry filbut....read that a little too fast then why not use an int???

    mike: I hated string....thats the cheating way do you have a copy of apstring? I dont think my school will like me taking their files

  13. #13
    Originally posted by SteveCRM
    woops sorry filbut....read that a little too fast then why not use an int???
    A char takes up only one byte of RAM.

  14. #14
    Originally posted by SteveCRM
    mike: I hated string....thats the cheating way do you have a copy of apstring? I dont think my school will like me taking their files
    http://www.collegeboard.org/ap/compu...l/classes.html

  15. #15
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by filburt1
    A char takes up only one byte of RAM.
    Not necessarily...although sizeof(char) is defined to be 1, it doesn't mean that a char takes up one byte on the target system.

    In fact, the target system may have no conception of bits and bytes

    I love being a techy
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  16. #16
    Thanks for thoroughly shooting down and nuking that idea.

  17. #17
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You're welcome

    Although if you stick to conventional processors (MIPS, ARM, x86) then what you say is correct
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  18. #18
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    While the others are talking, here's the code:
    Code:
    /*
    Josh
    christmas.cpp
    */
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    string day_of_christmas;
    int day;  // memory doesn't matter in such a program, and it cannot be mistaken
    for(day = 1; day <= 12; day++)
    {
    switch(day)
     {
    	case 12:
                                    day_of_christmas = "Twelth";
    		cout << ;
    	case 11:
                                    day_of_christmas = "Eleventh";
    		cout << 
    	case 10:
                                    day_of_christmas = "Tenth";
    		cout << 
    	case 9:
                                    day_of_christmas = "Ninth";
    		cout << 
    	case 8:
                                    day_of_christmas = "Eighth";
    		cout << 
    	case 7:
                                    day_of_christmas = "Seventh";
    		cout << 
    	case 6:
                                    day_of_christmas = "Sixth";
    		cout << 
    	case 5:
                                    day_of_christmas = "Fifth";
    		cout << 
    	case 4:
                                    day_of_christmas = "Fourth";
    		cout << 
    	case 3:
                                    day_of_christmas = "Third";
    		cout << 
    	case 2:
                                    day_of_christmas = "Second";
    		cout << 
    	case 1:
                                    day_of_christmas = "First";
    		cout << 
    	}
    	cout << "On the " << day_of_christmas << " day of Christmas, " << endl;
    	cout << "My true love gave to me " << endl;	switch(day)
     {
    	case 12:
    		cout << "Twelve drummers drumming";
    	case 11:
    		cout << "Eleven pipers piping";
    	case 10:
    		cout << "Ten lords a-leaping";
    	case 9:
    		cout << "Nine ladies dancing";
    	case 8:
    		cout << "Eight maids a-milking";
    	case 7:
    		cout << "Seven swans a-swimming";
    	case 6:
    		cout << "Six geese a laying";
    	case 5:
    		cout << "Five golden rings!";
    	case 4:
    		cout << "Four calling birds";
    	case 3:
    		cout << "Three french hens";
    	case 2:
    		cout << "Two turtle doves";
    	case 1:
    		cout << "A partridge in a pear tree";
    	}
             }
              return 0;
    }
    also, add a
    break;
    at the end of each case. I'm too lazy to do that. :P
    I also don't know why aou reversed the order of the cases, but that's your problem too.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  19. #19
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Don't put break, because then it falls through, so that if you have 5, it goes through five gold rings, four calling birds, etc.

    Quite nifty really
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  20. #20
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Oh, yeah, I forgot the song. Well, actually I know it only from blizzard's sc map "Twelve Days of Christmas".
    You're right parksie.
    Mysteryman: put the breaks only in the first switch block. And remove or complete the cout instructions (I got the days from there and forgot to remove the remains.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Sep 2001
    Posts
    23
    okay, thanks!

  22. #22
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    np, I like it
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width