-1

I have an assignment, where I have to count down in lyrics according to the number in user input (between 1 to 100). In the last set of lyrics, when the countdown gets to 1, it's supposed to change from bottles to bottle, then eventually 0.

Visual Studio is telling me I'm missing a while (last closing bracket) and a ; (open bracket after do (beerAmount - 1) ), but no matter where I put the ; where I think it makes sense, it wont run. I don't understand why it's telling me I'm missing a while statement. My professor strongly recommended I use a do-while loop.

An example of it sucessfully running would result with an output like this:

imagine the user imputed 2 when prompted for how many bottles of beer

2 bottles of beer on the wall. 2 bottles of beer. Take 1 down, pass it around.  1 bottle of beer on the wall.
1 bottle of beer on the wall. 1 bottle of beer. Take 1 down, pass it around.  0 bottles of beer on the wall.
#include <iostream>
using namespace std;

int main()
{
    int beerAmount;
    int lessAmount = (beerAmount - 1);  
    do {
        cout << "Enter an amount of beers between 0 and 101: " << endl;
        cin >> beerAmount;
        if ((beerAmount > 0 && beerAmount < 101)) {
            break;
        }
        else {
            cout << "Invalid amount! Please try again" << endl;
        }
    } 
    while (true); {
        do (beerAmount - 1) { //this bracket
            cout << beerAmount << " bottles of beer on the wall. " << beerAmount << " bottles of beer. Take " << beerAmount << " down, pass it around. " << lessAmount << " bottles of beer on the wall." << endl;
        }
        if ((beerAmount == 1)) {
            cout << beerAmount << " bottle of beer on the wall. " << beerAmount << " bottle of beer. Take " << beerAmount << " down, pass it around. 0 bottles of beer on the wall." << endl;
        }
        if (beerAmount == 0) {
            break;
        }
    } //this bracket
    
    return 0;
}        
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Orbit
  • 1
  • `do (beerAmount - 1)` `do` doesn't have an argument. – Retired Ninja Aug 24 '22 at 03:27
  • I would advise you to really learn the syntax of statements before doing the assignments. Also your code indentation looks really messy and that makes it easy to miss mismatched braces. – LostMikely Aug 24 '22 at 03:39
  • This is explained in any beginner level [c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jason Aug 24 '22 at 03:39
  • Perhaps you meant something like this: https://godbolt.org/z/3v9rq8qjE – Retired Ninja Aug 24 '22 at 03:42
  • Consider using a function to avoid code duplication (and always get the pluralization correct): https://godbolt.org/z/YKjd4xeMd – paddy Aug 24 '22 at 03:50

1 Answers1

0

The syntax of the do-while-loop is incorrect.

It should be:

do {
    //code to repeat...
}
while(condition);

When the loop is fixed, you also need to remove the closing curly brace just before the return-statement since it prematurely ends the function block.

LostMikely
  • 130
  • 10