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.
#include "StdAfx.h"
#include <iostream>
#include <string>
usingnamespace 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.
#include<iostream>
#include<string>
usingnamespace 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;}
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.
#include<iostream>
#include<string>
usingnamespace std;
int main(){
longdouble 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).
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