[Help] Function call and array

My main program is to convert a string of message to ASCII code, and then invoke a function call of binaryConvert() to convert the code to binary number. I'm not sure what goes wrong but the binary conversion seems messed up, though it's probably the logic error in the function.
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
#include <iostream>
#include <stdlib.h>
#include <string>
#include <math.h>
using namespace std;
void binaryConvert();

int binaryConvert(long convert)
{
    int a, b, num;
    int bit[] = {0,0,0,0,0,0,0,0};
    bit[1] = num%2;
    a = num-num%2;
    b = a/2;
    bit[2] = b%2;
    a = b-b%2;
    b = a/2;
    bit[3] = b%2;
    a = b-b%2;
    b = a/2;
    bit[4] = b%2;
    a = b-b%2;
    b = a/2;
    bit[5] = b%2;
    a = b-b%2;
    b = a/2;
    bit[6] = b%2;
    a = b-b%2;
    b = a/2;
    bit[7] = b%2;
    a = b-b%2;
    b = a/2;
    cout<<"Binary data: ";
    for(int i = 8; i > 0; i--)
    {
    cout<<bit[i];
}
}  

int parity(int x)
{
    unsigned int data;
    data = x;
    data ^= (data >> 1);
    data ^= (data >> 2);
    data ^= (data >> 4);
    data ^= (data >> 8);
    data ^= (data >> 16);
    return (data & 1);
} 

int main()
{
     
      const int buffer_size = 256;
      char message[buffer_size];
      int ascii[buffer_size];
      int send[buffer_size];
      int x = 0;
      
      cout<<" TRANSMITTER "<<endl;
      cout<<" =========== "<<endl;
      cout<<" Enter message: ";
      cin.getline(message, buffer_size);

      while(message[x] != '\0')   // while the string is not at the end
      {
            ascii[x] = int(message[x]);
            cout<<" The ascii code for the message " <<message[x]<<' '<<" : "<<ascii[x];
            cout<<"\n";
            x++;
            send[x] = ascii[x];
            }
            cout<<"Parity check: "<< parity(ascii[x]);
            cout<<"\n";
            cout<<binaryConvert(send[x]);  
            cin.get();
            cin.ignore();
            return 0;
            }
            

not sure about what's wrong with the array, hope to get helped ;)
hi,
check the comments...

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
#include <iostream>
#include <stdlib.h>
#include <string>
#include <math.h>
using namespace std;
void binaryConvert();  //what is this?

int binaryConvert(long convert)
{
    int a, b, num;
    int bit[] = {0,0,0,0,0,0,0,0};
    bit[1] = num%2;  //num is not initializd CAN'T DIVIDE WITH 0
    a = num-num%2; //num is not initializd
    b = a/2;
    bit[2] = b%2;
    a = b-b%2;
    b = a/2;
    bit[3] = b%2;
    a = b-b%2;
    b = a/2;
    bit[4] = b%2;
    a = b-b%2;
    b = a/2;
    bit[5] = b%2;
    a = b-b%2;
    b = a/2;
    bit[6] = b%2;
    a = b-b%2;
    b = a/2;
    bit[7] = b%2;
    a = b-b%2;
    b = a/2;
    cout<<"Binary data: ";
    for(int i = 8; i > 0; i--)
    {
    cout<<bit[i];
	}
	//FUNCTION MUST RETURN int VALUE (but it doesn't return anything)
}  

//rest of the code... 
@codekiddy
int a, b, num;
doesn't that initialize num?
@TheDudiful
1
2
3
   int a, b, num;
    int bit[] = {0,0,0,0,0,0,0,0};
    bit[1] = num%2;  //num is not initializd CAN'T DIVIDE WITH 0 


hi,
bit[1] = num%2; what result will be here?

EDIT:
a = num-num%2; //num is not initializd and what will be result here???

int binaryConvert(long convert) //why he used an argument which is not used in function at all??
Last edited on
hmm...
I guess you're right :)
@codekiddy
actually the code int binaryConvert(long convert) is suppose to be pass by array value from main() and the long convert suppose to be (int num), but i forgot to change it and I not sure how to pass array value, can you provide me some guidance?
I didn't test it but this may work for 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
void binaryConvert(int num) { 
int a, b;
    int bit[] = {0,0,0,0,0,0,0,0};
    bit[1] = num%2; 
    a = num-num%2;
    b = a/2;
    bit[2] = b%2;
    a = b-b%2;
    b = a/2;
    bit[3] = b%2;
    a = b-b%2;
    b = a/2;
    bit[4] = b%2;
    a = b-b%2;
    b = a/2;
    bit[5] = b%2;
    a = b-b%2;
    b = a/2;
    bit[6] = b%2;
    a = b-b%2;
    b = a/2;
    bit[7] = b%2;
    a = b-b%2;
    b = a/2;
    cout<<"Binary data: ";
    for(int i = 8; i > 0; i--)
    {
    cout<<bit[i];
    }
    return;
}
Last edited on
@codekiddy
ok i know the logic. But still it's not displaying what I want. The binary data output should be display 000101010(probably different each time run). BUt it display some other number : 445454895. Any idea of what's wrong?
@Noobprogrammer89

Remember, when you create an array, in your case bit[8], the values are 0 to 7. So when you try to access the eighth bit in your for loop, you are actually checking out of bounds of your array. Try for (int i = 7; i>=0;i--) and it should work correctly for you.
Works like a charm :) Thanks.. 1 more thing, while it converts to binary, it always display 0 value all the time eg: 00000000 , even i enter different messages. Is there something wrong?
@Noobprogrammer89

I'm not sure if you changed anything in your BinaryConvert() function, but the way it shows, it starts at position 1 instead of the beginning, position 0. Also, what is the Parity check for? Here is your program, displaying the ascii value, the binary value and lastly, the parity check.
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
// Convert String to ASCII.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <stdlib.h>
#include <string>
#include <math.h>

using namespace std;

//void binaryConvert();   Only needed if the function was placed AFTER main()

void binaryConvert(int num)
{
	int a, b;
	int bit[] = {0,0,0,0,0,0,0,0};
	bit[0] = num%2;
	a = num-num%2;
	b = a/2;
	bit[1] = b%2;
	a = b-b%2;
	b = a/2;
	bit[2] = b%2;
	a = b-b%2;
	b = a/2;
	bit[3] = b%2;
	a = b-b%2;
	b = a/2;
	bit[4] = b%2;
	a = b-b%2;
	b = a/2;
	bit[5] = b%2;
	a = b-b%2;
	b = a/2;
	bit[6] = b%2;
	a = b-b%2;
	b = a/2;
	bit[7] = b%2;

	cout<<"Binary data: ";
	for(int i = 7; i >= 0; i--)
	{
		cout<<bit[i];
	}
}  

int parity(int x)
{
	unsigned int data;
	data = x;
	data ^= (data >> 1);
	data ^= (data >> 2);
	data ^= (data >> 4);
	data ^= (data >> 8);
	data ^= (data >> 16);
	return (data & 1);
} 

int main()
{

	const int buffer_size = 256;
	char message[buffer_size];
	int ascii[buffer_size];
	int x = 0;

	cout<<" TRANSMITTER "<<endl;
	cout<<" =========== "<<endl;
	cout<<" Enter message: ";
	cin.getline(message, buffer_size);

	while(message[x] != '\0')   // while the string is not at the end
	{
		ascii[x] = int(message[x]);
		if ( ascii[x]!=32)
		{
		cout<<" The ascii code for " <<message[x] << " : "<<ascii[x]<< "  ";
		cout<<"\t";
		binaryConvert(ascii[x]);
		cout<<" Parity check: "<< parity(ascii[x]) << endl;
		}
		x++;
	}
	cout << endl <<"Press any key to continue.." << endl;
	cin.get();
	cin.ignore();
	return 0;
}
Last edited on
the program runs, but when i input a message and enter, it just hang and stopped working, any idea?
@Noobprogrammer89

I'm at a loss on why it hangs. I re-pasted the above code into my MS Visual C++ 2008 Express program, re-added the #include "stdafx.h" header, and it ran with no hitch. What IDE/compiler do you use? Also, why do you need to by-pass spaces?
Topic archived. No new replies allowed.