[Help] When come to converting string to ASCII code

I know the method of how to convert an array of string to ASCII code but it does not read the rest of the words after spaces between the string. For example i enter the message" Hi how are you". When it convert to ASCII code, it only convert till "Hi" and display the code and rest is lost, and the worst is the output terminate immediately when it does not convert the rest of the message.
So far I have done this:
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
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

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

main()
{
      char message[30];
      int ascii[30];
      int x = 0;
      
      cout<<" TRANSMITTER "<<endl;
      cout<<" =========== "<<endl;
      cout<<" Enter message: ";
      cin>>message;

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

so far I'm not sure the parity does the checking as everytime it only display 0 when i enter different message. Thanks for helps in advance ;)
cin >> message; only reads the input up to the first whitespace (space, tab or new line.

Try cin.getline(message, 30); instead.

Though I would prob define the buffer size using a const

1
2
3
4
5
6
7
8
9
10
      ...
      const int buffer_size = 256; // a bit bigger!
      char message[buffer_size];
      int ascii[buffer_size];

      ...

      cin.getline(message, buffer_size);

      ...


And your parity() function is using data without first initializing it (to x? to zero?)

Should it be something line (I' not sure what it's suppose to do...)

1
2
3
4
5
6
7
8
9
10
int parity(int x)
{
    unsigned int data = 0;
    data ^= (x >> 1);
    data ^= (x >> 2);
    data ^= (x >> 4);
    data ^= (x >> 8);
    data ^= (x >> 16);
    return (data & 1);
}


Andy

P.S. I don't see a def of iss, so don't see how sub is being set?

And in C++, main should return an int.

1
2
3
4
5
6
7
8
...

int main()
{
    ...

    return 0;
}

Last edited on
Ok. I get to convert all the messages into ASCII code, but it convert the space as well. How do i display only the words without the space as well?
 TRANSMITTER
 ===========
 Enter message: hi how are you
 The ascii code for the message h  : 104
 The ascii code for the message i  : 105
 The ascii code for the message    : 32
 The ascii code for the message h  : 104
 The ascii code for the message o  : 111
 The ascii code for the message w  : 119
 The ascii code for the message    : 32
 The ascii code for the message a  : 97
 The ascii code for the message r  : 114
 The ascii code for the message e  : 101
 The ascii code for the message    : 32
 The ascii code for the message y  : 121
 The ascii code for the message o  : 111
 The ascii code for the message u  : 117

And why do you want to define such a big buffer size though I don't understand your point. Also, you mean the parity() function that i need to initialize it to x? I have tried like this
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>
using namespace std;

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]);
            cout<<" The ascii code for the message "<<message[x]<<' '<<" : "<<ascii[x]<<endl;
            x++;
            }
            cin.get();
            cin.ignore();
            cout<<"Parity check: "<< parity(ascii[x]);  
            return 0;
            }


@Noobprogrammer89

To not print the spaces, just check for them. If ascii[x] is not equal to 32, then print the line and value. Like so..
if ( ascii[x]!=32) This goes right below ascii[x] = int(message[x]);
parity-wise, I don't know what you need to init x to. But if you don't init to something, the result of the ^= operator will be meaningless as it uses the current value,

256 is not a large buffer for a normal Windows program. But 30 is tiny. Unless you're programming some miniscule embedded system!

By chance, "And why do you want to define" is 29 chars, which will just fit in your buffer along with a null terminator. I don't thinks its impossible that a user will type a longer sentence than that. I would prob use 1024, which is safely long enough than any normal sentence.
Topic archived. No new replies allowed.