Void Functions

My program runs perfectly fine until I get to the void functions. The output for the void funtions is "000-1.#IND" What am I doing wrong? Any help or tips are appreciated.
Thank you.
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
//date: 02/08/2014
//Project #3
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>

using namespace std; 

void sum();
void difference();
void multiplication();
void division();

float real1, real2, imaginary1, imaginary2,sumz, differencez, multiplicationz, divisionz;
char sign1, sign2, complex1, complex2;

int main()
{
	cout << "Please enter two complex numbers using the format x+jy or x-jy." << endl;

	float real1, imaginary1;
	char sign1, complex1;

	//Asks the user to input the first complex number
	cout << "Enter the first complex number. (in x +- j y format, seperate each with a space.)" << endl;
	cin >> real1 >> sign1 >> complex1 >> imaginary1;
	if (sign1 == '-')
		imaginary1 = (-1)*imaginary1;
	cout << real1 << sign1 << complex1 << imaginary1 << endl;

	
	float real2, imaginary2;
	char sign2, complex2;
	
	//Asks the user to input the first complex number
	cout << "Enter the second complex number. (in x +- j y format, seperate each with a space.)" << endl;
	cin >> real2 >> sign2 >> complex2 >> imaginary2;
	if (sign2 == '-')
		imaginary2 = (-1)*imaginary2;
	cout << real2 << sign2 << complex2 << imaginary2 << endl;
	

	// Finds the magnitude of the first complex number
	float zmag1;
	zmag1 = (sqrt(real1*real1 + imaginary1*imaginary1));
	abs(zmag1);
		cout << "The magnitude of the first real number is " << abs(zmag1) << endl;


	// Finds the magnitude of the second complex number
	float zmag2;
	zmag2 = (sqrt(real2*real2 + imaginary2*imaginary2));
	abs(zmag2);
		cout << "The magnitude of the second real number is " << abs(zmag2) << endl;


	double pi;
		pi = atan(1) * 4;

	// Finds the argument of the first complex number
	if (real1 > 0 && imaginary1 > 0) {
		cout << "The argument of The first imaginary number is " << atan(imaginary1 / real1) << endl;
	}
	else if (real1 > 0 && imaginary1 < 0) {
		cout << "The argument of The first imaginary number is " << atan(imaginary1 / real1) << endl;
	}
	else	if (real1 < 0 && imaginary1 > 0) {
		cout << "The argument of The first imaginary number is " << pi + atan(imaginary1 / real1) << endl;
	}
	else if (real1 < 0 && imaginary1 < 0){
		cout << "The argument of The first imaginary number is " << -pi + atan(imaginary1 / real1) << endl;
	}


	// FInds the argument of the second complex number
	if (real2 > 0 && imaginary2 > 0) {
		cout << "The argument of The second imaginary number is " << atan(imaginary2 / real2) << endl;
	}
	else if (real2 > 0 && imaginary2 < 0) {
		cout << "The argument of The second imaginary number is " << atan(imaginary2 / real2) << endl;
	}
	else if (real2 < 0 && imaginary2 > 0) {
		cout << "The argument of The second imaginary number is " << pi + atan(imaginary2 / real2) << endl;
	}
	else if (real2 < 0 && imaginary2 < 0){
		cout << "The argument of The second imaginary number is " << -pi + atan(imaginary2 / real2) << endl;
	}

	sum();
	difference();
	multiplication();
	division();
}


void sum() {
	sumz = (real1 + real2) + complex1*(imaginary1 + imaginary2);
		cout << sumz;
}

void difference() {
	differencez = (real1 - real2) + complex1*(imaginary1 - imaginary2);
	cout << differencez;
}

void multiplication() {
	multiplicationz = (real1 * real2-imaginary1*imaginary2) + complex1*(real1*imaginary2 +real2 * imaginary1);
	cout << multiplicationz;
}

void division() {
	divisionz = ((real1 * real2 + imaginary1*imaginary2) + complex1*(real2*imaginary1 - real2 * imaginary2))/(real2*real2+imaginary2*imaginary2);
	cout << divisionz;
}
My professor posted this example as to what the outputs should be.

For example, if 3-j2 and -2+j4 are inputted, the output operations MUST be
EXACTLY as shown below:

|3-j2| = 3.6056
|-2+j4| = 4.4721

arg(3-j2) = -0.588
arg(-2+j4)= 2.0344

(3-j2)+(-2+j4) = 1+j2

(3-j2)-(-2+j4) = 5–j6

(3-j2)*(-2+j4) = 2+j16

(3-j2)/(-2+j4) = -0.7-j0.4
closed account (iAk3T05o)
You have global and local variables with the same name and why are there global variables?
It looks like you want to call "Methods" of an object "compex number" ... but you don't.
As you call FUNCTIONS you should pass arguments (real1, imaginery1 .... or so )
to your functions.
Or you keep your Variables global, but you redeclare them ... what Nathan2222 wrote.

What is "complex2" for, anyway ?

You'll have to calculate a real and a imaginery part ... that makes two number.
What you are doing in your mult...() - fcn for example is to multiply a char with a float, which doesn't make sense from my point of view.


Maybe try s.th. like
1
2
3
4
5
6

void multiplication() {
	float multiplication_real = (real1 * real2-imaginary1*imaginary2); 
        float multiplication_im   = (real1*imaginary2 +real2 * imaginary1);
	cout << multiplication_real << sign << "j" << multiplication_im;
}

(still wrong, it doesn't check the "sign1 and 2" for example)
Don't know if the output format suits your intends, but it's worth trying ...
I'm not completely sure but maybe you could try using int functions instead of void and have the function return those numbers like the divisionz variable.
Topic archived. No new replies allowed.