The old story of the emperor and chess..

int i=1;
    int grains=0;

    cout<<"enter the grains: ";

    while (cin>>grains)
       {for (double a=1;a<=grains;a=a*2) i++;
        cout<<"\nsquares needed: "<<i<<"\n\nenter the grains: ";
        a=1;i=1}


Why does it give me error in line
a=1;i=1

it should work just fine..
Change that line of code to this. a = 1, i = 1;
You've missed out a semicolon after i=1
@ xhtmlx: You should probably prefer semicolons over the comma operator.
Why is that?

a = 1; i = 1;

a = 1, i = 1;

These both do the same thing; assign one to both of the variables. I try to keep my code as compact as possible. That's probably not the best idea, but that's just how I do it.
Last edited on
Unless your compiler is stupid, a should not exist at that point in the program. It goes out of scope at the end of the for loop.
That's true, I just realized that. It's hard to tell because he threw i++ to the side of the for loop, which I hate.

he threw i++ to the side of the for loop, which I hate. 


and

It goes out of scope at the end of the for loop.


and

I try to keep my code as compact as possible.


GUYS!!!!
thank for the help but could you please also give some reasoning after each statement??
I am new here, new to C++ too, if you don't explain to me what you mean I am right back from the start!!

Keeping the code as compact as possible. right. so, how would
a = 1; i = 1;
be more/less compact than
a = 1, i = 1;


What does "out of scope" mean?
After each loop (after it has just been executed) I want to be able to use the program again!!! (that's why the loop) so what's wrog with setting I and A back to one?

Why would you "hate"
i++
on the side of the "for loop"??
Bjarne Stroustrup suggests that.
Would you please explain to me why/if it's "bad practise" or dangerous at tmes?

Thanks



Last edited on
What does "out of scope" mean?
It means just what I said. The variable ceases to exist when the for loop ends.

By the way, there's this cool little program. You may have heard of it:
http://www.google.com/search?q="going+out+of+scope"

what's wrog with setting I and A back to one?
As I said, a doesn't exist anymore at that point in the program.

Why would you "hate" i++ on the side of the "for loop"??
What he means is that
for (/*...*/) foo;
is in very poor style.
This is preferable:
1
2
for (/*...*/)
    foo;
Your whole program is fairly bad on the eyes.

http://en.wikipedia.org/wiki/Indent_style
oook.
Now I get it.
I still need to find anoher solution to make this work, but now I understand everything I have been commented about
thanks
Topic archived. No new replies allowed.