I suspect problems with typecasting!

This is my 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
#include <iostream>
using namespace std;
class Tobi
{
	private:
		int Kons1;
		int Kons2;
		int RT;
		int IT;
	public:
		Tobi(int k1,int k2) // Deklaration und Definition Konstruktor
		{
			Kons1 = k1;
			Kons2 = k2;
		};
		void add(int& rt, int& it)
		{
			
			rt += Kons1;
			it += Kons2;
		}
		void sub(int& rt, int& it)
		{
			rt = Kons1 - rt; // Kons1 = a & rt = c 
			it = Kons2 - it; // Kons2 = b & it = d
		}
		void mul(int& rt, int& it)
		{
			rt = ((Kons1 * rt) - (Kons2 * it)); 
			it = ((Kons2 * rt) + (Kons1 * it));
		}
		void div(int& rt, int& it)
		{ 
			float c; // Not sure if my typecasting is correct!
			c = int (rt);// Not even sure if this is allowed. 
			float d; // Anyway, the result that I get is not correct.
			d = int (it);
			float a;
			a = int (Kons1);
			float b;
			b = int (Kons2);		
			c = ((a * c) + (b * d)) / (( c * c) + (d * d));
			d = ((b * c) - (a * d)) / (( c * c) + (d * d));
		}
											
};
int main()
{
int x, y;
char z;
cin >> x;
cin >> y;
cin >> z;
Tobi v1(1,1);
Tobi v2(2,2);
switch (z)
{
	case 'a':
		v1.add(x,y);
		cout << x << " + " << y << "i" << endl;
		break;	
	case 's':
		v1.sub(x,y);
		cout << x << " + " << y << "i" << endl;
		break;
	case 'm':
		v1.mul(x,y);
		cout << x << " + " << y << "i" << endl;
		break;
	case 'd':
		v1.mul(x,y);
		cout << x << " + " << y << "i" << endl;
		break;		
}

return 0;
}


This is my problem:

The code compiles just fine.

This code is supposed to add, subtract, multiply and divide complex numbers.

The first three operations work just fine.

The DIVISION is not working at all. Any ideas why this is the case? Can I convert int into float like i did?

One other thing in regards to my code.

I am not happy with my objects. Is there a way so that I dont have to put every object in the switch clause?

I am still a beginner. So please be patient with me :)

Cheers, Tobi :)
c is a copy of the value of rt, and d is a copy of the value of it.

When you've completed your calculattion, you need to assign the value back to rt and it as in:
1
2
3
4
5
6
7
8
9
10
11
12
13
		void div(int& rt, int& it)
		{
			float c = rt; // there is a defined conversion from int to float
			float d = it;
			float a = Kons1;
			float b = Kons2;		
			c = ((a * c) + (b * d)) / (( c * c) + (d * d));
			d = ((b * c) - (a * d)) / (( c * c) + (d * d));

			// Return the result
			rt = c; // there'll be a warning about a narrowing conversion
			it = d;
		}

I did that, but the results are still wrong.

Kons1 = a= 1
Kons2 = b = 1

rt = c = 3
it = d =1

Should give you the following result.

4 (/)divided by 10 + 2 (/)divided by 10 i = 0.4 + 0.2i (Unless I am very much mistaken)

But my program gives me this result.

tobias@Lonestar:~/c++$ g++ tobi1.cpp -Wall
tobias@Lonestar:~/c++$ ./a.out
3
1
d
2 + 3i
1
2
3
4
	case 'd':
		v1.mul(x,y);	// should be v1.div(x, y);
		cout << x << " + " << y << "i" << endl;
		break;

Last edited on
thanks KBW! That was indeed wrong. ... but the problem remains. I only get 0 + 0i as a result for any given number and that is obviously wrong.

Has anyone ideas in regards to my other, more general problem, If I create another object I need to basically create another switch table for it. Is there an easier way?

Thanks in advance Tobias
I played around with it and luckily had the file around. This has your code with trace added. Hopefully it'll help you see what's wrong.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	void div(int& rt, int& it)
	{ 
		float c = rt;
		float d = it;
		float a = Kons1;
		float b = Kons2;

		std::cout << "intermediate: c = ((" << a << " * " << c << ") + (" << b << " * " << d
			<< ")) / ((" << c << " * " << c << ") + (" << d << " * " << d << "));" << std::endl;
		c = ((a * c) + (b * d)) / (( c * c) + (d * d));
		std::cout << "intermediate: c = " << c << std::endl;

		std::cout << "intermediate: d = ((" << b << " * " << c << ") - (" << a << " * " << d
			<< ")) / ((" << c << " * " << c << ") + (" << d << " * " << d << "));" << std::endl;
		d = ((b * c) - (a * d)) / (( c * c) + (d * d));
		std::cout << "intermediate: d = " << d << std::endl;

		rt = c;
		it = d;
	}
Topic archived. No new replies allowed.