# Thread: My binary converter trigger question about vector, datatype, sizof op. etc...

1. ## My binary converter trigger question about vector, datatype, sizof op. etc...

That's incredible. I work out a binary converter which convert decimal value into binary.A lot of people has already done the same that before,but now I also has maneged to do it. I didn't think so far that this task can be coded in such a simply and brief way. How effective c++ sometimes can be! Here you are:

#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

vector<int>binary(1); int i, s=0 ;

vector<int> Binary(int dec);

int main(){

cout<<"Give a positive integer: ";

while(cin>>i){ cin.ignore(); Binary(i);
cout<<endl; i = binary.size();

while(i>0){ i -=1; s+=sizeof(binary[i]);
cout<<binary[i]<<" ";
} cout<<"\tThe size of your vector in byte: "<< s;

cout<<endl<<endl; binary.resize(1); s=0; main(); }

cin.get();

}

vector<int> Binary(int dec){ int l=1, m=0; bool flag = 0; binary[0]=0;

loop: while(l<dec){
l*=2; m++; if (flag==false && l<=dec) binary.push_back(0);

}

if(dec==0){binary[0]=0;}
else if(dec==1){binary[0]=1;}
else if(dec-l==0)binary[m]=1;
else{binary[m-1]=1; dec -= l/2; flag = true; l=1; m=0; goto loop;}

}

My binary converter trigger question about vector, datatypes, sizeof op. and their usage which may be worth to discuss.

However this solvation put up some brand new question to me as well.

The point is, the following behaviour of the code: If you replace the type of the data that binary vector contains with bool

type (allocated 1byte space) the program doesn't do the converting in the right way.
The following event happens to programme with vector of bool type:

1st input(any power of the number two): e.g. 16 1st output: 10000

2nd input(any number that isn't power of two): e.g. 7 2nd output: 000 (the number of digit is right)

Then the program goes well without any mistake.

Am I right when I come to the conclusion that vector class is a description (designe) of such data containers that don't

work with bool datatype properly, Maybe, there are defined flag bits which needs to mark the beginning or the end of the

allocated memory region, for example.

The another event which not cause problem just simply new for me. The behavour of the sizeof operator:

In the case of code with vector of integer type (allocated 4 byte space for each element) : e.g.:

1st input: 3 1st output: 11 The size of your vector in byte 8
2nd input: 16 2nd output: 10000 The size of your vector in byte 20

In the case of code with vector of bool type (allocated 1 byte space for each element): e.g.:

1st input: 3 1st output: 11 The size of your vector in bit 16
2nd input: 16 2nd output: 10000 The size of your vector in bit 40

Yes, you can see well, you must overwrite even the text part "in byte" as I think sizeof operator, in case of bool type,

calculat tha space of mamory not byte by byte but bit by bit. That's the curious happening suprises me.

2. vector<bool> is a special case of vector that uses one bit per bool. If you need a container type with usable Boolean values, you're supposed to use a deque<bool>. vector is different from deque in terms of how it manages memory, and deque offers push_front() and pop_front(), but beyond that, they offer a similar container.

I think you need to read this: << !! Posting Code? Read this First !! >> and http://xkcd.com/292/, specifically, the panels on goto. I don't think it has anything on calling main(), but you're not supposed to do that either. It's unnecessary. If you need to repeat the program, you can put everything in a simple loop and decide when to exit main().

3. ## Here is the code!

Code:
```#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

vector<int>binary(1); int i, s=0 ;

vector<int> Binary(int dec);

int main(){

cout<<"Give a positive integer: ";

while(cin>>i){ cin.ignore();  Binary(i);
cout<<endl; i = binary.size();

while(i>0){ i -=1; s+=sizeof(binary[i]);
cout<<binary[i]<<" ";
} cout<<"\tThe size of your vector in byte: "<< s;

cout<<endl<<endl; binary.resize(1); s=0; main(); }

cin.get();

}

vector<int> Binary(int dec){ int l=1, m=0; bool flag = 0; binary[0]=0;

loop:                               while(l<dec){
l*=2; m++; if (flag==false && l<=dec) binary.push_back(0);
}

if(dec==0){binary[0]=0;}
else if(dec==1){binary[0]=1;}
else if(dec-l==0)binary[m]=1;
else{binary[m-1]=1; dec -= l/2; flag = true; l=1; m=0; goto loop;}

}```
I hope it is sufficient.

4. Turn your compiler warnings on and pay attention to them. That code is broken in a bad way.

Don't use 'l' as a variable, it looks too similar to '1'.

You don't need a vector for this. Instead of storing the ones and zeros you can just output them. Make sure you start from the right end of course.

5. You seriously need to indent better.

6. My poor English don't let me understand the last remark. Can you explain me? (to indent or to intend) though it doesn't matter, wheter one or other do not make any sense for me.

7. Ok! But how I understand the whole statment?

8. Hm. I still haven't got it. Help me please I don't wanna open a new thread for that.

9. Be polite, if you can.

10. OK. I see it. So you have problem with my style.

My answer is: I am a hobbiest and hardly have enough chance to become a proffessional coder I am a mechanical engineer, and inspite of this fact I am glad to have a job at all, and can get a little time to write piece of work like that.

I am interesting in any constructive opinion. Style is important, but as you can see, I don't know even how c++ work. I think, style can wait. :P

11. You need to appreciate how bad this code looks. Look at how many tabs you put in here.
Attachment 10398
Can you tell me without looking for curly braces or other hints what statements belong inside the while loops? Where does the first loop end? Anyway, the point is: To properly indent, you're supposed to line up code against a margin, and the code isn't indented until you could understand what is inside of what without the help of curly braces. People aren't going to help if you continue to write code no one can read.

12. Uhm. Just a qustion. I use dev c++. Could you run it with cod blocks?

You know, this work is pasted into txt format before copy to here, then try your suggested method "". That series of actions could caused some reformating. I will look after that.
I usually intend to write clear, understandable codes.

13. I get the following messages when I build.

-------------- Build: Debug in sandbox ---------------

Compiling: main.cpp
C:\Documents and Settings\Owner\My Documents\sandbox\main.cpp: In function 'int main()':
C:\Documents and Settings\Owner\My Documents\sandbox\main.cpp:34:14: warning: ISO C++ forbids taking address of function '::main'
C:\Documents and Settings\Owner\My Documents\sandbox\main.cpp: In function 'std::vector<int> Binary(int)':
C:\Documents and Settings\Owner\My Documents\sandbox\main.cpp:74:1: warning: no return statement in function returning non-void

Since I straightened the code out, things are different from yours, but line 34 refers to where you call main() and line 74 refers to the Binary function, where you return nothing, even though you said it returns a vector of ints.

I'm kinda scared to run it since you call freaking main() in main() and I may never get out but you lucked out on my machine. It works. Despite all the things you did.

I'm curious, did you not see how my earlier post addresses all of your questions and issues? If you don't understand, I'm here to clarify myself.

14. I don't know what teachers do, but it's not the first time I can see recursive main() calls.