Hey, I recently made an exponation program. It works fine, just with one problem: If a number is too big, it messes up and produces a weird number/word/operater mix.
For ex: I recently got a result of something like 5.10623 + 012, when I looked in the debugger I was using for the value of what was being displayed, the actualy number was more like 51062355165 or something like that.
//Made using the template created by Stephan Randy Davis
//Created by Randy Wills
#include <cstdio>
#include <cstdlib>
#include <iostream>
usingnamespace std;
double* newBase()
{
double* B;
B = newdouble;
return B;
}
int main(int nNumberofArgs, char* pszArgs[])
{
//Warn users that exponants with decimals will not be tolerated
cout << "Warning: Decimal points on exponants will not be recognized by this program.\n";
//Start declaration of everything
int* Expo; //Declare Expo
double Base;
//Assign Value to Base
cout << "Enter the base (number to be multiplied):";
cin >> Base;
//Assign Value to Expo
cout << "Now enter how many times the number is to be multiplied:";
Expo = newint;
cin >> *Expo;
//Assign Value to BaseB
double* BaseB = newBase();
*BaseB = Base*Base;
//Check if the Expo equals or is under the value of 1
if (*Expo <= 1)
{
//Take away from Expo, to make Zero or less
--*Expo;
// Finish Program by displaying Base
cout << Base;
cout << "\nFinished...";
system("PAUSE");
return 0;
}
//Take away from Expo, to make one less than Expo was assigned. For Mathematical Reasons
--*Expo;
//Check if Expo is Above the value of 1
if (*Expo > 1)
{
while (*Expo > 1)
{
//Start the Exponation Process
*BaseB = (*BaseB*Base);
//Take away from Expo, to make sure the loop is not infinite
--*Expo;
}
}
//Finish up the Program by displaying BaseB, which is the product of the
//repeated Exponation Process
delete Expo;
cout << *BaseB;
cout << "\nNow, please leave the area.\n";
system("PAUSE");
return 0;
}
I've just started editing it in order to fix my problem, so that's why I have the pointers and stuff.
Yes, there is a way to prevent scientific notation, but you should know that the result may be inaccurate. "double" and "float" use floating point precision, which means that when the number reaches a certain size, the least significant digits will be removed. E.g. 1000000000001 might become 1000000000000.
Try this:
1 2
cout.precision(0);
cout << fixed << *BaseB;
BTW: It's a lot easier to use int and double stack variables, instead of allocating on the heap with new.