Hey guys!
Need some help in C++ coding for a program to convert decimals to binary code. This is what I have come up with and I hope you guys can modify it for me.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
int value;
cout << "Enter a number: "; cin >> value;
// I will need to call dec2bin here. Not sure how to do it.
cout << value << "is" << answer << "in binary.";
system ("Pause");
return 0;
}
int dec2bin(int value)
{
string answer = "";
while (value > 0)
{
answer = answer + (value % 2);
value = value / 2;
}
reverseStr (answer);
}
I understand that I'll need an algorithm to divide the given number by 2, keep the quotient and append the remainder to a string (str). Keep dividig the quotient by 2 and append the reamainder to the string until the quotient is 0. The answer will be in reverse so I'll need to reverse the string using reverseStr.
I need to have one function called dec2bin which is already there but needs modification. This function does not write anything to the screen but returns a string which I need it to be string dec2bin (int num)
Progress of the coding should be: Get input from user in the main function, call dec2bin function and display the result.
Hope you guys can help me with this. If possible, please stick to the current code and algorithm as it is required for my exercise. Thanks everyone!
			
			Need some help in C++ coding for a program to convert decimals to binary code. This is what I have come up with and I hope you guys can modify it for me.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
int value;
cout << "Enter a number: "; cin >> value;
// I will need to call dec2bin here. Not sure how to do it.
cout << value << "is" << answer << "in binary.";
system ("Pause");
return 0;
}
int dec2bin(int value)
{
string answer = "";
while (value > 0)
{
answer = answer + (value % 2);
value = value / 2;
}
reverseStr (answer);
}
I understand that I'll need an algorithm to divide the given number by 2, keep the quotient and append the remainder to a string (str). Keep dividig the quotient by 2 and append the reamainder to the string until the quotient is 0. The answer will be in reverse so I'll need to reverse the string using reverseStr.
I need to have one function called dec2bin which is already there but needs modification. This function does not write anything to the screen but returns a string which I need it to be string dec2bin (int num)
Progress of the coding should be: Get input from user in the main function, call dec2bin function and display the result.
Hope you guys can help me with this. If possible, please stick to the current code and algorithm as it is required for my exercise. Thanks everyone!
 
				
		 
			 
 
		 
 
		 
 
		 
 
		 
 
		
 Facebook
Facebook Twitter
Twitter