well here is my problem i had a task to transfrom sequence of decimals entered by user to binary so i did it but here comes my problem it turn out that i MUST have used at least 2 functions 1 main for input output and 1 which contains the code that solves the problem but this code is a if else statement in a loop which is in another loop and i just dont know how realize that in another function so it would use the input from the main and then does the transformation and then comes back to main again and prints the result from this other function ... so i need help ASAP because my deadline is soon, hope someone here would help me so the code :


 using namespace std;
   int main()

   int dec, len;
   string bin;
       cout << "Please enter a decimal number(if you want to exit the      program enter 0 value) : " << endl;
while(cin >> dec)
    if (dec==0) break;
        bin = "";
           while (dec != 0)
               if (dec % 2 == 0)
                        bin.insert(0, "0");
               bin.insert(0, "1");
               dec = dec / 2;
     cout << "The equivalent binary number is: " << bin << endl << endl;

P.S. and the worst is that they gave us this task before explaining the functions :S

Edited by __avd: Added [code] tags.

6 Years
Discussion Span
Last Post by jonsca

P.S. that len integer is a mistake a just forgot to remove it so ingnore it please


and the worst is that they gave us this task before explaining the functions

I'm sure there are on the order of 10^23 pages online about functions and you probably have a textbook.

This is my interpretation:

string dec2bin(int decimal); //prototype

int main()

  //input a number "num" here
  //pass num to dec2bin
  //print out answer

string dec2bin(int decimal)
  //do your input calculations and your string manipulation
  //return the string to main

Votes + Comments
I did't knew it :)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.