Using sqrt, atan, cos and sin

Trying to convert rectangular complex numbers to polar form. Unfortunately I have no clue how to use the commands used in the title in c++, despite reading up here:

http://www.cplusplus.com/reference/cmath/atan/

Help is much appreciated. The error is line 149 saying sqrt cannot be used as a function, when mathematically it can.

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
  #include <iostream> // saves basic input output functions

using namespace std;

int main()
{
    char option;
	char optionSim;
	int sim1, sim2, simSum;
	char optionCom;
	int comReal1, comImag1, comReal2, comImag2, comSumR, comSumI;
	int imag;
	int pythag, polarR, polarI, polarA;

	do //do while loop, shows menu until user exits
	{
		// shows Options for the menu
		cout << "\n Main Menu" << endl;
        cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
		cout << "1) Simple Number Calculator " << endl;
		cout << "2) Complex Number Calculator " << endl;
		cout << "3) Calculate Capacitance " << endl;
		cout << "4) Calculate Reactance " << endl << endl;
		cout << "Q) Quit Program " << endl;
		//user input's a number
		cout << "Please select an option : ";
		cin >> option;  // taking option value as input and saving in variable "option"
		option = toupper(option); // check for upper case 'Q'

		if (option == '1') // Simple Calc
		{
			cout << "\n Simple Calculator" << endl;
			cout << "~~~~~~~~~~~~~~~~~~~~~" << endl;
			cout << "Do you want to do... " << endl;
			cout << "1) Addition " << endl;
			cout << "2) Subtraction " << endl;
			cout << "3) Multiplication " << endl;
			cout << "4) Division " << endl;
			cout << "5) Go back to main " << endl << endl;
			cout << "Please select an option : " << endl;
			cin >> optionSim;

			if (optionSim == '1')
            {
                cout << "Please input your numbers: " << endl << endl;
                cin >> sim1;
                cin >> sim2;

                simSum = sim1 + sim2;
                cout << "The sum of those numbers are " << simSum << endl << endl;

            }
			else if (optionSim == '2')
            {
                cout << "Please input your numbers: " << endl << endl;
                cin >> sim1;
                cin >> sim2;

                simSum = sim1 - sim2;
                cout << "The sum of those numbers are " << simSum << endl << endl;
            }
            else if (optionSim == '3')
            {
                cout << "Please input your numbers: " << endl << endl;
                cin >> sim1;
                cin >> sim2;

                simSum = sim1 * sim2;
                cout << "The sum of those numbers are " << simSum << endl << endl;
            }
            else if (optionSim == '4')
            {
                cout << "Please input your numbers: " << endl << endl;
                cin >> sim1;
                cin >> sim2;

                simSum = sim1 / sim2;
                cout << "The sum of those numbers are " << simSum << endl << endl;
            }
            else if (optionSim == '5')
            {
                cout << "go back " << endl;
            }
		}
		else if (option == '2') // Complex Calc
		{
			cout << "\n Complex Calculator" << endl;
			cout << "~~~~~~~~~~~~~~~~~~~~~~" << endl;
			cout << "Do you want to do... " << endl;
			cout << "1) Addition " << endl;
			cout << "2) Subtraction " << endl;
			cout << "3) Multiplication " << endl;
			cout << "4) Division " << endl;
			cout << "5) Go back to main " << endl << endl;
			cout << "Please select an option : " << endl;
			cin >> optionCom;

			if (optionCom == '1') //addition
            {
                cout << "Given your question in the form '(a +- bi) + (c +- di) the answer will be given in rectangular form" << endl;
                cout << "What is the real part of the first number" << endl;
                cin >> comReal1;
                cout << "What is the imaginary multiplier of the first number" << endl;
                cin >> comImag1;
                cout << "What is the real part of the second number" << endl;
                cin >> comReal2;
                cout << "What is the imaginary multiplier of the second number" << endl;
                cin >> comImag2;

                comSumR = comReal1 + comReal2; //add the real variable, then add the imaginary, put into line below
                comSumI = comImag1 + comImag2;

                cout <<"Your answer is "<<comSumR<<" + "<<comSumI<<"i" << endl;

            }
			else if (optionCom == '2')
            {
                cout << "Given your question in the form '(a +- bi) - (c +- di) the answer will be given in rectangular form" << endl;
                cout << "What is the real part of the first number" << endl;
                cin >> comReal1;
                cout << "What is the imaginary multiplier of the first number" << endl;
                cin >> comImag1;
                cout << "What is the real part of the second number" << endl;
                cin >> comReal2;
                cout << "What is the imaginary multiplier of the second number" << endl;
                cin >> comImag2;

                comSumR = comReal1 - comReal2; //add the real variable, then add the imaginary, put into line below
                comSumI = comImag1 - comImag2;

                cout <<"Your answer is "<<comSumR<<" + "<<comSumI<<"i" << endl;
            }
            else if (optionCom == '3')
            {
                cout << "Given your question in the form '(a +- bi) - (c +- di) the answer will be given in polar form" << endl;
                cout << "What is the real part of the first number" << endl;
                cin >> comReal1;
                cout << "What is the imaginary multiplier of the first number" << endl;
                cin >> comImag1;
                cout << "What is the real part of the second number" << endl;
                cin >> comReal2;
                cout << "What is the imaginary multiplier of the second number" << endl;
                cin >> comImag2;

                comSumR = (comReal1 * comReal2);
                comSumI = (comImag1 * comImag2) + (comReal1 * comImag1) + (comReal1 * comImag2);

                double sqrt, atan, cos, sin;                                        //   <-----   This is the part I'm struggling to get
                pythag = sqrt((comSumR*comSumR)+(comSumI*comSumI));
                polarA = atan (comSumI / comSumR);
                polarR = (pythag)*cos(polarA);
                polarI = (pythag)*sin(polarA);

                cout << "Your answer is "<<polarR<<" + "<<polarI<<"i "<< endl;

            }
            else if (optionCom == '4')
            {

            }
            else if (optionSim == '5')
            {
                cout << "go back " << endl;
            }
            else
            {
                cout << "That is not an option." << endl;
            }
		}
		else if (option == '3') // Capacitance
		{
			cout << "Do some Capacitance calcs here." << endl << endl;
		}
		else if (option == '4') // Reactance
		{
			cout << "Do a little Reactance calc here." << endl << endl;

		}
		else if (option == 'Q') // Exit
		{
			cout << "Terminating Program" << endl << endl << endl;
		}
		else
		{
			//Displaying error message
			cout << "That is not an option." << endl << endl;
		}
	} while (option != 'Q');  //condition of do-while loop

	cout << "Program is finished!!" << endl << endl << endl;

	return 0;
}
Last edited on
What exactly is not clear to you?

sqrt() — Expect some value as argument, return floating point value equal to square root of argument:
cout << sqrt(4); //Prints 2

sin() / cos() — Expect angle in radians as parameter, returns value of sine or cosine:
cout << cos(3.14159265358); //Prints -1

atan() — expects some value as argument, returns angle in range [-pi/2,+pi/2] which tangent is equal to value.
cout << atan(1); //Prints 0.78539816339 (Pi / 4)
Last edited on
So it can only be used to print, and not to actually change the value of the variable then?

Here is what I am trying to do:

1
2
3
4
5
6
7
8
9
10
comSumR = (comReal1 * comReal2);
                comSumI = (comImag1 * comImag2) + (comReal1 * comImag1) + (comReal1 * comImag2);

                double sqrt, atan, cos, sin;                                        //   <-----   This is the part I'm struggling to get
                pythag = sqrt((comSumR*comSumR)+(comSumI*comSumI));
                polarA = atan (comSumI / comSumR);
                polarR = (pythag)*cos(polarA);
                polarI = (pythag)*sin(polarA);

                cout << "Your answer is "<<polarR<<" + "<<polarI<<"i "<< endl;
Last edited on
So it can only be used to print, and not to actually change the value of the variable then?
They are functions. They get some value and return result back. What you will do with this result is up to you.

double sqrt, atan, cos, sin;
What is this for? You do not need it (as those variables are not used anyway and reusing names from standard library is not a good idea)

Your code looks fine aside from line 4 (which is not needed) and 6 — you potentially need angle correction here if real part is negative; or you can use atan2() instead
Right, so that bit's removed (I just saw that being used in the examples):

1
2
3
4
5
6
7
8
9
 comSumR = (comReal1 * comReal2);
                comSumI = (comImag1 * comImag2) + (comReal1 * comImag1) + (comReal1 * comImag2);

                pythag = sqrt((comSumR*comSumR)+(comSumI*comSumI));
                polarA = atan2 (comSumI / comSumR);
                polarR = (pythag)*cos(polarA);
                polarI = (pythag)*sin(polarA);

                cout << "Your answer is "<<polarR<<" + "<<polarI<<"i "<< endl;


However I get the error message:

'srt' was not declared in this scope
'atan' was not declared in this scope
etc.

I believe that is what happens when a variable is not declared, however these should be functions, not variables, so I'm unsure as to why that message is used.
Did you #include <cmath> ? And atan2 works differently. Consult reference: http://en.cppreference.com/w/cpp/numeric/math/atan2
Aha! Thank you so much! That was the issue all along. And yes I've just changed my program accordingly :)
Topic archived. No new replies allowed.