Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Annoying break statement error

It keeps saying my break statement isn't in the loop, but it is. Someone please explain this to me, been trying to figure it out all night.

Btw don't ask why im using do-while loops lol it's just for a practice problem.
Code
#include <iostream>

using namespace std;

int main()
{
    int bottlecounter = 99;
    do
    {
        cout << "99 bottles of beer lyrics - ";
    }while (bottlecounter>0);
    {
       cout << bottlecounter << " bottles of beer on the wall, " << bottlecounter << " bottles of beer.";

        bottlecounter--;

        cout << "Take one down, pass it around, " << bottlecounter << " bottles of beer on the wall...";

        cout << "If one of those bottles should happen to fall, " << bottlecounter << " bottles of beer on the wall...";
        
        if( bottlecounter ==1)
        {
            break;
        }

    cout << " No more bottles of beer on wall, no more bottles of beer. ";
    cout << " Go to the store and buy some more, 99 bottles of beer on the wall...";
        return 0;
   }
}
(Oct 6, 2016 09:31 PM)D_MuLa Wrote:  #include <iostream>

using namespace std;

int main()
{
int bottlecounter = 99;
do
{
cout << "99 bottles of beer lyrics - ";
}while (bottlecounter>0);
{
cout << bottlecounter << " bottles of beer on the wall, " << bottlecounter << " bottles of beer.";

bottlecounter--;

cout << "Take one down, pass it around, " << bottlecounter << " bottles of beer on the wall...";

cout << "If one of those bottles should happen to fall, " << bottlecounter << " bottles of beer on the wall...";

if( bottlecounter ==1)
{
break;
}

cout << " No more bottles of beer on wall, no more bottles of beer. ";
cout << " Go to the store and buy some more, 99 bottles of beer on the wall...";
return 0;
}
}
@Abraluna, i have a semi-colon after the while condition.
Apparently I can't read lol oops

Why do you have random braces around the code under the do while loop, it's misleading


Okay upon actually reading and attempting to compile your code I see the issue - it's wrong

Here's how do while loops work:

do{
CODE
}while(COND);

And it does CODE while COND is true. All that stuff coming after is just plain code but you for some reason put braces around it, and there's a break statement where it doesn't belong. That bottom code isn't in any loop. Break is for breaking out of loops.

you have a do while loop.

I think you got confused and thought that the while loop was used for the if statement.

you need a second while loop for that.
"Design must be simple. Elegant. Implementation, less so. Sequences must change. Intent static, product fluid. Always can improve. Stressers reveal flaws. Flaws reveal potential. Always improving. Good."
(Oct 7, 2016 01:15 AM)Abathur Wrote:  you have a do while loop.

I think you got confused and thought that the while loop was used for the if statement.

you need a second while loop for that.
damn but when I try something else it turns into an infinite loop :(


okay i figured it out 😏
Code
int bottlecounter = 99;
    do
    {
        cout << "99 bottles of beer lyrics - ";
        {
            break;
        }
    }while (1);
(Oct 7, 2016 03:52 AM)D_MuLa Wrote:  damn but when I try something else it turns into an infinite loop :(


okay i figured it out 😏
Code
int bottlecounter = 99;
    do
    {
        cout << "99 bottles of beer lyrics - ";
        {
            break;
        }
    }while (1);

Why do you have break in its own scope?
Why not use a for loop?
Why are you not checking a condition before breaking? This will only print the string one time.
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
(Oct 7, 2016 11:26 AM)Goodies Wrote:  Why do you have break in its own scope?
Why not use a for loop?
Why are you not checking a condition before breaking? This will only print the string one time.
it only needs to be printed one time because it's the title of the song. Also, I used do-while because my task is to do a bunch of programs with each loop. I'm trying for-while on this next.
(Oct 7, 2016 11:46 AM)D_MuLa Wrote:  it only needs to be printed one time because it's the title of the song. Also, I used do-while because my task is to do a bunch of programs with each loop. I'm trying for-while on this next.

Why have a loop at all? Why not just print the statement?
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
(Oct 7, 2016 01:40 PM)Goodies Wrote:  Why have a loop at all? Why not just print the statement?
i'm learning about loops, these were practice problems from my book.
You should be learning about proper use of loops and how they can actually help simplify your code, not using them just because and hardly understanding how you've made things work.

Code
#include <iostream>

using namespace std;

int main()
{
    int bottlecounter = 99;
    cout << "99 bottles of beer lyrics - ";

    do
    {
        cout << bottlecounter << " bottles of beer on the wall, " << bottlecounter << " bottles of beer.";
        --bottlecounter;
        cout << "Take one down, pass it around, " << bottlecounter << " bottles of beer on the wall...";
    }
    while (bottlecounter > 0);

    cout << " No more bottles of beer on wall, no more bottles of beer. ";
    cout << " Go to the store and buy some more, 99 bottles of beer on the wall...";

    return 0;
}
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by pmsSep 28, 2019 03:55 AM 9 240 Sep 28, 2019 03:55 AM
Last Post: pms
[C#] by grweh3wrhJul 16, 2019 06:20 AM 6 220 Jul 16, 2019 06:20 AM
Last Post: grweh3wrh
[Java] by blackcoder00Mar 11, 2019 07:20 AM 5 245 Mar 11, 2019 07:20 AM
Last Post: blackcoder00