calculator project

Alright I'm having some trouble with making a calculator. Its a simple text based calculator that I plan to add more features to when I get it working correctly. Main problem: Whenever I enter the answer to the first cin words start falling like drops in a waterfall. Try it out. (I'm using latest version of Dev-C++)

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
123
124
125
126
127
128
129
 // Draft 1 - Simple Text Based Caculator
//Include Files

#include <iostream> //basic input output operators
#include <math.h> // additional features for when pausing works

//Shortcuts to avoid having to type std::cout/cin everytime

using std::cout;
using std::cin;
using namespace std;
//Beginning of actual calculator
int main ()
{
     char Done;
     char Subtraction;
     char Addition;
     char Multiplication;
     char Division;
     char answer;
     labelA:
             cout << "What basic mathematical operator shall I perform ?";
     cout << "Please answer with: Division, Multiplication, Addition, Subtraction or Done.";
     cin >> answer;
     cin.get();
     if (answer == Division); //Division section of the calculator
     { 
                //Declaring the variables for use in the division function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
                
                long double value1;
                long double value2;
                
                //asking for and storing the values to be divided
                 
                      cout << "Input value to be divided: ";
                      cin >> value1;
                      cin.get();
                      cout << "Input the dividend: ";
                      cin >> value2;
                      cin.get();
                
                //dividing the values and outputting the answer
                
                value1 /= value2;
                cout << value1;
                cin.get();
                goto labelA;
     }
     if (answer == Multiplication); //Multiplication section of the calculator
     { 
                //Declaring the variables for use in the multiplication function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
                
                long double value1;
                long double value2;
                
                //asking for and storing the values to be multiplied
                 
                      cout << "Input value to be first number: ";
                      cin >> value1;
                      cin.get();
                      cout << "Input the second number: ";
                      cin >> value2;
                      cin.get();
                
                //multiplying the values and outputting the answer
                
                value1 *= value2;
                cout << value1;
                cin.get();
                goto labelA;
                }
                if (answer == Addition); //Addition section of the calculator
     { 
                //Declaring the variables for use in the addition function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
                
                long double value1;
                long double value2;
                
                //asking for and storing the values to be added
                 
                      cout << "Input the first value : ";
                      cin >> value1;
                      cin.get();
                      cout << "Input the second value: ";
                      cin >> value2;
                      cin.get();
                
                //subtracting the values and outputting the answer
                
                value1 += value2;
                cout << value1;
                cin.get();
                goto labelA;
                }
                if (answer == Subtraction); //Subtraction section of the calculator
     { 
                //Declaring the variables for use in the subtraction function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
                
                long double value1;
                long double value2;
                
                //asking for and storing the values to be subtracted
                 
                      cout << "Input value to be subtracted from: ";
                      cin >> value1;
                      cin.get();
                      cout << "Input the number to subtract from the first: ";
                      cin >> value2;
                      cin.get();
                
                //subtracted the values and outputting the answer
                
                value1 -= value2;
                cout << value1;
                cin.get();
                goto labelA;
                }
                if (answer == Done); //Function to end the program
                cout << "Good bye";
                return 0;
                }


Thank you for your help!
Your if statements compare the user's input to Division, Multiplication, etc. all characters which you never define. So you are comparing input to undefined, uninitialized values. It's not going to work.

And I see you are using goto statements. Beware: your code will become illegible and impossible to read if you use goto statements too liberally. I know my professors refuse to even look at homeworks and assignments with goto statements. Use loops instead.
How exactly would I use a loop instead of goto?
Defining the statements didnt help much.
New code:
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
123
124
125
126
127
128
// Draft 1 - Simple Text Based Caculator
//Include Files

#include <iostream> //basic input output operators

//Shortcuts to avoid having to type std::cout/cin everytime

using std::cout;
using std::cin;
using namespace std;
//Beginning of actual calculator
int main ()
{
     char Done = Done;
     char Subtraction = Subtraction;
     char Addition = Addition;
     char Multiplication = Multiplication;
     char Division = Division;
     char answer = answer;
     labelA:
             cout << "What basic mathematical operator shall I perform ?";
     cout << "Please answer with: Division, Multiplication, Addition, Subtraction or Done.";
     cin >> answer;
     cin.get();
     if (answer == Division); //Division section of the calculator
     { 
                //Declaring the variables for use in the division function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
                
                long double value1;
                long double value2;
                
                //asking for and storing the values to be divided
                 
                      cout << "Input value to be divided: ";
                      cin >> value1;
                      cin.get();
                      cout << "Input the dividend: ";
                      cin >> value2;
                      cin.get();
                
                //dividing the values and outputting the answer
                
                value1 /= value2;
                cout << value1;
                cin.get();
                goto labelA;
     }
     if (answer == Multiplication); //Multiplication section of the calculator
     { 
                //Declaring the variables for use in the multiplication function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
                
                long double value1;
                long double value2;
                
                //asking for and storing the values to be multiplied
                 
                      cout << "Input value to be first number: ";
                      cin >> value1;
                      cin.get();
                      cout << "Input the second number: ";
                      cin >> value2;
                      cin.get();
                
                //multiplying the values and outputting the answer
                
                value1 *= value2;
                cout << value1;
                cin.get();
                goto labelA;
                }
                if (answer == Addition); //Addition section of the calculator
     { 
                //Declaring the variables for use in the addition function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
                
                long double value1;
                long double value2;
                
                //asking for and storing the values to be added
                 
                      cout << "Input the first value : ";
                      cin >> value1;
                      cin.get();
                      cout << "Input the second value: ";
                      cin >> value2;
                      cin.get();
                
                //subtracting the values and outputting the answer
                
                value1 += value2;
                cout << value1;
                cin.get();
                goto labelA;
                }
                if (answer == Subtraction); //Subtraction section of the calculator
     { 
                //Declaring the variables for use in the subtraction function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
                
                long double value1;
                long double value2;
                
                //asking for and storing the values to be subtracted
                 
                      cout << "Input value to be subtracted from: ";
                      cin >> value1;
                      cin.get();
                      cout << "Input the number to subtract from the first: ";
                      cin >> value2;
                      cin.get();
                
                //subtracted the values and outputting the answer
                
                value1 -= value2;
                cout << value1;
                cin.get();
                goto labelA;
                }
                if (answer == Done); //Function to end the program
                {cout << "Good bye";
                return 0;
                }                                 
Last edited on
A char type variable can only store one character. If you need to store more than one, I think you have to use either a char array or a string object.

Something like this:
1
2
3
4
#include <string>
string answer;
cin>>answer;
if (answer == "Division") {}; 
Last edited on
Now it doesn't wait for me to input anything.
closed account (L1T0RXSz)
I'm sorry this has nothing to do with this... what is the goto statement?
Last edited on
The goto statement simply goes to the label listed after it. Or at least its supposed to. I use it because that way I can simply goto the operator requested by the user, I would recommend using a loop instead as the goto statement can seriously hurt your programs logic but I cant wrap my brain around how I would do that here.
Last edited on
New code after some updates:
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
123
124
125
126
127
128
129
130
131
132
133
// Draft 1 - Simple Text Based Caculator
// NEEDS SOMETHING TO PAUSE THE PROGRAM AFTER EACH OUTPUT!!!
//Include Files

#include <iostream>
#include <string>

//Shortcuts to avoid having to type std::cout/cin everytime

using std::endl;
using std::cout;
using std::cin;
using namespace std;

//Beginning of actual calculator

int main()
{
     string Done = Done;
     string Subtraction = Subtraction;
     string Addition = Addition;
     string Multiplication = Multiplication;
     string Division = Division;
     string answer = answer;
     labelA: 
             cout << "What basic mathematical operator shall I perform ?" << endl;
     cout << "Please answer with: Division, Multiplication, Addition, Subtraction or Done." << endl;
     cin >> answer;
     cin.get();
     if (answer == Division); //Division section of the calculator
     { 
                //Declaring the variables for use in the division function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
                
                long double value1;
                long double value2;
                long double answer1;
                
                //asking for and storing the values to be divided
                 
                      cout << "Input value to be divided: " << endl;
                      cin >> value1;
                      cin.get();
                      cout << "Input the dividend: " << endl;
                      cin >> value2;
                      cin.get();
                
                //dividing the values and outputting the answer
                
                value1 /= value2;
                cout << value1 << endl;
                goto labelA;
     }
     if (answer == Multiplication); //Multiplication section of the calculator
     { 
                //Declaring the variables for use in the multiplication function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
                
                long double value1;
                long double value2;
                
                //asking for and storing the values to be multiplied
                 
                      cout << "Input value to be first number: " << endl;
                      cin >> value1;
                      cin.get();
                      cout << "Input the second number: " << endl;
                      cin >> value2;
                      cin.get();
                
                //multiplying the values and outputting the answer
                
                value1 *= value2;
                cout << value1 << endl;
                goto labelA;
                }
                if (answer == Addition); //Addition section of the calculator
     { 
                //Declaring the variables for use in the addition function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
                
                long double value1;
                long double value2;
                
                //asking for and storing the values to be added
                 
                      cout << "Input the first value : " << endl;
                      cin >> value1;
                      cin.get();
                      cout << "Input the second value: " << endl;
                      cin >> value2;
                      cin.get();
                
                //adding the values and outputting the answer
                
                value1 += value2;
                cout << value1 << endl;
                goto labelA;
                }
                if (answer == Subtraction); //Subtraction section of the calculator
     { 
                //Declaring the variables for use in the subtraction function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
                
                long double value1;
                long double value2;
                
                //asking for and storing the values to be subtracted
                 
                      cout << "Input value to be subtracted from: " << endl;
                      cin >> value1;
                      cin.get();
                      cout << "Input the number to subtract from the first: " << endl;
                      cin >> value2;
                      cin.get();
                
                //subtracting the values and outputting the answer
                
                value1 -= value2;
                cout << value1 << endl;
                goto labelA;
                }
                if (answer == Done);
                cout << "Good bye";
                cin.get();
                return 0;
                }
                
                 
Update: Program now pauses but follows a linear path regardless of user input.
[code]
// Draft 1 - Simple Text Based Caculator
//Program will go through
//Include Files

#include <iostream>
#include <string>

//Shortcuts to avoid having to type std::cout/cin/endl everytime

using std::endl;
using std::cout;
using std::cin;
using namespace std;

//Beginning of actual calculator

int main()
{
int Done = 5;
int Subtraction = 4;
int Addition = 3;
int Multiplication = 2;
int Division = 1;
int answer = 5;

cout << "What basic mathematical operator shall I perform ?" << endl;
cout << "Please answer with: Division(1), Multiplication(2), Addition(3), Subtraction(4) or Done(5)." << endl;
cin >> answer;
// Program will go through a linear path regardless of user input at the beginning.
if (answer == 1); //Division section of the calculator
{
//Declaring the variables for use in the division function
//They can be used again(in a different sub function)
// because they are declared in a sub function

long double value1;
long double value2;
long double answer1;

//asking for and storing the values to be divided

cout << "Input value to be divided: " << endl;
cin >> value1;
cout << "Input the dividend: " << endl;
cin >> value2;

//dividing the values and outputting the answer

value1 /= value2;
cout << value1 << endl;
}
if (answer == 2); //Multiplication section of the calculator
{
//Declaring the variables for use in the multiplication function
//They can be used again(in a different sub function)
// because they are declared in a sub function

long double value1;
long double value2;

//asking for and storing the values to be multiplied

cout << "Input value to be first number: " << endl;
cin >> value1;
cout << "Input the second number: " << endl;
cin >> value2;

//multiplying the values and outputting the answer

value1 *= value2;
cout << value1 << endl;
}
if (answer == 3); //Addition section of the calculator
{
//Declaring the variables for use in the addition function
//They can be used again(in a different sub function)
// because they are declared in a sub function

long double value1;
long double value2;

//asking for and storing the values to be added

cout << "Input the first value : " << endl;
cin >> value1;
cout << "Input the second value: " << endl;
cin >> value2;

//adding the values and outputting the answer

value1 += value2;
cout << value1 << endl;
}
if (answer == 4); //Subtraction section of the calculator
{
//Declaring the variables for use in the subtraction function
//They can be used again(in a different sub function)
// because they are declared in a sub function

long double value1;
long double value2;

//asking for and storing the values to be subtracted

cout << "Input value to be subtracted from: " << endl;
cin >> value1;
cout << "Input the number to subtract from the first: " << endl;
cin >> value2;

//subtracting the values and outputting the answer

value1 -= value2;
cout << value1 << endl;
}
if (answer == 5);
cout << "Good bye";
return 0;
}
[code]
Last edited on
Big progress made. Alright all I need to do is to put this in a loop. I have nearly no experience with loops so please help.
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
123
124
125
126
127
128
129
130
// Draft 1 - Simple Text Based Caculator
//Program ends after getting user input the second time.
//Include Files

#include <iostream>
#include <string>
#include <cmath>

//Shortcuts to avoid having to type std::cout/cin/endl everytime

using std::endl;
using std::cout;
using std::cin;
using namespace std;

//Beginning of actual calculator

int main()
{
     int Done = 5;
     int Subtraction = 4;
     int Addition = 3;
     int Multiplication = 2;
     int Division = 1;
     int answer = 5;
            
     cout << "What basic mathematical operator shall I perform ?" << endl;
     cout << "Please answer with: Division(1), Multiplication(2), Addition(3), Subtraction(4) or Done(5)." << endl;
     cin >> answer;
     // Program will go through a linear path regardless of user input at the beginning.
     if (answer == 1) //Division section of the calculator
     { 
                //Declaring the variables for use in the division function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
                
                long double value1;
                long double value2;
                
                //asking for and storing the values to be divided
                 
                      cout << "Input value to be divided: " << endl;
                      cin >> value1;
                      cout << "Input the dividend: " << endl;
                      cin >> value2;
                
                //dividing the values and outputting the answer
                
                value1 /= value2;
                cout << value1 << endl;
                cout << "Please input the next operation number you would like to preform." << endl;
                cin >> answer;
                
     }
     if (answer == 2) //Multiplication section of the calculator
     { 
                //Declaring the variables for use in the multiplication function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
                
                long double value1;
                long double value2;
                
                //asking for and storing the values to be multiplied
                 
                      cout << "Input value to be first number: " << endl;
                      cin >> value1;
                      cout << "Input the second number: " << endl;
                      cin >> value2;
                
                //multiplying the values and outputting the answer
                
                value1 *= value2;
                cout << value1 << endl;
                cout << "Please input the next operation number you would like to preform." << endl;
                cin >> answer;
                }
                if (answer == 3) //Addition section of the calculator
     { 
                //Declaring the variables for use in the addition function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
                
                long double value1;
                long double value2;
                
                //asking for and storing the values to be added
                 
                      cout << "Input the first value : " << endl;
                      cin >> value1;
                      cout << "Input the second value: " << endl;
                      cin >> value2;
                
                //adding the values and outputting the answer
                
                value1 += value2;
                cout << value1 << endl;
                cout << "Please input the next operation number you would like to preform." << endl;
                cin >> answer;
                }
                if (answer == 4) //Subtraction section of the calculator
     { 
                //Declaring the variables for use in the subtraction function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
                
                long double value1;
                long double value2;
                
                //asking for and storing the values to be subtracted
                 
                      cout << "Input value to be subtracted from: " << endl;
                      cin >> value1;
                      cout << "Input the number to subtract from the first: " << endl;
                      cin >> value2;
                
                //subtracting the values and outputting the answer
                
                value1 -= value2;
                cout << value1 << endl;
                cout << "Please input the next operation number you would like to preform." << endl;
                cin >> answer;
                }
                if (answer = 5)
                cout << "Good bye";
                return 0;
                }
                
                
                 
Topic archived. No new replies allowed.