asking for a 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

void deg ();
void calc ();
void sq ();
void po ();

void ask () {
	int choi;
	cout << "1. regular calculation" << "\n";
	cout << "2. cos, sin, tan\n";
	cout << "3. square root\n";
	cout << "4. power\n";
	cin >> choi;

	switch (choi) {
	case 1: calc();break;
	case 2: deg(); break;
	case 3: sq(); break;
	case 4: po(); break;
	default: break;
	}
}

void calc () {
	int num1, num2, res;
	char oper, choi;
	cout << "";
	cin >> num1 >> oper >> num2;

	switch (oper) {
	case '+': {
		res = num1 + num2;
		cout << res << "\n";
		break;
	}

	case '-': {
		res = num1 - num2;
		cout << res << "\n";
		break;
	}

	case '/': {
		res = num1 / num2;
		cout << res << "\n";
		break;
	}

	case '*': {
		res = num1 * num2;
		cout << res << "\n";
		break;
	}

	default:
		break;
	}

	cout << "again? (y/n): ";
	cin >> choi;

	if (choi == 'y')
		ask();

	else
		exit;
}

void deg () {
	int num_deg;
	char * cho, choi;
	float res;
	cout << "";
	cin >> choi >> num_deg;

	switch (choi) {
	case 'sin': {
		res = sin(num_deg);
		cout << res << "\n";
		break;
	}

	case 'cos': {
		res = cos(num_deg);
		cout << res << "\n";
		break;
	}

	case 'tan': {
		res = tan(num_deg);
		cout << res << "\n";
		break;
	}

	default:
		break;
	}

	cout << "again? (y/n): \n";
	cin >> choi;

	if (choi == 'y')
		ask();

	else
		exit;
}

void sq () {
	int sq_num;
	float res;
	char choi;
	cout << "";
	cin sq_num;
	res = sqrt(sq_num);
	cout << res << "\n";
	cout << "again? (y/n): ";
	cin >> choi;

	if (choi == 'y')
		ask();

	else
		exit;
}

void po () {
	int num_pow;
	char choi;
	cout << "";
	cin >> num_pow;
	cout << pow(num_pow) << "\n";
	cout << "again? (y/n): ";
	cin >> choi;

	if (choi == 'y')
		ask();

	else
		exit;
}

int main() {
		
	ask();

	return 0;
}


is something wrong with the code above?
well, it would be nice if you tell us what you are trying to achieve, and what problems you are having. we might understand your code one way, while u may be trying to achieve something else.

i found a couple of things that are wrong no matter what you are trying to achieve though:

LINE 75 - you need to specify an array (char[3]) because you are trying to get a string of 3 letters from the user. as of now, "choi" will get the first character the user enters, ("s", "c" or "t"). none of your switch statements would work because "choi" is 1 letter, while the cases are three letters. also, what's that pointer *choi doing there? you don't seem to use it at all.

LINE 118 - you are missing ">>" after cin. so your whole script won't work

also, by the format you have now (for example, for the calc() function) you use the statement

cin >> num1 >> oper >> num2;

for this to work, you have to separate your choices with a space. so typing

"1 + 2" would work
"1+2" would not
Topic archived. No new replies allowed.