1

I have written a program to take in a text message and convert it to binary. How do I get it to also convert the space (0010 0000) between letters?

int main(){

    cout << "Enter plaintext: " << endl;
    string plaintext;
    cin >> plaintext;
    
    for(int i=0; i < plaintext.length(); i++){
        bitset<8> plain(plaintext[i]);
        cout << plaintext[i] << plain << endl;
        plaintext[i] &= 0b11111111;
        bitset<8> 1st_enc(plaintext[i]);
        cout << 1st_enc << endl;
    }

    cout << "word:" << plaintext << endl;

    return 0;
}
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
rookie
  • 21
  • 1
  • Not sure exactly what you mean. Could you add a sample input and its desired output? Note: The default behaviour of `cin >> plaintext;` is to discard all whitespace. – user4581301 Oct 13 '21 at 22:18
  • 2
    It looks to me that your issue is not converting spaces that exist in `plaintext`. Rather, the issue is getting spaces to exist in `plaintext`, is it not? If you drop the idea of converting characters and focus on reading spaces, you might think to search SO for `[c++] read space cin` and find [How can I make std::cin read spaces?](https://stackoverflow.com/questions/38653733/) – JaMiT Oct 13 '21 at 22:54
  • I will try to get spaces exist in plaintext and see if that fixes it. – rookie Oct 14 '21 at 02:23
  • @rookieJ Saying "try" suggests the task might have any difficulty to it. You don't need anything complex for a test; just skip the user input and initialize `string plaintext = "This string has spaces in it.";`. This is one way to test each piece of your code in isolation. Another way is to output `plaintext` after reading it in to make sure that its value is what you think it is. Once you identify the broken piece, you can focus on fixing it. Once each piece works, you can combine them and test the result (which in this case might be your whole program). – JaMiT Oct 15 '21 at 00:36

0 Answers0