I'm working o a simple little program that can give my daughter a few answers to some simple math, but every time I try to get the thing to divide or multiply a number larger than seven digits I get back garbage. what am I doing wrong here?
#include <iostream>
#include <cmath>
usingnamespace std;
constdouble PI = 3.141592653589793;
int main()
{
longdouble firstNum;
longdouble secondNum;
longdouble firstSide;
longdouble secondSide;
longdouble radius;
longdouble height;
longdouble width;
longdouble length;
char choice = ' ';
cout <<"Hey Shandel! you have to decide what you want to do.\n\n";
cout <<"('A' to add, 'S' to subtract, 'M' to multiply, and 'D' to divide)"<<endl;
cout <<"\n";
cout << "('1' will find the area of a circle, '2' will find the area of a square)"<<endl;
cout << "\n";
cout << "('3' will find the area of a triangle, '4' finds the hypotenuse of a triangle)"<<endl;
cout << "\n";
cin >> choice;
if (choice == 'a' || choice == 'A')
{cout <<"\n";
cout <<"Enter the first number you wish to calculate\n";
cout <<"\n";
cin >> firstNum;
cout <<"\n";
cout <<"Now if you would enter the second number in the calculation\n";
cout <<"\n";
cin >> secondNum;
cout <<"\n";
cout <<"Your answer is " <<(firstNum+secondNum)<<endl;}
elseif (choice == 's' || choice == 'S')
{cout <<"\n";
cout <<"Enter the first number you wish to calculate\n";
cout <<"\n";
cin >> firstNum;
cout <<"\n";
cout <<"Now if you would enter the second number in the calculation\n";
cout <<"\n";
cin >> secondNum;
cout <<"\n";
cout <<"Your answer is " <<(firstNum-secondNum)<<endl;}
elseif (choice == 'm' || choice == 'M')
{cout <<"\n";
cout <<"Enter the first number you wish to calculate\n";
cout <<"\n";
cin >> firstNum;
cout <<"\n";
cout <<"Now if you would enter the second number in the calculation\n";
cout <<"\n";
cin >> secondNum;
cout <<"\n";
cout <<"Your answer is " <<(firstNum*secondNum)<<endl;}
elseif (choice == 'd' || choice == 'D')
{cout <<"\n";
cout <<"Enter the first number you wish to calculate\n";
cout <<"\n";
cin >> firstNum;
cout <<"\n";
cout <<"Now if you would enter the second number in the calculation\n";
cout <<"\n";
cin >> secondNum;
cout <<"\n";
cout <<"Your answer is " <<(firstNum/secondNum)<<endl;}
elseif (choice == '1')
{cout << "\n";
cout << "Enter the radius of your circle in the chosen units\n";
cout << "\n";
cin >> radius;
cout << "\n";
cout << "The area of your circle is "<<(PI*(radius*radius))<< " Units" <<endl;}
elseif (choice == '2')
{cout << "\n";
cout << "Enter the length of your square in the chosen units\n";
cout << "\n";
cin >> length;
cout << "\n";
cout << "Now, enter the width of your square in the chosen units\n";
cout << "\n";
cin >> width;
cout << "\n";
cout << "The area of your square is "<<(width*length)<< " Units" <<endl;}
elseif (choice == '3')
{cout << "\n";
cout << "Enter the height of your triangle in the chosen units\n";
cout << "\n";
cin >> height;
cout << "\n";
cout << "Now, enter the base of your triangle in the chosen units\n";
cout << "\n";
cin >> width;
cout << "\n";
cout << "The area of your triangle is "<<(height*(width*.5))<< " Units" <<endl;}
elseif (choice == '4')
{cout << "\n";
cout << "Enter the first side of your triangle in the chosen untis\n";
cout << "\n";
cin >> firstSide;
cout << "\n";
cout << "Enter the second side of your triangle in the chosen units\n";
cout << "\n";
cin >> secondSide;
cout << "\n";
cout << "The remaining side of your triangle is " <<sqrt((firstSide*firstSide)+(secondSide*secondSide))<< " Units" <<endl;}
cout << "\n";
cout <<"Maybe you would like to try to calculate something else?\n";
cout << "\n";
char ch = 'n';
cout <<"If you want to take another shot then just select the 'y' key,"<<
"\notherwise select any other key to quit\n"<<endl;
cin >> ch;
if (ch == 'Y' || ch == 'y')
{system ("CLS");
main ();}
elsereturn 0;
}
Can't you just use double, rather than long double? It should be adequate for any reasonably-sized numbers.
The "error" might actually be to do with how you input long doubles from the terminal, rather than your code per se. When I tried printing out firstNumber and secondNumber immediately after input, they printed out as garbage.
Some minor points:
- you ought to add another
#include <cstdlib>
on order to use the system call.
- I'm not sure that having multiple calls to main() is a good idea (I'm surprised it's allowed).
#include <iostream>
#include <cmath>
#include <cstdlib>
usingnamespace std;
constdouble PI = 3.141592653589793;
int main()
{
longdouble firstNum = 0;
longdouble secondNum = 0;
double firstSide = 0;
double secondSide = 0;
double radius = 0;
double height = 0;
double width = 0;
double length = 0;
char choice = ' ';
cout <<"Hey Shandel! you have to decide what you want to do.\n\n";
cout <<"('A' to add, 'S' to subtract, 'M' to multiply, and 'D' to divide)"<<endl;
cout <<"\n";
cout << "('1' finds the area of a circle, '2' finds the area of a rectangle or square)"<<endl;
cout << "\n";
cout << "('3' finds the area of a triangle, '4' finds the hypotenuse of a triangle)"<<endl;
cout << "\n";
cin >> choice;
if (choice == 'a' || choice == 'A')
{cout <<"\n";
cout <<"Enter the first number you wish to calculate\n";
cout <<"\n";
cin >> firstNum;
cout <<"\n";
cout <<"Now if you would enter the second number in the calculation\n";
cout <<"\n";
cin >> secondNum;
cout <<"\n";
cout <<"Your answer is " <<(firstNum+secondNum)<<endl;}
elseif (choice == 's' || choice == 'S')
{cout <<"\n";
cout <<"Enter the first number you wish to calculate\n";
cout <<"\n";
cin >> firstNum;
cout <<"\n";
cout <<"Now if you would enter the second number in the calculation\n";
cout <<"\n";
cin >> secondNum;
cout <<"\n";
cout <<"Your answer is " <<(firstNum-secondNum)<<endl;}
elseif (choice == 'm' || choice == 'M')
{cout <<"\n";
cout <<"Enter the first number you wish to calculate\n";
cout <<"\n";
cin >> firstNum;
cout <<"\n";
cout <<"Now if you would enter the second number in the calculation\n";
cout <<"\n";
cin >> secondNum;
cout <<"\n";
cout <<"Your answer is " <<(firstNum*secondNum)<<endl;}
elseif (choice == 'd' || choice == 'D')
{cout <<"\n";
cout <<"Enter the first number you wish to calculate\n";
cout <<"\n";
cin >> firstNum;
cout <<"\n";
cout <<"Now if you would enter the second number in the calculation\n";
cout <<"\n";
cin >> secondNum;
cout <<"\n";
cout <<"Your answer is " <<(firstNum/secondNum)<<endl;}
elseif (choice == '1')
{cout << "\n";
cout << "Enter the radius of your circle in the chosen units\n";
cout << "\n";
cin >> radius;
cout << "\n";
cout << "The area of your circle is "<<(PI*(radius*radius))<< " Units" <<endl;}
elseif (choice == '2')
{cout << "\n";
cout << "Enter the length of your square in the chosen units\n";
cout << "\n";
cin >> length;
cout << "\n";
cout << "Now, enter the width of your square in the chosen units\n";
cout << "\n";
cin >> width;
cout << "\n";
cout << "The area of your square is "<<(width*length)<< " Units" <<endl;}
elseif (choice == '3')
{cout << "\n";
cout << "Enter the height of your triangle in the chosen units\n";
cout << "\n";
cin >> height;
cout << "\n";
cout << "Now, enter the base of your triangle in the chosen units\n";
cout << "\n";
cin >> width;
cout << "\n";
cout << "The area of your triangle is "<<(height*(width*.5))<< " Units" <<endl;}
elseif (choice == '4')
{cout << "\n";
cout << "Enter the first side of your triangle in the chosen untis\n";
cout << "\n";
cin >> firstSide;
cout << "\n";
cout << "Enter the second side of your triangle in the chosen units\n";
cout << "\n";
cin >> secondSide;
cout << "\n";
cout << "The remaining side of your triangle is " <<sqrt((firstSide*firstSide)+(secondSide*secondSide))<< " Units" <<endl;}
cout << "\n";
cout <<"Maybe you would like to try to calculate something else?\n";
cout << "\n";
char ch = 'n';
cout <<"If you want to take another shot then just select the 'y' key,"<<
"\notherwise select any other key to quit\n"<<endl;
cin >> ch;
if (ch == 'Y' || ch == 'y')
{system ("CLS");
main ();}
elsereturn 0;
}
Your firstNum and secondNum are stilll declared as long doubles - remove the qualifier long.
If you insist on having them as long doubles, then you may have to put an L at the end of the number you enter at the keyboard.
Whatever you do, I should try printing out the values of firstNum and secondNum as soon as you have input them, to confirm that they have been read correctly.
I have made the changes mentioned, all the variables are doubles now, but I do not have another way to restart the program at the time being without calling main(). Any ideas? I am still trying to figure out how to print the variables directly after it is input, still very new at this, but I'm working on it.