My first program, problem with large numbers.

I made a program to put in a number to check if it has a whole root. Helps me with math homework. BUT, I noticed if I input a large number that I know has a root that should be found, it just gives me the answer of 8^22 regardless.

I thought using long long would have fixed it but it had no effect. Why does the computer get confused for very large numbers with long long?

It works just fine for anything up to 9 digits I've tested.

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
#include <iostream>
using namespace std;
int main()
{
	long long DURKA(0L);
	long long DuRka(1L);
	int DURR(2);
	int durka(0);
	
	cout << "Enter a whole number to see if it has a whole root value: ";
	cin >> DURKA;
	cout << endl;
	
	while(DuRka != DURKA)
	{
		if(durka == 31)
		{
			if(DURR == 100)
			{
				cout << "Error, did not find any whole roots." << endl;
				return(0);
			}
			DURR = DURR + 1;
			durka = 0;
			DuRka = 1;
		}
		DuRka = DuRka * DURR;
		durka = durka + 1;
	}
	cout << endl << DURKA << " = " << DURR << "^" << durka << endl;
	return(0);
}
It gets confused with long long for the same reason I suspect it'd get confused with a normal int. Long long does have a size limit, although it is many times larger than a normal int, and I suspect you're reaching it.

Sorry. There is a way around your problem, but it's not exactly simple.

-Albatross
Works fine for me on Code::Blocks with MinGW with numbers such as 285311670611.

What compiler and IDE are you using?
Visual Studio 2010. Also, just realized I copied the durka version I created while trolling reddit...lol

Here is a better version.

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
#include <iostream>
using namespace std;
int main()
{
	long long userNum(0L);
	long long calcNum(1L);
	int multipleNum(2);
	int countExponent(0);
	
	cout << "Enter a whole number to see if it has a whole root value: ";
	cin >> userNum;
	cout << endl;
	
	while(calcNum != userNum)
	{
		if(countExponent == 31)
		{
			if(multipleNum == 100)
			{
				cout << "Error, did not find any whole roots." << endl;
				return(0);
			}
			multipleNum = multipleNum + 1;
			countExponent = 0;
			calcNum = 1;
		}
		calcNum = calcNum * multipleNum;
		countExponent = countExponent + 1;
	}
	cout << endl << userNum << " = " << multipleNum << "^" << countExponent << endl;
	return(0);
}
Thanks, re-tested with smaller numbers and it does work. I thought long long was going to enable me to work in the quadrillions or something.

Next step is to also display the prime factorization of each number I throw at it. Need to learn how to make seperate objects and return values. I can't make a new function and figure out how to return a value from the function and store it in a variable.

Java this was easy, but I gave up on Java after realizing I couldn't make executable programs.

Anyways, thanks for the help! marking solved.
long long int on c++11 should have at least 64 bits and should be able to handle up to 9,223,372,036,854,775,807.

If you make it unsigned, you can go as far as the mythical 18,446,744,073,709,551,615.

But only if your compiler supports C++11 long long int.
long long int is also in C++03 afaik, at least it works on most C++03 compilers...
In C++03 it would be compiler specific. In C++11, it becomes standard. All compilers should support it now or very soon.
Topic archived. No new replies allowed.