99 bottles of beer program question?

This is a discussion on 99 bottles of beer program question? within the C++ Programming forums, part of the General Programming Boards category; Hi All, I am new to the c++ language and I am reading the book Jumping_into c++. In the Practice ...

  1. #1
    Registered User jvac's Avatar
    Join Date
    Sep 2013
    Posts
    7

    99 bottles of beer program question?

    Hi All,

    I am new to the c++ language and I am reading the book Jumping_into c++. In the Practice problems for chapter 5, Loops the first assignment states to write a program that prints out the entire lyrics to a full rendition of "99 Bottles of Beer". Which I did and it executes perfectly when I build and run the this code:

    Code:
    //Write a program that prints out the entire lyrics
    //to a full rendition of "99 Bottles of Beer"
    
    
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    
    
    {
        int x;
        for (x = 99; x > 0; x --)
    
    
        {
        cout << x << " " << "bottles of beer on the wall, ";
        cout << x << " " << "bottles of beer.\n";
        cout << "" << "Take one down, pass it around, ";
        cout << --X << " bottles of beer on the wall...," << endl;
        cout << "" << "If one of those bottles should happen to fall,\n" << endl;
        }
    
    }
    But when I tweak the code and change this line to:

    Code:
    cout << X-- << " bottles of beer on the wall...," << endl;
    The program does not not execute correctly.

    Why is that?

    Thanks in advance.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    How many times are you doing x-- (and --x) in that loop?
    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.

  3. #3
    Registered User jvac's Avatar
    Join Date
    Sep 2013
    Posts
    7
    Hi Salem,

    I am doing x-- x(2)it does not work, and --x x(1) it works fine.

    Why you ask?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Well you stated "it doesn't work" without actually telling us how you considered it to be incorrect.

    From my point of view, both versions of the code are wrong.

    Reading the wikipedia page, the lyrics should be
    99 bottles of beer on the wall, 99 bottles of beer.
    Take one down, pass it around, 98 bottles of beer on the wall...
    Or
    99 bottles of beer on the wall, 99 bottles of beer.
    If one of those bottles should happen to fall, 98 bottles of beer on the wall...

    But you have both lines, which means you count down two bottles at a time.

    I suppose your question boils down to the difference between --x and x--, and that was discussed at length in another thread just this week.
    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.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Quote Originally Posted by Salem View Post
    I suppose your question boils down to the difference between --x and x--, and that was discussed at length in another thread just this week.
    The thread: Increment Operator Results
    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.

  6. #6
    Registered User jvac's Avatar
    Join Date
    Sep 2013
    Posts
    7
    Thanks Salem, and Elysia.
    Understood thanks I will look into thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check to see whether all bottles can be closed
    By iamnew in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2010, 01:05 AM
  2. Help!For "Ninety-nine Bottles of Beer on the wall"
    By tx1988 in forum C++ Programming
    Replies: 12
    Last Post: 09-18-2007, 05:08 AM
  3. 99 Bottles of Beer...
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-19-2007, 04:43 PM
  4. Mmmm beer
    By -JM in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-05-2005, 05:22 PM

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