14 Replies - 325 Views - Last Post: 02 February 2012 - 01:08 AM Rate Topic: -----

Topic Sponsor:

#1 chapu08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 30-January 12

99 bottles program

Posted 01 February 2012 - 11:28 PM

I have two issues. First, when it reaches to 1 bottle, the program should read bottle not bottles. Secondly, I can't get like a paragraph break in between each new block of "bottle". Can someone please help me?? I would really appreciate it.


#include <iostream>
using namespace std;

int main() {
#include <iostream>
using namespace std;

	// variable declarations
	int number;
	
	//start program

	cout << "Hell I'd like to sing you a song" << endl;
	cout << "Enter a number: ";
		cin >> number;

	cout << number << " bottles of Gatorade on the wall," << endl;
	cout << number << " bottles of Gatorade!" << endl;
	cout << "Take 1 down, pass it around,"<< endl;
	cout << number - 1 << " bottles of Gatorade on the wall!" << endl;

	while ( number > 0 ){
        cout << number -1 << " bottles of Gatorade on the wall," << endl;
        cout << number -1 << " bottles of Gatorade!" << endl;
        cout << "Take 1 down, pass it around," << endl;
        cout << --number -1 << " bottles of Gatorade on the wall." << endl;
        }
	if ( number = 1){
		cout << "1 bottle of Gatorade on the wall" << endl;
		cout << "1 bottle of Gatorade on the wall" << endl;
		cout << "Take 1 down, pass it around," << endl;
		cout << "0 bottles of Gatorade on the wall." << endl;
	}


    // Exit program.
    return 0;

}


:code:

This post has been edited by jimblumberg: 02 February 2012 - 06:50 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: 99 bottles program

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1169
  • View blog
  • Posts: 2,226
  • Joined: 30-May 10

Re: 99 bottles program

Posted 01 February 2012 - 11:31 PM

First, use [code][/code] tags when posting code.

Second, use == (not =) when comparing (not assigning).

Third, see http://99-bottles-of-beer.net/ ;)
Was This Post Helpful? 0
  • +
  • -

#3 chapu08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 30-January 12

Re: 99 bottles program

Posted 01 February 2012 - 11:36 PM

I'm fairly new so I don't really understand the plural function... can you guide me or give me some tips??? Thanks a lot
Was This Post Helpful? 0
  • +
  • -

#4 chapu08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 30-January 12

Re: 99 bottles program

Posted 02 February 2012 - 12:10 AM

I need help printing 1 bottle instead of bottles but it's in the loop. Can someone help me out with this?
#include <cstdlib>
#include <iostream>
using namespace std;

int main() {
#include <iostream>
using namespace std;

	// variable declarations
	int number;
	
	//start program

	cout << "Hell I'd like to sing you a song" << endl;
	cout << "Enter a number: ";
		cin >> number;

	cout << number << " bottles of Gatorade on the wall," << endl;
	cout << number << " bottles of Gatorade!" << endl;
	cout << "Take 1 down, pass it around,"<< endl;
	cout << number - 1 << " bottles of Gatorade on the wall!" << endl;

	while ( number -1 > 0 ){
        cout << number -1 << " bottles of Gatorade on the wall," << endl;
        cout << number -1 << " bottles of Gatorade!" << endl;
        cout << "Take 1 down, pass it around," << endl;
        cout << --number -1 << " bottles of Gatorade on the wall." << endl;
    }

	if (number -1 == 1){
		cout << number -1 << "bottle of Gatorade on the wall" << endl;
		cout << number -1 << " bottle of Gatorade!" << endl;
        cout << "Take 1 down, pass it around," << endl;
        cout << --number -1 << " bottle of Gatorade on the wall." << endl;
	}
    // Exit program.
    return 0;

}

This post has been edited by r.stiltskin: 02 February 2012 - 12:29 AM
Reason for edit:: Added code tags. (Code tags go around your code, not in the title.)

Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1812
  • View blog
  • Posts: 4,891
  • Joined: 27-December 05

Re: 99 bottles program

Posted 02 February 2012 - 12:24 AM

Take that one out of the loop.
Was This Post Helpful? 0
  • +
  • -

#6 chapu08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 30-January 12

Re: 99 bottles program

Posted 02 February 2012 - 12:26 AM

how do I do that?
Was This Post Helpful? 0
  • +
  • -

#7 Toadill  Icon User is online

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 274
  • Joined: 08-January 12

Re: 99 bottles program

Posted 02 February 2012 - 12:27 AM

Okay my first question is why are you #including something inside of your main function that should be above main? It also looks like you have included some things twice.
Also as a suggestion use a do while loop so you don't have to repeat code.

Another thing you are not deincrementing the base value for the loop being number. This is a infinite loop.
try this inside your loop
number--; //post
or
--number; //pre



Also get rid of the - 1 that is not decrementing
Example

#include <iostream>
using namespace std;

int main() {

// variable declarations
int number;

//start program

cout << "Hell I'd like to sing you a song" << endl;
cout << "Enter a number: ";
cin >> number;

do {
cout << number  << " bottles of Gatorade on the wall," << endl;
cout << number  << " bottles of Gatorade!" << endl;
cout << "Take 1 down, pass it around," << endl;
cout << number  << " bottles of Gatorade on the wall." << endl;
--number;
}
while( number  > 0 ) // If number is great then 0 keep looping 
//else
// Exit program.
cin.get(); //let you see the output
cin.get();
return 0;

} 


This post has been edited by Toadill: 02 February 2012 - 12:41 AM

Was This Post Helpful? 0
  • +
  • -

#8 chapu08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 30-January 12

Re: 99 bottles program

Posted 02 February 2012 - 12:39 AM

I apologize I am fairly new. the code that I posted is the ideal code that I would like to use since it's my original one. Can you tell me though, is there any way to exclude the 1 to say bottle instead of bottles or do I have to start over again with a do while?
Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1812
  • View blog
  • Posts: 4,891
  • Joined: 27-December 05

Re: 99 bottles program

Posted 02 February 2012 - 12:39 AM

I don't understand why you want to print any verses before the while test. Why don't you begin (after inputting the number) with

while( number > 1 ) {
  // all of the "bottles" verses
}


then do the "1 bottle" verse after the loop.
Was This Post Helpful? 0
  • +
  • -

#10 chapu08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 30-January 12

Re: 99 bottles program

Posted 02 February 2012 - 12:43 AM

It's a requirement for my homework to have the verse for the inputted number. After I do the 1 bottle verse I also have to create the 0 verse as well right?
Was This Post Helpful? 0
  • +
  • -

#11 Toadill  Icon User is online

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 274
  • Joined: 08-January 12

Re: 99 bottles program

Posted 02 February 2012 - 12:47 AM

Your home work? :nottalkingtoyou:
You are not allowed to ask for help with your homework on this forum.

I will not give you the answer but a conditional statement may clear that problem up if you put it inside the loop.

This post has been edited by Toadill: 02 February 2012 - 12:50 AM

Was This Post Helpful? 0
  • +
  • -

#12 chapu08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 30-January 12

Re: 99 bottles program

Posted 02 February 2012 - 12:49 AM

I apologize I figured that I've done almost all of it except I'm stumped. I can't figure out how to exclude the 1 at all and I've been at it for hours.
Was This Post Helpful? 0
  • +
  • -

#13 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1812
  • View blog
  • Posts: 4,891
  • Joined: 27-December 05

Re: 99 bottles program

Posted 02 February 2012 - 12:51 AM

But why don't you want to put the verse for the inputted number inside the loop?

And yes, you would then have to create the 0 verse too.

Another option is to do everything inside the loop. Instead of printing the entire "number bottles of beer ... " in a single line of code, you can split it up: print the number, then have an if statement that decides whether to print "bottles" or "bottle" based on the value of number, and then print the rest of the line.
Was This Post Helpful? 1
  • +
  • -

#14 Toadill  Icon User is online

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 274
  • Joined: 08-January 12

Re: 99 bottles program

Posted 02 February 2012 - 01:04 AM

This is exactly what I was getting at, thank you
Also another thing to consider is the postfix and prefix operators
--number //means decrement then pass the number

number-- //means pass the number then decrement

The reason I am pointing this out is because when the user enters a number you start with that number you don't start with one number less therefore you should be using the postfix operator

Example



do {
cout << number  << " bottles of Gatorade on the wall," << endl;
cout << number  << " bottles of Gatorade!" << endl;
cout << "Take 1 down, pass it around," << endl;
cout << number--  << " bottles of Gatorade on the wall." << endl;
}
while( number  > 0 ) // If number is greater then 0 keep looping 
//else
// Exit program.


This reads the number given by the user then decrements the value, therefore the next time through the loop it will be one less like it should be.

This also makes for easy error handling as you will learn later in C++ You can simple use another conditional statement before the loop and after the input to check if the value entered is a character or a number less then zero.

This post has been edited by Toadill: 02 February 2012 - 01:21 AM

Was This Post Helpful? 0
  • +
  • -

#15 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1812
  • View blog
  • Posts: 4,891
  • Joined: 27-December 05

Re: 99 bottles program

Posted 02 February 2012 - 01:08 AM

View PostToadill, on 02 February 2012 - 03:04 AM, said:

This is exactly what I was getting at, thank you

You're welcome. I couldn't predict that you were going to post that while I was typing, could I? :/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1