help

it runs, but in a loop...

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <math.h>

using namespace std;

int a = 1;
double x;
double y;
int c;
double sum;
int z;
int u;
int q;
int w;
int r;
int e;
int der1 = q * e;
int der2 = e - 1;
int der3 = w *r;
int der4 = r - 1;



int multip()
{

	cout << "please chose a number to multiply" << endl;
	cin >> x;
	cout << "the other number too" << endl;
	cin >> y;

	sum = x * y;

	cout << "the result is " << sum << endl;
	return 0;
}





int division()
{
	cout << "please chose a number to divide" << endl;
	cin >> x;
	cout << "pleas decide a number to divide by" << endl;
	cin >> y;

	sum = x / y;

	cout << "the result is " << sum << endl;
	return 0;


}

int sub()
{
	cout << "please chose a number to subtract" << endl;
	cin >> x;
	cout << "pleas decide a number to subtract by" << endl;
	cin >> y;

	sum = x - y;

	cout << "the result is " << sum << endl;
	return 0;
}

int add()
{
	cout << "please chose a number to add" << endl;
	cin >> x;
	cout << "pleas decide a number to add by" << endl;
	cin >> y;

	sum = x + y;

	cout << "the result is " << sum << endl;
	return 0;
}

int root()
{
	cout << "please chose a number to square root" << endl;
	cin >> x;
	sum = sqrt(x);

	cout << "the result is " << sum << endl;
	return 0;
} 

int log()
{
	cout << "please chose a number to log (natural log)" << endl;
	cin >> x;
	sum = log10(x);

	cout << "the result is " << sum << endl;
	return 0;

}

int expo()
{
	cout << "please chose a base" << endl;
	cin >> x;
	cout << "please chose a exponent" << endl;
	cin >> y;

	sum = pow(x, y);

	cout << "the result is " << sum << endl;
	return 0;

}

int main()
{

	while (a <= 1)
	{



		cout << "please chose an operation: for mult. put 1, for dev but 2 for sub put 3, for addition put 4, square root is 5, log is 6, exponents is 7, enter 8 for derivative" << endl;

		cin >> c;

		if (c == 8) {


			cout << "press 1 for product" << endl;
			cin >> u;
			// multiplication		

			if (u == 1)
			{


				// v'
				cout << "for product enter it in the form of ax^n" << endl;
				cout << "first  enter a then enter n" << endl;
				cin >> q;
				cin >> e;


				//u'
				cout << "now enter the second equation in the same format" << endl;
				cin >> w;
				cin >> r;




				cout << "the derivative is: (" << q << "x^" << e << ")" << der3 << "x^" << der4 << "+(" << w << "x^" << r << ")" << der1 << "x^" << der2 << endl;
			}




		}




		if (c == 2)
		{
			int division();
		}

		if (c == 1)
		{
			int multip();
		}

		if (c == 3)
		{
			int sub();
		}

		if (c == 4)
		{
			int add();
		}
			if (c == 5)
			{
				int root();

			}
			if (c == 6)
			{
				int log();

			}

			if (c == 7)
			{
				int expo();

			}

			cout << "Press 1 followed by enter to begin the calculator, or enter 2 to end" << endl;
			cin >> a;
		}


	}
Last edited on
Well at least you managed to use code tags.

But....
http://www.catb.org/esr/faqs/smart-questions.html#bespecific

1
2
3
4
5
		if (c == 7)
			{
				int expo();

			}

This is just another prototype for expo();
If you want to actually call it, then do something like
int result = expo();


1
2
3
4
5
6
7
8
9
10
int z;
int u;
int q;
int w;
int r;
int e;
int der1 = q * e;
int der2 = e - 1;
int der3 = w *r;
int der4 = r - 1;

Pick MEANINGFUL variable names.

Also, the der variables are NOT automatically recalculated each time you assign say a new value to q.


Use a switch statement instead of a bunch of if's.

I'd write functions to prompt for 1 and 2 variables and call them in each case. This will eliminate lots of redundant code.

Here is a subset of your program that demonstrates what I mean.
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
#include <iostream>
#include <math.h>

using namespace std;

void get1(double &a, const string &prompt)
{
    cout << prompt << '\n';
    cout << "please enter a: ";
    cin >> a;
}

void get2(double &a, double &b, const string &prompt)
{
    get1(a, prompt);   // Call get1() to get the first value.
    cout << "please enter b: ";
    cin >> b;
}


int
main()
{
    double a,b;
    int c;
    
    while (true) {

	cout <<
	    "please chose an operation: for mult. put 1, for dev but 2 for sub put 3, for addition put 4, square root is 5, log is 6, exponents is 7, enter 8 for derivative"
	    << endl;
	cout << "Press ctrl-d to exit\n";
	cin >> c;
	if (!cin) break;	// exit the loop on error or end of file

	switch(c) {
	case 8:
	case 6:
	case 7:
	    cout << "Not yet implemented";
	    break;
	case 2:
	    get2(a,b, "Computing a/b");
	    cout << "result is " << a/b << '\n';
	    break;

	case 5:
	    get1(a, "Computing sqrt(a)");
	    cout << "result is " << sqrt(a) << '\n';
	    break;

	}
    }
}

I've never heard if cases and switch... I'll do more research, thank you! This is for my AP computer science exam.
Topic archived. No new replies allowed.