Hi, so I have been trying for hours to figure out what to do to make this program work, but I am just not getting it. My assignment is to convert decimal to binary, octal, and hex. So far, I am still on binary because I have been hitting a problem. When I go to print the binary out, it's backwards. I cannot figure out how to reverse it. Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<iostream>
usingnamespace std;
#include "stack.h"
int main()
{
int num, total = 0;
cout << "Please enter a decimal: ";
cin >> num;
while(num > 0)
{
total = num % 2;
num /= 2;
cout << total;
}
cout << endl;
return 0;
}
I tried using a reverse function, only to realize that the sequence of numbers is not being stored as a string or integer, and that I do not know how to do that. So, if anybody could help me understand how to do this, that would be helpful.
Also, my professor gave me this class, but I have no idea how to implement it, or if I even need to for the binary portion:
#include<iostream>
usingnamespace std;
#include "stack.h"
int main()
{
int num, total = 0;
cout << "Please enter a decimal: ";
cin >> num;
stack s; // You need the stack;
while(num > 0)
{
total = num % 2;
num /= 2;
s.push(total); // push 'total'(?) onto the stack
//cout << total;
}
while(s.getCount() > 0) // Another loop to get the values from the stack / now in the correct order
{
cout << s.pop();
}
cout << endl;
return 0;
}
Oh wow. I hadn't thought to use the getCount method at all. >_< Thank you very much. Anyways, I have run into another problem. I have changed my program so that now I have this:
#include<iostream>
usingnamespace std;
#include "stack.h"
int main()
{
int decimal, remainder, choice;
stack s;
cout << "Please enter a decimal: ";
cin >> decimal;
cout << "Convert the number from decimal into:\n0 – Binary\n1 – Octal\n2 – Hexadecimal\n";
cin >> choice;
switch (choice)
{
case 0:
while (decimal > 0)
{
remainder = decimal % 2;
decimal /= 2;
s.push(remainder);
}
break;
case 1:
while (decimal > 0)
{
remainder = decimal % 8;
decimal /= 8;
s.push(remainder);
}
break;
case 2:
while (decimal > 0)
{
remainder = decimal % 16;
decimal /= 16;
if (remainder > 9)
cout << char('A' + remainder - 10);
else
cout << remainder;
s.push(remainder);
}
break;
default:
cout << "Invalid.";
}
while (s.getCount() > 0)
{
cout << s.pop();
}
return 0;
}
Binary and octal are both working fine, but I can't get hexadecimal to work. It comes out in reverse as well. I presume the problem comes from the fact that push doesn't work for the char data type, but I don't know how to resolve the issue. I need guidance here, please.
//other code here
while(hexStack.getSize() > 0)
{
//buffer being a previously declared int
buffer = hexStack.pop();
//if it buffer (last value popped) is greater then 9... enter the switch statement
//to assign it a letter, else just display the number(since it won't need a letter
//representation
if(buffer > 9)
{
switch(buffer)
{
case 10 : cout << "A"; break;
case 11 : cout << "B"; break;
case 12 : cout << "C"; break;
case 13 : cout << "D"; break;
case 14 : cout << "E"; break;
default : cout << "F"; break;
}
}
else
cout << buffer;
}
cout << " in hexadecimal\n";
cout << endl;
}
@georgewashere - I assume your switch mechanism is assuming that the number is guaranteed to be in the range 0 - 15, given you're using the switch's default case to handle 15 -> F. If you are making this assumption, I would prob tweak the code to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
while(hexStack.size() > 0)
{
buffer = hexStack.pop();
switch(buffer)
{
case 10 : cout << "A"; break;
case 11 : cout << "B"; break;
case 12 : cout << "C"; break;
case 13 : cout << "D"; break;
case 14 : cout << "E"; break;
case 15 : cout << "F"; break;
default : cout << buffer;
}
}
cout << " in hexadecimal\n";
cout << endl;
But if you do want to protect against values outside the range 0-15, then you need either (a) handle 15 as a specific case and use the default for out of range, and also handle underflow in the else. Or (b) use my version and protect the switch in an if(0 <= buffer && buffer <=15) test.
A more compact, pretty standard form of the trusting version is: