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;
}
}
DO NOT DEAL WITH THIS MEMBER
Oct 6, 2016 09:39 PM
(This post was last modified: Oct 6, 2016 09:40 PM by 6577657534735.)
(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.
DO NOT DEAL WITH THIS MEMBER
Oct 6, 2016 10:09 PM
(This post was last modified: Oct 6, 2016 10:11 PM by 6577657534735.)
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.
Oct 7, 2016 01:15 AM
(This post was last modified: Oct 7, 2016 01:16 AM by Abathur.)
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 03:52 AM
(This post was last modified: Oct 7, 2016 10:04 AM by D_MuLa.)
(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 11:26 AM
(This post was last modified: Oct 7, 2016 11:27 AM by Goodies.)
(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.
(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?
(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;
}
|