overflow question newb

Hello just a quick question. I thought when i ran this that the double would hold more tha an int on a 64 bit system but at square 30 they both overflow. Did i do something wrong or is that the point were it gets to high for both of them and i have to split the board in half?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  // Emperor Rice problem         Date 8/26/15            E.Stephan
//This program will figure out the number of rice per square a person would get my doubling it.


#include <iostream>

using namespace std;

int main()
{
    //initialize val ints then doubles
    long int oldRice = 0;
    long int rice =1;

    double oldRice2 =0.0;
    double rice2=1.0;

//set up chess square loop
    for (int i=1;i<=64;i++)
    {
        if (i == 1)         //if on square 1
            {
                oldRice=rice;
                oldRice2=rice2;
                cout << "\nYou have " << rice << " rice at square "<< i <<"\n";

            }
        else            //if square is higher than 1 till then do this to  end of the board.
            {
                rice=oldRice * 2;
                cout<<"\nYou have " << rice <<" rice at square " << i <<"with int only \n";
                oldRice=rice;

                rice2=oldRice2 *2;
                cout <<"\nYou have " << rice2 <<" rice at square " << i<< " with double int \n";
                oldRice2=rice2;
            }

    }

    return 0;
}



Thanks for you time,

E Stephan
Last edited on
You never print the rice2 variable. Instead you print rice twice.
Last edited on
lol my bad i just had chemo so i am a bit out of it atm thanks peter.
Topic archived. No new replies allowed.