Old Chinese rice problem

Hello everyone.
I wrote this going through the c++ guys book.
It works fine but when I try to plug in anything above 2 billion
the program goes into some kind of endless loop.

The highest value of "i" I get to is 31 before it does this behavior.

Is there some kind of limit to how many numbers visual studio can handle?
Is it something wrong with my code?

Thanks for the help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "std_lib_facilities.h"
int square(int a);
double simpleCalculator();
double distances();

int main(){
	while (1 == 1) {
		int rice = 0;//for input
		cout << "Input the number of rice you want\nthis calculator will tell you how many days it will take to get there following the peasent's method\n";
		cin >> rice;//input amount of rice wanted
		int totalRice = 0;//our counter
		for (int i = 1; i > 0; i++) {
			totalRice = pow(2, i - 1) + totalRice;//every day doubled 1..2..4..., old chinese tale
			cout << i << " current total rice is " << totalRice << "\n";
			if (totalRice >= rice) { cout << "it took " << i << " days to reach " << rice << "\n\n"; break; }//end if
			if (i > 64) { cout << "64 is our limit\n\n"; break; }
		}//end for	
	}//end while
	return 0;
}
  
closed account (48T7M4Gy)
There is a limit on the size of int's (and all the others)

http://www.cplusplus.com/doc/tutorial/variables/
Thanks man, that int limit is surprisingly low, I thought computers were smart.
closed account (48T7M4Gy)
Maybe try long, or long long instead of int. Unfortunately there is always a limit even if you surf around the web and find the large number libraries.
Topic archived. No new replies allowed.