Can't convert a string to binary

Hi guys, can you re-write my program where it's mistaken ? I can't figure out how to convert my input string into binary :/ I will input 2 programs:the first is my current, and the other is an example from which I should figure out how to do it but I can't and it's driving me crazy.Please use only the following directories : "iostream,string,math".

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
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
int main()
{
    char s[50];
    cin.getline(s,50);
    int bin[0];
    for(int k=0;k<strlen(s);k++)
    {
    int i=0;
    int p=(int)s;
    do
    {
        bin[i]=p%2;
        p=p/2;
        i++;
    }
    while (p!=0);
    for(int j=i-1;j>=0;j--)
    cout<<s[j]<<endl;
    }
    system("pause");
    return 0;
}



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
#include<iostream>
using namespace std;
void DecToBin(int x)
{
 int a[100];
 int i=0;
 do
 {
   a[i]=x%2;
   x=x/2;
   i++;
 }
 while(x!=0);
 for(int j=i-1;j>=0;j--)
 cout<<a[j];
 cout<<" ";
}
int main()
{
 for(int i=180;i<=210;i++)
 DecToBin(i);
 system("pause");
 return 0;
}

would have been a lot easier if we could use <bitset>.

But as we can't I have a doubt. What is the second code dong? And how are you using it as reference?
I've been told that the second code is going to help me figure it out how to write my first one.I tried doing it like the second one - it got even worse.And I know it's bad I can't use <bitset> but .. I think the whole do-while function is the key thing in the second code but I can't figure out how to use it in the first one .. I've been trying for days now and my brain is literally melting..fck's sake
Do you know what exactly is the second code is doing? It doesn't even take an input so I am guessing its converting a number (of base 10) to binary...
Yeah it's converting from decimal to binary.And from that I'm supposed to build my first code .. it's so fcked up dude .. so fcked up.. Please help if you have any ideas how can this happen.
Last edited on
I think the solution is by taking a string and then take its ascii value and then convert it to binary by the second code....

But I still didn't quite understand what number is the second code converting to binary :D

EDIT-
oh!!! got it it is converting the numbers (from 180 to 210) into binary..... now we just have to figure out the code :D
Last edited on
a decimal number to binary number.Well actually if you want don't use the second coding at all.Just make the first one work, I don't care how just make it work please .. and as I said no <bitset>, sadly.I'm going to take a shower, if you're willing to re-write it so it can work - take your time.(please do it since I'm desperate as fck)
alright will try my best :D
Last edited on
got it!

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
void DecToBin(int x)
{
 int a[100];
 int i=0;
 do
 {
	a[i]=x%2;
	x=x/2;
	i++;
 }
 while(x!=0);
 for(int j=i-1;j>=0;j--)
 cout<<a[j];
 cout<<" ";
}
int main()
{
 cout<<"Enter the string to convert to binary:: ";
 char i[50];
 gets(i);
 int j=strlen(i);
 for(int k=0;k<j;k++)//the loop will end only when all the stuff is converted to binary
 {
 int num=i[k];//converting char to its numerical ascii value
 DecToBin(num);//just calling the function in the loop
 }
 return 0;
}


Hope it helps...
Everything is perfect .. but .. can you change a bit the int main ? I mean there is coding in there that I haven't even studied and it would be a bit suspicious if I hand in something I haven't studied ..
1
2
3
4
5
6
7
 char i[50];
 gets(i);
 int j=strlen(i);
 for(int k=0;k<j;k++)
 {
 int num=i[k];
 DecToBin(num);

Here's a typical int main which I study :
https://www.dropbox.com/sc/xur7sbdum8lz9fs/AAC55CkzMMwVUJURLpGNKKxea
Don't worry.. Just say that you were bit of curious and you studied in advance :D Nobody would suspect any thing :D
I'm going to follow your lead then .. heahea.Thank you a lot dude.Since you're the only guy that helped me in a way I can hand it in can you help me with further programs ? I'm not saying something like : help me with every program from now on.. I'm just asking if you can help me with programs I literally have no idea how to deal with.
Of course.... :D Happy to help... Just post your problem on forum and pm me the link of your post, as many brains are better than one :D
Last edited on
btw figured out how to change it so it can look authentic as if I have written it (all of it)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
 cout<<"Enter the array which you want to convert into binary : "<<endl;
 char a[50];
 cin.getline(a,50);
 cout<<"The array which you wrote converted into binary looks like : "<<endl;
 for(int k=0;k<strlen(a);k++)
 {
 int n=a[k];
 DecToBin(n);
 }
 cout<<endl;
 system("pause");
 return 0;
}
nice...... :D
Topic archived. No new replies allowed.