Thread: cout question

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    16

    cout question

    On chapter 5 problem 1 in Jumping into C++ I need to write a program that prints out all the lyrics to 99 Bottles of Beer on the Wall.

    I did it two ways. One with several cout statements and one condensed. Here is the second way:

    Code:
    #include <iostream>
    
    using namespace std;
    
    // Write a program that prints out the entire lyrics
    // to a full rendition of "99 bottles of bear".
    
    int main()
    {
        for ( int i = 99; i > 0; i-- )
        {
            cout << i << " bottles of beer on the wall, " << i << " bottles of beer.\n";
            cout << "Take one down, pass it around, " << i - 1 << " bottle of beer on the wall.\n";
            cout << "\n";  // for better format on the output
        }
    }
    Is this way of doing it OK formatting? Also, is there a way to convert numbers to text? Example: 99 = Ninety-Nine

    Thanks,

    Kranky

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Also, is there a way to convert numbers to text?
    Nope, but that sounds like an awesome project for a beginner.

    Soma

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    Ok Soma,

    I have been thinking about this little project at work and on the ride home. I have some experience programming in BASIC back in the early 80's. From what I can see if I define the numbers 1 through 19, 20, 30, 40, 50, 60, 70, 80, and 90 I can spell out any number. I would think I could make a 2 dimensional array and put those in it with column 1 being the number and column 2 being the words. I would have to split up the number into digits and pull each digit from the array spelling out the word. All I would need to do is insert the appropriate thousand or hundred here and there and it should work.

    The only thing is I have a few days worth of C++ programming under my belt and have no idea how to implement any of that. I will have to do some research unless you can give me some hints on where to start.

    Thanks,

    Kranky

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    15
    Quote Originally Posted by Kranky View Post
    I would think I could make a 2 dimensional array and put those in it with column 1 being the number and column 2 being the words.
    I suggest you use a normal array of strings and define them like this :

    num_string[1]="one"
    num_string[2]="two"
    ..........
    num_string[98]="ninety eight"
    num_string[99]="ninety nine"

    If want the conversion of a number i, just look up num_string[i].
    This would make things a whole lot easier for you. Plus you'd be saving stack space.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    IfI understand it correctly, it will be some waste of space (not very significant though) to use an array, as you're not going to store all of the numbers.
    After you get it working with a normal array, try out an associative array.
    Code:
    map<int, string> number_map=
    {
        {1,"one"},
        {2,"two"},
        //{..rest...},
     
        {90,"ninety"}  
    };
    You can access the strings by using the same notation, number_map[number] .
    Also, you'll need to #include <map> and <string>

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    That program's output would contain:
    "98 bottle"
    "1 bottles"
    Perhaps you should work out how to so a special case for i==1
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    Thank you for pointing that out iMalc. I will fix that at lunch time.

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    OK. I couldn't wait for lunch. I actually had 2 issues when beer was at 2 and at 1. Is this coded correctly or should I have done it differently?

    Thanks,

    Kranky

    Code:
    #include <iostream>
    
    using namespace std;
    
    // Write a program that prints out the entire lyrics
    // to a full rendition of "99 bottles of bear".
    
    int main()
    {
        for ( int i = 99; i > 0; i-- )
        {
            if ( i == 2)
            {
                cout << i << " bottles of beer on the wall, " << i << " bottles of beer.\n";
                cout << "Take one down, pass it around, " << i - 1 << " bottle of beer on the wall.\n";
                cout << "\n";  // for better format on the output
            }
            else if ( i == 1 )
            {
                cout << i << " bottle of beer on the wall, " << i << " bottle of beer.\n";
                cout << "Take it down, pass it around, no more bottles of beer on the wall.\n";
                cout << "\n";  // for better format on the output
            }
            else
            {
                cout << i << " bottles of beer on the wall, " << i << " bottles of beer.\n";
                cout << "Take one down, pass it around, " << i - 1 << " bottles of beer on the wall.\n";
                cout << "\n";  // for better format on the output
            }
        }
    }
    Edit: I realize this stuff is so basic for you guys. Thanks for taking the time and helping me. I look forward to passing it on when I know more.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Whatever works for you. You could use the ternary (tertiary?) operator ?: to handle plural/singular cases and reduce some of that code. Maybe...
    Code:
    for( int i = 99; i > 0; --i )
    {
        cout << i << " bottle" << ((i != 1) ? "s" : "") << " of beer on the wall, ";
        cout << i << " bottle" << ((i != 1) ? "s" : "") << " of beer.\n";
        cout << "Take it down, pass it around, ";
        if( i == 1 )
            cout << "no more bottles";
        else
            cout << i - 1 << " bottle" << ((i > 2) ? "s" : "");
        cout << " of beer on the wall.\n\n";
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    Hk_mp5kpdw, I can follow the code and I understand what you did. I didn't realize you could put a conditional if-else statement (not sure I am using the right terminology) in the middle of the cout. It seems to me if i != 1 the ? puts the "s" and the : is an else statement that happens when i == 1. This blows my mind on how much of a code saver this is.

    I also didn't think that you (stupid of me for not trying it) that you could use \n\n to add the blank line at the end of the loop.

    Thank you,

    Kranky

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Kranky View Post
    ...I didn't realize you could put a conditional if-else statement (not sure I am using the right terminology) in the middle of the cout. It seems to me if i != 1 the ? puts the "s" and the : is an else statement that happens when i == 1...
    Yup, it's called the ternary operator.
    Basically it evaluates to some expression depending on the condition:

    cond ? a : b

    will evaluate to either "a" (if cond is true) or "b".
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cout Question
    By macman in forum C++ Programming
    Replies: 14
    Last Post: 04-08-2005, 04:00 AM
  2. cout question?
    By correlcj in forum C++ Programming
    Replies: 6
    Last Post: 11-03-2002, 01:05 PM
  3. cout question
    By The Gweech in forum C++ Programming
    Replies: 4
    Last Post: 07-09-2002, 04:11 PM
  4. Question using cout
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-18-2001, 12:44 AM
  5. cout question...
    By bishop_74 in forum C++ Programming
    Replies: 1
    Last Post: 12-06-2001, 11:22 AM