Something for my daughter

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?
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  #include <iostream>
#include <cmath>
using namespace std;
const double PI = 3.141592653589793;
int main()
{
long double firstNum;
long double secondNum;
long double firstSide;
long double secondSide;
long double radius;
long double height;
long double width;
long double 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;}
          else if (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;}
          else if (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;}
          else if (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;}
          else if (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;}
          else if (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;}
          else if (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;}
          else if (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 ();}
    else
       return 0;
    }
To avoid garbage, initialize all variables you can see with 0.
1
2
3
4
5
6
7
long double firstNum = 0;
long double secondNum = 0;
long double firstSide = 0;
long double secondSide = 0;
long double radius = 0;
long double height = 0;
long double width = 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).
Last edited on
I have made the changes mentioned and yet when I divide 4444444 by 2 I get 2.22222e+006, otherwise it runs great!

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
const double PI = 3.141592653589793;
int main()
{
long double firstNum = 0;
long double 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;}
          else if (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;}
          else if (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;}
          else if (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;}
          else if (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;}
          else if (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;}
          else if (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;}
          else if (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 ();}
    else
       return 0;
    }
Line 118: You should not call main() recursively. It is a violation of the C++ standard.

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.
closed account (LA48b7Xj)
Here is a better way than calling main()

1
2
3
4
5
6
7
8
9
10
11
12
while(true)
{
    //program code here

    cout << "Do you want to continue?\n";

    char ch;
    cin >> ch;

    if(ch != 'y' && ch != 'Y')
	break;
}
Topic archived. No new replies allowed.