Take first 12 bits of a number if longer than 12 I need to only take first 12 bits of a number if its longer than a 12 bit number and convert it to a decimal number. So if 7000 is entered than it will return 3500. I also print out the binary form of the new number to check if its converting and far as I can tell it is.

I am wondering if there is a better way to write this code?

#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;
#include &lt;stack&gt; 
using namespace std;


int main()
{
    int num, total = 0, x = 0, sizeOfStack, foo[12], i = 0;
    stack &lt;int&gt;  s; // You need the stack;
    stringstream ss;
    unsigned long long int binaryNumber;

    cout &lt;&lt; "Please enter a decimal: ";
    cin &gt;&gt; num;

    while (num &gt; 0)
    {
        total = num % 2;
        num /= 2;
        s.push(total); // push 'total'(?) onto the stack
                       //cout &lt;&lt; total;
    }
    sizeOfStack = s.size();
    while (x &lt; 12 &amp;&amp; x &lt; sizeOfStack) // Another loop to get the values from the stack / now in the correct order
    {
        if (!s.empty())
        {
            foo[i] = s.top();
            s.pop();
        }
        x++; // increment while loop
        i++; //increment array

    }
    for (int w : foo)
    {
        ss &lt;&lt; w; // Converting an Integer Array into a Number 
    }
    string some = ss.str();
    const char *p = some.c_str();
    binaryNumber = strtoull(p, NULL, 10);
    cout &lt;&lt; "The Binary Number is: " &lt;&lt; binaryNumber &lt;&lt; endl;
    unsigned long long int  dec = 0, rem, num2, base = 1;
    num2 = binaryNumber; //set binarynum2ber to num2
    while (num2 &gt; 0)
    {
        rem = num2 % 10;
        dec = dec + rem * base;
        base = base * 2;
        num2 = num2 / 10;
    }
    cout &lt;&lt; "The Decimal Number is: " &lt;&lt; dec &lt;&lt; endl;
    cout &lt;&lt; endl;
    return 0;
}
There is a lot of room for improvement here, but first, let us talk about code style and best practices.


using namespace std; is not a good practice, because it can make your code harder to read if you are using a lot of standard functionality and introduce hard-to-spot name resolution problems. While that is less likely to be an issue in short programs, I still advise you to not use it (creating good habits is worth a lot in the long run!).
Don't do too much on a single line! In particular, don't int num, total = 0, x = 0, sizeOfStack, foo[12], i = 0;, because, on the one hand, you are likely to forget an initializer or a part of a type here and, in the worst case, end up with undefined behavior, and, on the other hand, it is just hard to read for everybody not familiar with your code (such as I). Please split that line up into multiple definitions and declarations. Also, ...
Initialize and use variables as locally as possible. This not only prevents scope pollution, but also increases the readability of your code. As a reviewer, I don't want to (and even cannot) keep a mental map on more than a handful of variables, especially if I have to remember them through large blocks of code where they don't even appear once. 
To my understanding at least, the term decimal implies floating point numbers, not just integers. Please think about clarifying, e.g. by writing "decimal integers" or something alike.
Your code does (probably) unexpected thing for numbers smaller than 0. Since you take input as an int and also do virtually no error checking, you could end up with the user inputting a negative number, for which your code will output 0 for the binary and 0 for the decimal representation. If you don't want to accept negative numbers at all, num should be unsigned. If you do, change your implementation to work correctly for those. Either way, you should do input verification on the numbers (or non-numbers) you get.
Don't use std::endl. If you want a newline, simply write '\n'. The reason is that std::endl does more than the name suggests. In particular it writes a '\n' and flushes the underlying buffer which is very rarely what you want or need to do because it seldom yields any benefits and can decrease performance.
Order your includes. In general, alphabetic ordering is expected to enable fast header verification for people reading your code. 
Write meaningful comments. In particular, follow the DRY principle in comments as well as code, which means that s.push(total); // push 'total'(?) onto the stack, for example, should go without the comment because it only repeats what the code does. Instead, try to us comments to give reasons as to why you're doing something, and don't overuse them because you've read somewhere that "the more comments, the better". Usefulness and clarity are key.
foo is not exactly what I would call a good variable name. Most of your other names are good, but this one identifier is definitely not. How about something along the lines of binaryDigits instead?
If you have access to anything &gt;=C++11, use nullptr in place of NULL because NULL is fragile and convertible to a lot of things that are not actually pointers.




You asked 


  I am wondering if there is a better way to write this code?


and the answer to that is yes. The main idea of your current implementation is to use a stack with your numbers binary digits, which is wasteful (and probably slow) in comparison to just doing the operation in place using bitshifting. 

The algorithm that I would propose works like this:

1. Find the highest set bit (= most significant set bit/mssb) in the input
2. Shift the number right so that the mssb is in 12th position


which should be simple enough to implement. Let's walk this through step by step: 


There are different algorithms for finding the most significant bit, but usually an iterative approach works well enough. The general idea is to iterate the number bitwise from the most significant to the least significant bit, checking whether the current bit is set. If it is, return the current bit position. In code, this might look like this: 

static unsigned find_mssb(unsigned const num) {

    unsigned const num_bit_count = sizeof num * CHAR_BIT;

    unsigned flag = 1u &lt;&lt; (num_bit_count - 1u);

    //num_bit_count is also the index of the msb
    unsigned index = num_bit_count;
    while (index) {

        if (num &amp; flag) {
            return index;
        }

        flag &gt;&gt;= 1u;
        --index;
    }

    return 0u;
}


It is important to note that the least significant byte has the index 1, not 0, because we reserve 0 for the occasion that no bit is actually set. Alternatively, if you use either gcc or clang, there is the builtin __builtin_clz which returns the number of leading zeroes, greatly simplifying the computation: 

static unsigned find_mssb(unsigned const num) {

    unsigned const num_bit_count = sizeof num * CHAR_BIT;

    //__builtin_clz(0u) is undefined behavior
    if (num) {
        unsigned leading_zeroes = __builtin_clz(num);
        return num_bit_count - leading_zeroes;
    }

    return 0u;
}


Now that we have a way to find the most significant set bit, let's move on to step 
Shifting the number right so that the most significant set bit is in the 12th position. This is trivial to do. In code:

unsigned mssb = find_mssb(number);

if (mssb &lt; 12u) {
  //error message, exit, ...
}

number &gt;&gt;= mssb - 12u;


And we're done. 


Putting all of this (and some bits of supporting code) together, a reworked version of your program could look something like this: 

#include &lt;algorithm&gt;
#include &lt;climits&gt;
#include &lt;iostream&gt;
#include &lt;limits&gt;
#include &lt;sstream&gt;

static unsigned find_mssb(unsigned const num) {

    unsigned const num_bit_count = sizeof num * CHAR_BIT;

#ifdef __GNUG__

    if (num) {
        unsigned leading_zeroes = __builtin_clz(num);
        return num_bit_count - leading_zeroes;
    }

#else
    unsigned flag = 1u &lt;&lt; (num_bit_count - 1);

    unsigned index = num_bit_count;
    while (index) {

        if (num &amp; flag) {
            return index;
        }

        flag &gt;&gt;= 1;
        --index;
    }

#endif

    return 0;
}

static std::string binary_representation(unsigned num) {

    std::ostringstream representation;

    while (num) {
        representation &lt;&lt; (num &amp; 0x1u);
        num &gt;&gt;= 1;
    }

    std::string str {representation.str()};
    std::reverse(str.begin(), str.end());

    return str;
}

int main() {

    std::cout &lt;&lt; "Please enter an integer\n&gt; ";
    unsigned number;
    std::cin &gt;&gt; number;

    unsigned mssb = find_mssb(number);

    if (mssb &lt; 12u) {
        std::cout &lt;&lt; "The number you entered is too short. Its binary"
            " representation is only " &lt;&lt; mssb &lt;&lt; " bits long, which is"
            " less than the required length of 12. In binary, your number is "
            &lt;&lt; binary_representation(number) &lt;&lt; ".\n";
        return 0;
    }

    number &gt;&gt;= mssb - 12u;
    std::cout &lt;&lt; "The trimmed number is "&lt;&lt; number
            &lt;&lt; ". The 12 most significant bits are "
            &lt;&lt; binary_representation(number) &lt;&lt; '\n';
}




Edit: As TobySpeight mentioned in the comments, the bit length of char is not dictated by the standard, so assuming each byte has eight bits is not portable. Instead, the standard library offers CHAR_BIT instead, which is now used in the updated code.