I\'m trying to take a positive or negative integer input from the user (I.E., 5,
ID: 3637066 • Letter: I
Question
I'm trying to take a positive or negative integer input from the user (I.E., 5, 3, -2, etc...), convert that signed integerto binary (and store it in an array)... But everytime I test my program saying "Stack around 'mydata' is corrupted"...
I'm really not sure what I'm doing wrong.
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
int valueIn;
char mydata [3] = {'0'};
cout << "Enter some value: ";
cin >> valueIn;
int temp = valueIn;
double power = 3;
int powerTemp = 3;
if(valueIn >= 0)
{
cout << valueIn << " is a positive number." << endl;
while( power >= 0 )
{
if( temp >= pow( 2, power ))
{
mydata [ powerTemp ] = '1';
temp = temp - pow( 2, power );
cout << "1";
}
else
{
mydata [ powerTemp ] = '0';
cout << "0";
}
power--;
powerTemp--;
}
cout << endl;
}
else
{
cout << "Negative Number" << endl;
}
cout << "Your final binary value is: " << endl;
for( int tempCount = 3; tempCount != 0; tempCount-- )
{
cout << mydata [tempCount];
}
cout << endl;
return 0;
}