FAQ: Loops - 99 Bottles

This community-built FAQ covers the “99 Bottles” exercise from the lesson “Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise 99 Bottles

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Another answer for this exercise is the next:

#include <iostream>

int main() {

  // Write a for loop here:
  int n = 99;
  for(;n > 0;){
    std::cout << n;
    std::cout << " bottles of pop on the wall.\nTake one down and pass it around.\n";
      std::cout << --n << " bottles of pop on the wall.\n\n";  
  }
 return 0; 
}

We can use -for- like -while-, while(n > 0) can write like for(;n > 0;) and if we use pre-decrement --n, we can do a decrement of n.

I love for loop, oh! by the way we can do an infinite loop with for(;;){}

Hi josen0,

I’m assuming such is the case, but you’ve tried this code and it lets you pass the exercise, correct? I only ask because I have a heck of a time trying to move forward and finish this lesson, but no matter what I do, CA’s “terminal” thingy won’t accept it. Before, I had it reading out the same message, but without the space between the verses, so I tested out your code in my computer’s real terminal, and thought “Aha! that’s why it doesn’t pass.” so I added the extra newline code and its output is identical now, but…still no. The only difference with my code is that I use the endl methods for the 1st “bottles on wall” line and the “take one down” line. I’m pretty exasperated , and I’ve got a mind to report a bug to Codecademy. Do you have any thoughts on that? Other readers, have any of y’all had similar problems with this exercise?

2 Likes

Hello rubycoder77505,

CA doesn’t recognize this code as OK, in fact, the official solution is the next:

#include <iostream>

int main() {

  // Write a for loop here:

  for (int i = 99; i > 0; i--) {

    std::cout << i << " bottles of pop on the wall.\n";
    std::cout << "Take one down and pass it around.\n";
    std::cout << i - 1 << " bottles of pop on the wall.\n\n";

  }

}

I put an alternative solution for this exercise in order to show an alternative use of for loop, as you can see my code works, maybe you can take some ideas for that code and use it for you.

Sorry to not specify that CA doesn’t recognize that code as good answer, please take a breath and try the official solution to pass step.

Thank you for comment, if you have some question, please feel free to comment here.

Happy Holidays.

Regards,

Jose.

1 Like

Thanks for your reply Jose, I’m convinced at this point then, sadly, that this is indeed a bug. Perhaps it has something to do with my browser, I usually use Opera, but I think, why now, when all the other code I’ve used before is fine?

Either way, I figure my code is perfectly valid and it works in a “real-life” (i.e., my computer’s terminal) situation, and there’s only so much time of my life I’m gonna spend editing code for a 99 bottles of beer/pop/whatever program, so I’ll report the bug and move forward with my life. BTW, I tried the official solution you’ve posted here, that apparently doesn’t pass for what I get with CA either! (Not blaming you, of course. Like I said, I really think it’s a bug at this point). I wanna get past this before I start having psycho nightmares where “99 bottles of beer on the wall” is being sung slowly by children playing jumprope LOL.

For any who are interested in this topic, here is my code. Happy Holidays to you too, Jose!

#include

int main() {

// Write a for loop here:
for (int bottle = 99; bottle > 0; --bottle) {
std::cout << bottle << " bottles of pop on the wall." << std::endl;
std::cout << “Take one down and pass it around.” << std::endl;
std::cout << (bottle - 1) << " bottles of pop on the wall.\n\n";

}

}

(Later edit, the #include line has iostream in the more than/less than symbols, but Codecademy’s forum seems not to recognize that, for HTML reasons i guess)

@rubycoder77505, has this run for you yet? I’ve tried different browsers and even went so far as to testing it on three computers. I reported a bug as well and it’s still not going. Even cheating and using the hint’s code does nothing for me. I, too, am having nightmares of 99 bottles.

The exercise wants one of the following patterns to match in the code:

for\s*\(int\s*i\s*=\s*99\s*;\s*i\s*>\s*0\s*;\s*i--\s*\)
for\s*\(\s*i\s*=\s*99\s*;\s*i\s*>\s*0\s*;\s*i--\s*\)

\s means whitespace and * means zero or more, \) and \( are just )(, the rest is the same as it says in the pattern.

@sonnynomnom exercises that test behaviour are much more pleasant, and I suspect a big reason for the existence of a solution button in the learning environment is the widespread use of regexes which just can’t be reliably passed by following instructions. For example, renaming i, or doing --i or putting a space between i and --. Maybe someone doesn’t want to use int as the type. Maybe they use >= instead of >, perfectly valid, doesn’t conflict with instructions. Results in an error message asking whether a for loop was added. Yeah. It was. The wrong thing is tested.
(Also, I can’t pass it at all, get no error message or anything, but that might be my unusual access pattern, maybe I do something very unintended, so I won’t claim that it’s a bug, maybe though)

@ionatan totally agree. i’m also a huge believer of problem solving in different ways. right now we have to rely on regex for a lot of the tests in learn c++ simply because we just added the language. things will change very soon tho!

om_niscient, I did manage to finally get the exercise to pass, but not for any good reason. I ran josen0’s official solution code, and received the following error message:

99bottles.cpp:3:1: error: expected unqualified-id before ‘for’
for (int i = 99; i > 0; i–) {
^
99bottles.cpp:3:18: error: ‘i’ does not name a type
for (int i = 99; i > 0; i–) {
^
99bottles.cpp:3:25: error: ‘i’ does not name a type
for (int i = 99; i > 0; i–) {
^
99bottles.cpp:19:1: error: expected declaration before ‘}’ token
}
^

But, it did light up the ‘Next’ button and allow me to pass. Now I’m a bit disappointed to see that that’s basically all for C++ lessons here for now, at least without upgrading to Pro, which i’m unfortunately not in a position to do right now. It was good learning experience at least, except for that darn 99 bottles exercise. I don’t know what glitch caused that, I hope that’ll work out for you as well. I’m not sure why it wasn’t working before, and then all of a sudden it did that one time.

It seems like the official solution suggests that you should add the final verse at the end as well to pass.

std::cout << "No more bottles of pop on the wall.\n";
std::cout << "No more bottles of pop.\n";
std::cout << "Go to the store and buy some more,\n";
std::cout << "99 bottles of pop on the wall.\n";
  

This is really silly and CA should recognise this.

I remember this song going a little differently, so I programmed it like this:
(NB, this solution won’t pass the exercise, you have to replicate the sample verse exactly to the letter)

#include <iostream>

int main() {

  int cont = true;
  char yesno;
  while (cont){
    
  	for (int i = 99; i > 0; i--){
    	std::cout << i << " bottles of beer on the wall,\n" << i << " bottles of beer.\nTake one down, pass it around,\n" << i-1 << " bottles of beer on the wall.\n\n";
  	}
	std::cout << 0 << " bottles of beer on the wall,\n" << 0 << " bottles of beer.\nGo to the shops, buy a new box\n" << 99 << " bottles of beer on the wall...\n\n";  
	
  std::cout << "Continue? (y/n): ";
  std::cin >> yesno;
  std::cout << "\n";
    
    if(toupper(yesno)=='Y'){
      cont = true;
    } else {
      cont = false;
    }
    
    
  }
 
}
  

There is an error with this exercise’s instructions, it says “If you want to add the final verse at the end:”.
It should not say this like it is optional. It should say to also add it period. I spent time trying to figure out what was wrong with my code only to find out that that part was not optional and I couldn’t proceed because of that.

Wasn’t able to complete the code unless I added the final verse exactly as it shows in the hint for the assignment. There are various ways to write the code, I felt like I had it right the first go but there aren’t instructions to complete the verse in the assignment unless you go to the Hint. Eventually I was forced to quit the assignment and ask for the correct code thinking I was missing something else only to find it was indeed just a copy of the solution in the hint menu. Not huge but it does hinder the flow to not have those instructions included in the practice.

What if I put this :

for (int i = 99; i > 0; i++) {

std::cout << i << " bottles of pop on the wall.\n";
std::cout << "Take one down and pass it around.\n";
std::cout << i - 1 << " bottles of pop on the wall.\n\n";

}

and not this:

for (int i = 99; i > 0; i–) {

std::cout << i << " bottles of pop on the wall.\n";
std::cout << "Take one down and pass it around.\n";
std::cout << i - 1 << " bottles of pop on the wall.\n\n";

}

What will happen because it won’t run it

If it’s not running is anything happening?
I’d argue it would run.
And, I’d argue you could step through a few iterations manually.
And, the code is the source of truth for what would happen, it says what would happen. What does it say?
You could also run it locally if codecademy doesn’t deal with it.

Can anyone explain why my code is not accepted??

#include

int main() {

// Write a for loop here:
for (int i = 99; i > 0; i–)
{
std::cout << i << " bottles of pop on the wall.\n";
std::cout << “Take one down and pass it around.\n” << i-1 <<" bottles of pop on the wall\n\n";
}
}

just delete the last std::cout line

delete last line
for (int i = 99; i > 0; i–) {
std::cout << i << " bottles of pop on the wall.\n";
std::cout << " Take one down and pass it around.\n";
}

1 Like

I think that CA should mark this as a correct answer.

#include <iostream>

int main() {

  // Write a for loop here:
  for (int i = 99; i > 0; i--) {
    std::cout << i << " bottles of beer on the wall, " << i << " bottles of beer. Take one down and pass it around, " << i - 1 << " bottles of beer on the wall.\n\n";
  }
}