Can anyone please help me reverse the output. This is just a very simple code and the output needs to be reversed in order for it to be correct.

#include <iostream>
#include "stdafx.h"

using std::cout;
using std::endl;

int _tmain(int argc, _TCHAR* argv[])
{
	int num = 0;
	int bin = 0;
	cout << "Please enter a number to be converted to binary:";
	std::cin >> num;
	
	while (num != 0)
	{
		bin = num%2;
		cout << bin;
		num = num/2;
	}

	cout << endl;
		system("PAUSE");
		return 0;
}

BTW, i tried the search - i couldnt understand anything. Im sorry if im too much of a bother please bear with me.

Simplest solution: store the result values in an array, then traverse the array backward.

I get tired of saying it: why does anyone use [B]system("PAUSE");[/B] ??

Isn't [B]cin.get()[/B] good enough? Is it maybe too fast ??

BTW, Take a look at my signature as well!
Edit:: And at this :)

Yes you store it in an array and than run a loop which display the data in the array in the reverse order.(Its a c++ code)
Just like This:

// declaretion of array
int array[];
int count=0;
// store the value of bin in the above declared array
bin = num%2;
array[count] = bin;
count++;

// display in reverse order
for ( int i = count; i >= 1; i-- ) {
     cout << " " << array[i];

}//for

I hope you enjoy it.I solve it but #include "stdafx.h" is missing.

commented: Post using code tags, read the forum rules: we don't give free code on this forum, and the answer has already been given by Narue :angry: !! -1
commented: *sigh* -2

>why does anyone use system("PAUSE"); ??
Because it gives you a pretty message? Because teachers teach it? Because beginners don't, and shouldn't be expected to, know the pitfalls of it?

commented: Bad teaching practice then :P +7

>...but #include "stdafx.h" is missing
Please note that it's not needed for non-Microsoft compilers :)

>>...but #include "stdafx.h" is missing
>Please note that it's not needed for non-Microsoft compilers
It's not needed for Microsoft compilers either. :icon_rolleyes: I hate how they use that as a default include for generated code. That's one of the reasons I always create an empty project with Visual Studio instead of using the templates.

>>...but #include "stdafx.h" is missing
>Please note that it's not needed for non-Microsoft compilers
It's not needed for Microsoft compilers either. :icon_rolleyes: I hate how they use that as a default include for generated code. That's one of the reasons I always create an empty project with Visual Studio instead of using the templates.

I do the same thing, I only ever use stdafx.h when including large header files to minimize build time, like when I had a generated header file with about 60000 constant c-strings in it (I was making a scrabble solver and didn't want any loose files :P).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.