Calculator Program Doesn't Work

Hello, I just started teaching myself to code and I am having a problem with a calculator I am trying to make. The first part is a slope calculator that then converts to y=mx+b. The second is a normal calculator. When I run the normal calculator, it allows you to input the command, but then it doesnt do anything I tell it to after that.
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
#include <iostream>
using namespace std;
main(){
       cout << "Enter Command: ";
       
       while(1){
                
                int a;
                int b;
                int c;
                int d;
                char e;
                char f;
                char g;
                int h;
                int i;
                char j;
                float k;
                float l;
                float m;
                char myArray[50];
                double x, y, result;
                char z;

                cin.getline( myArray, 50, '\n');
                
                if( !strcmp( myArray, "slope")){
                    
                            cout << "SLOPE\n";
                            cout << "Enter Point 1 (x,y): ";
                            cin >> e >> a >> f >> b >> g;
                            
                            cout << "Enter Point 2 (x,y): ";
                            cin >> e >> c >> f >> d >> g;
                            
                            h = d-b;
                            i = c-a;
                            
                            cout << "M=: " << h << "/" << i << "\n";
                            cout << "Calculate equation of line (y/n)? ";
                            cin >> j;
                            
                            if(j=='n'){
                                cout << "Enter Command: ";
                                
                            }else if(j=='y'){
                                  k = h/i;
                                  l = k*a;
                                  m = l+b;
                                  cout << "y=" << k << "x" << "+" << m << "\n";
                                  cout << "Enter a Command: ";
                            }
                            
                            
                }else if( !strcmp( myArray, "quit")){
                      
                            return 0;
                            
                }else if( !strcmp( myArray, "help")){
                      
                      cout << "HELP\n";
                      cout << "slope - Calculate the slope of two points\n";
                      cout << "calculator - Run Calculator\n";
                      cout << "quit - Exit\n";
                      cout << "Enter Command: ";
                }else if( !strcmp( myArray, "calculator")){
                      

                      cout << "CALCULATOR\n";
                      cin >> x >> z >> y;
                      switch(z){
                                case '+':
                                     result = x+y;
                                     break;
                                case '-':
                                     result = x-y;
                                     break;
                                case '*':
                                     result = x*y;
                                     break;
                                case '/':
                                     if(y == 0){
                                          cout << "DIV. BY 0 ERROR";
                                     }else{
                                           result = x/y;
                                     }
                                     break;
                                
                      cout << "Result= " << result << "\n";
                      }
                }
       }              
}
Just move the cout statement in the switch down:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
switch(z){
                                case '+':
                                     result = x+y;
                                     break;
                                case '-':
                                     result = x-y;
                                     break;
                                case '*':
                                     result = x*y;
                                     break;
                                case '/':
                                     if(y == 0){
                                          cout << "DIV. BY 0 ERROR";
                                     }else{
                                           result = x/y;
                                     }
                                     break;
                                
                      cout << "Result= " << result << "\n"; //should not be in the switch 
                      }


A big improvement in your code would be naming the variables something more distinct than simple characters. It makes it easier for others to help as well :)

EDIT: Oh, and you can name multiple variables on one line:

int a, b = 3, c = 0, d, e = 10;
Last edited on
Ok thanks, I didnt realize it was such an easy fix. Thanks.
Topic archived. No new replies allowed.