unsigned char value disappearing

Haiyo ^^
Whenever I try to pass an unsigned char by reference it somehow becomes empty, while running the program, it prints out proper values of the unsigned chars during calculations, but when I want to print them out at the very end it messes up, they become empty, and the program passes an empty char as reference.


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
#include <iostream>
using namespace std;

void getnum(double &result, unsigned char &c1, unsigned char &c2, unsigned char &c3)
{
	result = rand()%10000;
	cout << "rand: " << result;
	result = result / 1000;
	cout << "/1000 = " << result << "\t";
	
	if (result < 1)
	{
		result = result * 100;
		cout << "*100 = " << result;
		/*rounding*/if (result > 100) result = 100;
		double c1 = 255 - result;
		cout << "\tc1: " << c1;
	}
	
	else if (result > 1)
	{
		double c2 = 255;
		double c3 = 0;
		result = result * 10;
		cout << "*10 = " << result;
		/*rounding*/if (result > 100) result = 100;
		if (result > 50)
		{
			c2 = 255 - result;
		}
		else
		{
			c3 = 100 - result*2;
		}
		cout << "\tcolor2 = " << c2 << "\tcolor3 = " << c3;
	} 
	else 
	{
		c1=177;c2=177;c3=177; 
		cout << "c1,2,3:" << c1 << "," << c2 << "," << c3;
	}
	//cout << "\t (c1,c2,c3): " << c1 << ", " << c2 << ", " << c3 << endl;
cout << endl;
}

int main()
{
	double r = 0;
	unsigned char c1 = 0, c2 = 0, c3 = 0;

	for (int i=0; i<52; i++){
	getnum(r,c1,c2,c3);
	//cout << "r: " << r << ", c1,c2,c3: (" << c1 << "," << c2 << "," << c3 << ")" << endl;
	}
	system("PAUSE");
	return 0;
}


output with //cout << "\t (c1,c2,c3): " << c1 << ", " << c2 << ", " << c3 << endl; uncommented and couts commented
rand: 41/1000 = 0.041   *100 = 4.1       (c1,c2,c3):  ,  ,
rand: 8467/1000 = 8.467 *10 = 84.67      (c1,c2,c3):  ,  ,
rand: 6334/1000 = 6.334 *10 = 63.34      (c1,c2,c3):  ,  ,
rand: 6500/1000 = 6.5   *10 = 65         (c1,c2,c3):  ,  ,
rand: 9169/1000 = 9.169 *10 = 91.69      (c1,c2,c3):  ,  ,
rand: 5724/1000 = 5.724 *10 = 57.24      (c1,c2,c3):  ,  ,
rand: 1478/1000 = 1.478 *10 = 14.78      (c1,c2,c3):  ,  ,
rand: 9358/1000 = 9.358 *10 = 93.58      (c1,c2,c3):  ,  ,
rand: 6962/1000 = 6.962 *10 = 69.62      (c1,c2,c3):  ,  ,
rand: 4464/1000 = 4.464 *10 = 44.64      (c1,c2,c3):  ,  ,
rand: 5705/1000 = 5.705 *10 = 57.05      (c1,c2,c3):  ,  ,
rand: 8145/1000 = 8.145 *10 = 81.45      (c1,c2,c3):  ,  ,
rand: 3281/1000 = 3.281 *10 = 32.81      (c1,c2,c3):  ,  ,
rand: 6827/1000 = 6.827 *10 = 68.27      (c1,c2,c3):  ,  ,
rand: 9961/1000 = 9.961 *10 = 99.61      (c1,c2,c3):  ,  ,
rand: 491/1000 = 0.491  *100 = 49.1      (c1,c2,c3):  ,  ,
rand: 2995/1000 = 2.995 *10 = 29.95      (c1,c2,c3):  ,  ,
rand: 1942/1000 = 1.942 *10 = 19.42      (c1,c2,c3):  ,  ,
rand: 4827/1000 = 4.827 *10 = 48.27      (c1,c2,c3):  ,  ,
rand: 5436/1000 = 5.436 *10 = 54.36      (c1,c2,c3):  ,  ,
rand: 2391/1000 = 2.391 *10 = 23.91      (c1,c2,c3):  ,  ,
rand: 4604/1000 = 4.604 *10 = 46.04      (c1,c2,c3):  ,  ,
rand: 3902/1000 = 3.902 *10 = 39.02      (c1,c2,c3):  ,  ,
rand: 153/1000 = 0.153  *100 = 15.3      (c1,c2,c3):  ,  ,
rand: 292/1000 = 0.292  *100 = 29.2      (c1,c2,c3):  ,  ,
rand: 2382/1000 = 2.382 *10 = 23.82      (c1,c2,c3):  ,  ,
rand: 7421/1000 = 7.421 *10 = 74.21      (c1,c2,c3):  ,  ,
rand: 8716/1000 = 8.716 *10 = 87.16      (c1,c2,c3):  ,  ,
rand: 9718/1000 = 9.718 *10 = 97.18      (c1,c2,c3):  ,  ,
rand: 9895/1000 = 9.895 *10 = 98.95      (c1,c2,c3):  ,  ,
rand: 5447/1000 = 5.447 *10 = 54.47      (c1,c2,c3):  ,  ,
rand: 1726/1000 = 1.726 *10 = 17.26      (c1,c2,c3):  ,  ,
rand: 4771/1000 = 4.771 *10 = 47.71      (c1,c2,c3):  ,  ,
rand: 1538/1000 = 1.538 *10 = 15.38      (c1,c2,c3):  ,  ,
rand: 1869/1000 = 1.869 *10 = 18.69      (c1,c2,c3):  ,  ,
rand: 9912/1000 = 9.912 *10 = 99.12      (c1,c2,c3):  ,  ,
rand: 5667/1000 = 5.667 *10 = 56.67      (c1,c2,c3):  ,  ,
rand: 6299/1000 = 6.299 *10 = 62.99      (c1,c2,c3):  ,  ,
rand: 7035/1000 = 7.035 *10 = 70.35      (c1,c2,c3):  ,  ,
rand: 9894/1000 = 9.894 *10 = 98.94      (c1,c2,c3):  ,  ,
rand: 8703/1000 = 8.703 *10 = 87.03      (c1,c2,c3):  ,  ,
rand: 3811/1000 = 3.811 *10 = 38.11      (c1,c2,c3):  ,  ,
rand: 1322/1000 = 1.322 *10 = 13.22      (c1,c2,c3):  ,  ,
rand: 333/1000 = 0.333  *100 = 33.3      (c1,c2,c3):  ,  ,
rand: 7673/1000 = 7.673 *10 = 76.73      (c1,c2,c3):  ,  ,
rand: 4664/1000 = 4.664 *10 = 46.64      (c1,c2,c3):  ,  ,
rand: 5141/1000 = 5.141 *10 = 51.41      (c1,c2,c3):  ,  ,
rand: 7711/1000 = 7.711 *10 = 77.11      (c1,c2,c3):  ,  ,
rand: 8253/1000 = 8.253 *10 = 82.53      (c1,c2,c3):  ,  ,
rand: 6868/1000 = 6.868 *10 = 68.68      (c1,c2,c3):  ,  ,
rand: 5547/1000 = 5.547 *10 = 55.47      (c1,c2,c3):  ,  ,
rand: 7644/1000 = 7.644 *10 = 76.44      (c1,c2,c3):  ,  ,
Press any key to continue . . .


Output while using the cout in the loops
rand: 41/1000 = 0.041   *100 = 4.1      c1: 250.9
rand: 8467/1000 = 8.467 *10 = 84.67     color2 = 170.33 color3 = 0
rand: 6334/1000 = 6.334 *10 = 63.34     color2 = 191.66 color3 = 0
rand: 6500/1000 = 6.5   *10 = 65        color2 = 190    color3 = 0
rand: 9169/1000 = 9.169 *10 = 91.69     color2 = 163.31 color3 = 0
rand: 5724/1000 = 5.724 *10 = 57.24     color2 = 197.76 color3 = 0
rand: 1478/1000 = 1.478 *10 = 14.78     color2 = 255    color3 = 70.44
rand: 9358/1000 = 9.358 *10 = 93.58     color2 = 161.42 color3 = 0
rand: 6962/1000 = 6.962 *10 = 69.62     color2 = 185.38 color3 = 0
rand: 4464/1000 = 4.464 *10 = 44.64     color2 = 255    color3 = 10.72
rand: 5705/1000 = 5.705 *10 = 57.05     color2 = 197.95 color3 = 0
rand: 8145/1000 = 8.145 *10 = 81.45     color2 = 173.55 color3 = 0
rand: 3281/1000 = 3.281 *10 = 32.81     color2 = 255    color3 = 34.38
rand: 6827/1000 = 6.827 *10 = 68.27     color2 = 186.73 color3 = 0
rand: 9961/1000 = 9.961 *10 = 99.61     color2 = 155.39 color3 = 0
rand: 491/1000 = 0.491  *100 = 49.1     c1: 205.9
rand: 2995/1000 = 2.995 *10 = 29.95     color2 = 255    color3 = 40.1
rand: 1942/1000 = 1.942 *10 = 19.42     color2 = 255    color3 = 61.16
rand: 4827/1000 = 4.827 *10 = 48.27     color2 = 255    color3 = 3.46
rand: 5436/1000 = 5.436 *10 = 54.36     color2 = 200.64 color3 = 0
rand: 2391/1000 = 2.391 *10 = 23.91     color2 = 255    color3 = 52.18
rand: 4604/1000 = 4.604 *10 = 46.04     color2 = 255    color3 = 7.92
rand: 3902/1000 = 3.902 *10 = 39.02     color2 = 255    color3 = 21.96
rand: 153/1000 = 0.153  *100 = 15.3     c1: 239.7
rand: 292/1000 = 0.292  *100 = 29.2     c1: 225.8
rand: 2382/1000 = 2.382 *10 = 23.82     color2 = 255    color3 = 52.36
rand: 7421/1000 = 7.421 *10 = 74.21     color2 = 180.79 color3 = 0
rand: 8716/1000 = 8.716 *10 = 87.16     color2 = 167.84 color3 = 0
rand: 9718/1000 = 9.718 *10 = 97.18     color2 = 157.82 color3 = 0
rand: 9895/1000 = 9.895 *10 = 98.95     color2 = 156.05 color3 = 0
rand: 5447/1000 = 5.447 *10 = 54.47     color2 = 200.53 color3 = 0
rand: 1726/1000 = 1.726 *10 = 17.26     color2 = 255    color3 = 65.48
rand: 4771/1000 = 4.771 *10 = 47.71     color2 = 255    color3 = 4.58
rand: 1538/1000 = 1.538 *10 = 15.38     color2 = 255    color3 = 69.24
rand: 1869/1000 = 1.869 *10 = 18.69     color2 = 255    color3 = 62.62
rand: 9912/1000 = 9.912 *10 = 99.12     color2 = 155.88 color3 = 0
rand: 5667/1000 = 5.667 *10 = 56.67     color2 = 198.33 color3 = 0
rand: 6299/1000 = 6.299 *10 = 62.99     color2 = 192.01 color3 = 0
rand: 7035/1000 = 7.035 *10 = 70.35     color2 = 184.65 color3 = 0
rand: 9894/1000 = 9.894 *10 = 98.94     color2 = 156.06 color3 = 0
rand: 8703/1000 = 8.703 *10 = 87.03     color2 = 167.97 color3 = 0
rand: 3811/1000 = 3.811 *10 = 38.11     color2 = 255    color3 = 23.78
rand: 1322/1000 = 1.322 *10 = 13.22     color2 = 255    color3 = 73.56
rand: 333/1000 = 0.333  *100 = 33.3     c1: 221.7
rand: 7673/1000 = 7.673 *10 = 76.73     color2 = 178.27 color3 = 0
rand: 4664/1000 = 4.664 *10 = 46.64     color2 = 255    color3 = 6.72
rand: 5141/1000 = 5.141 *10 = 51.41     color2 = 203.59 color3 = 0
rand: 7711/1000 = 7.711 *10 = 77.11     color2 = 177.89 color3 = 0
rand: 8253/1000 = 8.253 *10 = 82.53     color2 = 172.47 color3 = 0
rand: 6868/1000 = 6.868 *10 = 68.68     color2 = 186.32 color3 = 0
rand: 5547/1000 = 5.547 *10 = 55.47     color2 = 199.53 color3 = 0
rand: 7644/1000 = 7.644 *10 = 76.44     color2 = 178.56 color3 = 0
Press any key to continue . . .


any help appreciated ^^

~Rii
Your function getnum creates its own local variables named c1, c2 and c3, which are not even of the same type as the ones you pass in.

1
2
3
4
double c1 = 255 - result;
...
double c2 = 255;
double c3 = 0;


In function void getnum(double &result, unsigned char &c1, unsigned char &c2, unsigned char &c3)

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
void getnum(double &result, unsigned char &c1, unsigned char &c2, unsigned char &c3)
{
	result = rand()%10000;
	cout << "rand: " << result;
	result = result / 1000;
	cout << "/1000 = " << result << "\t";
	
	if (result < 1)
	{
		result = result * 100;
		cout << "*100 = " << result;
		/*rounding*/if (result > 100) result = 100;
		double c1 = 255 - result;
		cout << "\tc1: " << c1;
	}
	
	else if (result > 1)
	{
		double c2 = 255;
		double c3 = 0;
		result = result * 10;
		cout << "*10 = " << result;
		/*rounding*/if (result > 100) result = 100;
		if (result > 50)
		{
			c2 = 255 - result;
		}
		else
		{
			c3 = 100 - result*2;
		}
		cout << "\tcolor2 = " << c2 << "\tcolor3 = " << c3;
	} 
	else 
	{
		c1=177;c2=177;c3=177; 
		cout << "c1,2,3:" << c1 << "," << c2 << "," << c3;
	}
	//cout << "\t (c1,c2,c3): " << c1 << ", " << c2 << ", " << c3 << endl;
cout << endl;
}


you use original c1, c2, and c3 only in the code block

1
2
3
4
5
	else 
	{
		c1=177;c2=177;c3=177; 
		cout << "c1,2,3:" << c1 << "," << c2 << "," << c3;
	}


In all other code blocks of if statements you use local variables with the same name as for example

1
2
		double c2 = 255;
		double c3 = 0;
If you want to print the numerical value of the char variables you have to cast them before outputting.
cout << "\t (c1,c2,c3): " << static_cast<int>(c1) << ", " << static_cast<int>(c2) << ", " << static_cast<int>(c3) << endl;
thanks all! it works like a charm now, sorry for the late responce, I was a bit occupied.
Topic archived. No new replies allowed.