hey, I am writing a small program that takes a decimal number ranging from 1 to 255 and converts it to bcd. Somewhere near the end of the program my output gets messed up. For example I need 255 to be converted to 0010 0101 0101.

Well, this being C++ why don't we take advantage of the STL and what it has to offer us. There are a couple of approaches to this, however lets keep things simple for now and just use std::vector and std::bitset. We can save the other advantages for later:

//go through each number in the string and convert to binary representationfor(int i=0;i<input.size();i++){
std::bitset<4>number(input[i]-'0');//e.g.,converts char '2' to integer 2
bcd.push_back(number); //add the binary representation to our vector
}//print out bcdfor(int j=0;j<bcd.size();j++){
std::cout<<bcd[j].to_string()<<" ";
}