@programnick I think you missed what I was saying.
My goal is to convert 011000110110000101110100 into an ascii vice versa. please advice thanks! |
"011000110110000101110100" is a string. the result in ASCII is "cat" which is also a string.
Therefore your functions need to have these prototypes (or similar)
1 2
|
string bin_to_ascii(string bin);
string ascii_to_bin(string ascii);
|
Also, your code is rather muddled:
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
|
while(true)
{
string choice;
string str;
cout<<"Do you want to encrypt or decrypt the message: ";
cin>>choice;
if(choice == "encrypt")
{
char ch;
cout<<"Enter the string to encrypt:";
cin>> choice;
choice = ascii_to_bin(ch);
}
else if(choice == "decrypt")
{
cout<<"Enter the string to decrypt:";
string bin;
cin>> choice;
choice = bin_to_ascii(bin);
}
else{ cout<<"Invalid Input\n\n"; }
cout<<"Repeat again(y or n)? ";
char ch;
cin>>ch;
if(ch != 'y' && ch != 'Y')
break;
}
|
At line 11,
cin>> choice;
you get the user input as a string. But what do you pass to the function? It isn't the user input, but something else, you pass a single uninitialised character ch.
That part of the code might look like this:
1 2 3 4 5
|
cout <<"Enter the string to encrypt:";
string str;
cin >> str;
string bin = ascii_to_bin(str);
cout << "binary = \n" << bin << '\n';
|
It would be better to keep the variable
choice
out of this part of the logic, it serves its purpose in handling the user choice, use some other variables for the ascii/binary strings.
Tackling a program like this is a matter of breaking the task down into smaller subtasks. In the main() function we are concerned only with strings. The individual characters can be handled by the other functions.
The other two functions I mentioned would be used at the lowest level of all. So we would have a hierarchy:
main() calls
bin_to_ascii() calls
str_to_char()
ascii_to_bin() calls
char_to_str() |
Also, it would help if you used code tags, then I could refer directly to line numbers in your code without having to requote it.
[code]your code here[/code]