int squaring error

I've just finished writing my first program. It asks for integers and then returns a value of that integer squared. In messing around with different values for this I found that when I input a value of 180043 the program returns a value of -1944256519. This is obviously wrong, both in sign and in magnitude. The same thing happens for a few other similar values I have tried (anything from 170000 to 180099). Here is the code for the program. I'm using microsoft visual C++ express 2010 on windows 7.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "StdAfx.h"

#include <iostream>

#include <string>

using namespace std;


int main()
{

int n;

int a;

char x [10];

first:

cout << "\n\nPlease insert an integer\n\n";

cin >> n;

cout << "\n\nThe integer you entered is " << n;

cout << "\n\n";

cout << "The square of your integer is ";
	
	a = n*n;

cout << a;

cout << "\n\n";

if (a<500)
	cout << "Come on, that's only a little number :) Try something bigger to really test me!\n\n";

if (a<500) goto second;


if (a> 500) 
	cout << "That's a nice big number! Bet you didn't know the square of that ;)\n\n";

if (a> 500) goto finish;

second:

cout << "Please insert an integer\n\n";

cin >> n;

cout << "\n\nThe integer you entered is " << n;

cout << "\n\n";

cout << "The square of your integer is ";
	
	a = n*n;

cout << a;

cout << "\n\n";

if (a<500)
	cout << "Come on, that's only a little number :) Try something bigger to really test me!\n\n";

if (a<500) goto second;

if (a>500) 
	cout << "That's a better number; nice and big!\n\n";

finish:

cout << "Would you like to enter another number? (y/n) \n\n";

cin >> x;

if (stricmp ("y",x ) ==0 )
	goto first;

if (stricmp ("n",x) ==0 )
	cout << "\n\nO.K. Thank you anyway... I'll be here if you need me. \n\n";

	system("PAUSE");
	
	return 0;
}


I'd be very interested if anyone could explain where my program is causing this mstake.

Also please excuse my inability to write code in a concise way. Any criticism or suggestions for improvement of the code will also be gratefully recieved.
I believe this (-1944256519) is what is called a "garbage value".

Meaning if I declare a variable like this:

int blah;

Then do this:

cout << blah;

Then since blah was given no meaningful value, a garbage value will be cout(ed).


Anyways this is how I'd implement a squaring program, try it out, :) :

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
#include<iostream>
#include<string>
using namespace std;

int main(){

	int number = 0;
	string answer;

	cout << "Wouldst thou like to enter a value to be squared? (Type in 0 to exit at any time.)" << endl;
	cin >> answer;

	if ((answer == "yes") || (answer == "Yes")) {

		cout << "Enter thy number." << endl;

		while (true) {

			if (cin >> number) {

				if (number == 0){break;}
				
				cout << number * number << endl << "Enter thy number." << endl;}}}

	else {}

	return 0;}
Last edited on
You might have exceeded the int limit.
You might want to look at this: http://msdn.microsoft.com/en-us/library/296az74e%28v=vs.80%29.aspx
What Jepcats said is correct, the maximum a signed integer can hold is 2^31 (2147483648) and 180043 squared is 32415481849, causing a buffer overflow.
What Jepcats said is correct, the maximum a signed integer can hold is 2^31 (2147483648) and 180043 squared is 32415481849, causing a buffer overflow.


oh... lawl

In that case I'd say go with a long double, to allow you to use larger number, unfortunately there still will be a limit on the size of the number you can use, I think.

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
#include<iostream>
#include<string>
using namespace std;

int main(){

	long double number = 0;
	string answer;

	cout << "Wouldst thou like to enter a value to be squared? (Type in 0 to exit at any time.)" << endl;
	cin >> answer;

	if ((answer == "yes") || (answer == "Yes")) {

		cout << "Enter thy number." << endl;

		while (true) {

			if (cin >> number) {

				if (number == 0){break;}
				
				cout << number * number << endl << "Enter thy number." << endl;}}}

	else {}

	return 0;}
I've tried both long int and long double and they both give the same negative value. I also tried using unsigned long int which gave a possitive value but was still wrong by quite a way...
Can you look at that website I showed you again.
Your number will STILL be bigger than the maximum for long int and your normal int. Why do you even need such a large number?
Just in case you do, look at the maximum value for _I64 (or long long).

1
2
long long bigNum = 9223372036854775807LL;
cout << bigNum;

That'll work for much bigger numbers, the one I just showed you being the biggest. You might also want to end your long long number in LL. Look it up :)
@creekist: I don't think its a garbage value. If it goes past the max limit, it wraps to the lowest (negative) limit and continues, or vice verse (from my textbook explanation anyway), so basically what warnis said
edit->format selection
or
ctrl + k, ctrl + f

The IDE will format the code for you with either of those. (make sure you select anything you want to be formatted first.)
Last edited on
Thanks for explaining guys. I don't need such a large number, I was just messing around seeing what was what.
Topic archived. No new replies allowed.