This is what i try to do, but i dont know why it gives me the message:"break statement not within loop or switch" and the program doesnt compile.
P.S.: IM NOT VERY GOOD IN C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<iostream>
usingnamespace std;
int main(void)
{
int a,b,i,c;
cout<<"Give a=";
cin>>a;
cout<<"Give b=";
cin>>b;
for (i=1,500; i<=500 && c<250; i++)
{
c=(a*a+b*b)*i;
if (c>250) break;
else i+=i;
}
cout<<"c=(a^2+b^2)*i"<<c<<" and i="<<i<<endl;
system("pause");
return 0;
}
#include<iostream>
usingnamespace std;
int main(void)
{
int a,b,i,c; /*<<----Uninitialized variables. This isn't a problem yet because you set a+b in
the next 4 lines of code.*/
cout<<"Give a=";
cin>>a;
cout<<"Give b=";
cin>>b;
/*<----- First problem Variable c is being used without being initialized so c could be any
value.*/
for (i=1,500; i<=500 && c<250; i++) /*<---- so if C is randomly given a value above 250.
This will never happen. It will end the program. / Curious about the random number 500 */
{
c=(a*a+b*b)*i;
if (c>250) break;
else i+=i; /*<<--this line looks like it would cause the loop to change strangly if you
don't meet the if statement. ( i++ and else i+=i). first loop(1++, 1+=1, i = 3 on the next loop)(3++, 3+=3, i = 7 on the 3rd loop), so your loop is
skipping a lot of it's "loops" everytime it doesn't meet the if statement. unless this is what
you're intending to do*/
}
cout<<"c=(a^2+b^2)*i"<<c<<" si i="<<i<<endl; /*<<-- the i value here will change
depending on how far you got through the loop before it gets to break;*/
system("pause");
return 0;
}
I expect those are the reasons your code isn't working as it should be. What is it you're trying to do?
nah, i made it work, it was just a error at for (i=1,500; which should be for (i=0,500;
now it works. thanks anyway :D
P.S.: it didnt work 1st time but after i posted i compiled it again and it worked :D