Move Cursor to End of Text

I'm an absolute beginner and am trying to get some practice, so I made this little program to convert decimal into a binary number. The problem is that it when the binary number is being written, it writes it backwards due to the way the formula works. So I just need the cout to be written from the back of the number.

So when the decimal 16 is entered into nBin it will print as "0 0001" instead of
"1 0000"

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>
using namespace std;

int main()
{	
	for(int nX = 0; nX == 0; nX = 0)
	{
		cout << "Enter a decimal number to convert to binary." << endl;
		cin >> nX;
		int nCounter = 0;
		cout << "The binary number for " << nX << " is: ";
		
		for( ;nX > 0; nX /= 2)
		{
			int nBin = nX % 2;
			cout << nBin;
			nCounter++;
		
			for( ;nCounter == 4; nCounter = 0)
			cout << " ";	
		}

		cout << "\n\n---------------------------------------------------\n" << endl;
	}
		
return 0;	
}


Thanks. x.o
Last edited on
Write it in some buffer first and then output from the end. Alternatively use approach which calculates binary digits starting from high bit.
I've already did this using an alternative way, I was just looking to try a different formula.

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
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;

int main()
{	
	for(double nX = 0; nX == 0; nX = 0)
	{
		cout << "Enter a decimal to convert to binary." << endl;
		cin >> nX;
		double nY = 134217728;
		bool bOne = false;
		int nCounter = 0;
		cout << "The binary number for " << nX << " is: ";
		
		for( ;nY >= 1; nY /=2)
		{
			if(nX >= nY)
			{
		 	cout << "1"; 
			nX -= nY; 
			bOne = true;
			nCounter = ++nCounter;
			}
			else
				{ 
				if(bOne == true)
					{
					cout << "0";
					nCounter = ++nCounter;
					}
				}
			for( ;nCounter == 4; nCounter = 0)
			cout << " ";	
		}

		cout << "\n\n---------------------------------------------------\n" << endl;
	}
		
return 0;	
}


This was the previous version using a different formula, but it was long and I was just practising on being efficient.

I'll try figure out the buffer thing, I am still working my way through the learning phase.

Thanks. o.x
Well, I guess learning phase shouldn't be a phase ^^; I meant I am still doing the tutorials on C++ basics.
Is there a way to press the "End" button on the keyboard before cout << "1";
or "0" so that the cursor is at the end?
You can use char array or std::string to hold data. Then you can output it. Like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main()
{
    std::cout << "Enter a decimal number to convert to binary.\n";
    int nX;
    std::cin >> nX;
    int nCounter = 0;
    std::cout << "The binary number for " << nX << " is: ";
    char buffer[33] = { 0 };
    for( ; nX > 0; nX /= 2)
         //Store next digit
        buffer[nCounter++] = (nX % 2 == 0) ? '0' : '1';


    while (nCounter --> 0) {
        //output digit
        std::cout << buffer[nCounter];
        //Output separator if nessesary
        if (nCounter % 4 == 0)   std::cout << ' ';
    }
    std::cout << "\n\n---------------------------------------------------\n\n";
}
Enter a decimal number to convert to binary.
33
The binary number for 33 is: 10 0001

---------------------------------------------------


Edit:
Is there a way to press the "End" button on the keyboard
There is, but it would do nothing. As there is no "console" or "window" in pure C++
Last edited on
Thank you MiiNiPaa, I'll have to study this for a while x.x
Topic archived. No new replies allowed.