| | |
decimal to binary in while loop..stuck
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 20
Reputation:
Solved Threads: 0
i am doing a decimal to binary conversion and i have figure out how to do the conversion i just don't know how to make it cout so that it is in reverse.... i was also told that it could done in a while loop
here is the code
here is the code
C++ Syntax (Toggle Plain Text)
1. #include<iostream> 2. using namespace std; 3. int main(){ 4. 5. int dec,olddec,remain; 6. 7. cout<<"please input a decimal to be converted to binary:"<<endl; 8. cin>>dec; 9. cout<<endl; 10. 11. while(dec>0){ 12. 13. olddec=dec; 14. 15. remain = dec % 2; 16. 17. dec /=2; 18. 19. cout<<olddec<<"/2 = "<<dec<<" "<<"remainder = "<<remain<<endl; 20. } 21. 22. return 0; 23. }
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
•
•
•
•
i am doing a decimal to binary conversion and i have figure out how to do the conversion i just don't know how to make it cout so that it is in reverse.... i was also told that it could done in a while loop
here is the code
C++ Syntax (Toggle Plain Text)
1. #include<iostream> 2. using namespace std; 3. int main(){ 4. 5. int dec,olddec,remain; 6. 7. cout<<"please input a decimal to be converted to binary:"<<endl; 8. cin>>dec; 9. cout<<endl; 10. 11. while(dec>0){ 12. 13. olddec=dec; 14. 15. remain = dec % 2; 16. 17. dec /=2; 18. 19. cout<<olddec<<"/2 = "<<dec<<" "<<"remainder = "<<remain<<endl; 20. } 21. 22. return 0; 23. }
Well it looks to me like you are calculating the digits in the reverse order that you want to display them. remainder is the digit. You can't just print remainder in the order that you calculate it because then the digits are in reverse order, and currently remainder is writing over itself. Currently you HAVE to write over remainder before displaying because you have no place to store the old remainder.
Solution: use an array that sets aside an integer for every digit. Since you are using integers and integers have 32 bits, you might as well make an array called digit that would hold 32 integers. So say the user typed in the number 18. In binary, that is:
10010
Working right to left, by the end of your calculation, you'd want your digit values to be this for the above number:
C++ Syntax (Toggle Plain Text)
digit[0] = 0 digit[1] = 1 digit[2] = 0 digit[3] = 0 digit[4] = 1
10010
Make sure you initialize all of the digits to 0 first or have some way of keeping track of the number of digits in the number.
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
I believe a common way to get around the reversal is to use a string to store the output:
C++ Syntax (Toggle Plain Text)
void binToString(int x){ string binStr; while(x > 0){ if(x & 1) // same as if(x % 2) binStr = "1" + binStr; else binStr = "0" + binStr; x >>= 1; // same as x = x / 2 } cout << binStr << endl; }
•
•
Join Date: Feb 2008
Posts: 20
Reputation:
Solved Threads: 0
ok i am really bad at strings since my professor barely explained how to use them so i am trying to avoid for this project i have to do till i can try it with simpler programs;
i tried an array but it does not seem to want to output the numbers when it run it gives a - number that are not in 0's and ones
i tried an array but it does not seem to want to output the numbers when it run it gives a - number that are not in 0's and ones
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; //int dec(int []); // for after i get program to run i want to turn it into function int main(){ int dec,olddec,remain,i,p[50]; cout<<"please input a decimal to be converted to binary:"<<endl; cin>>dec; cout<<endl; while(dec > 0){ i = 0; olddec = dec; remain = dec % 2; p[i]=remain; i++; dec /=2; cout<<olddec<<"/2 = "<<dec<<" "<<"remainder = "<<remain<<endl; } for (;i>=0;i--){ cout<<p[i]; } }
Last edited by Crazycfk; Mar 15th, 2008 at 3:28 pm. Reason: clarifying unclear statements
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
•
•
•
•
ok i am really bad at strings since my professor barely explained how to use them so i am trying to avoid for this project i have to do till i can try it with simpler programs;
i tried an array but it does not seem to want to output the numbers when it run it gives a - number that are not in 0's and ones
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; //int dec(int []); // for after i get program to run i want to turn it into function int main(){ int dec,olddec,remain,i,p[50]; cout<<"please input a decimal to be converted to binary:"<<endl; cin>>dec; cout<<endl; while(dec > 0){ i = 0; olddec = dec; remain = dec % 2; p[i]=remain; i++; dec /=2; cout<<olddec<<"/2 = "<<dec<<" "<<"remainder = "<<remain<<endl; } for (;i>=0;i--){ cout<<p[i]; } }
Two problems, both involving i. Problem 1, you are initializing i to 0 every trip through the loop in line 13. i needs to be initialized once, and before the loop. You are thus rewriting over old calculations. Two, i is too large when you get to your for-loop in line 27. If a number has 5 digits, you are going through that loop 6 times. You want to go through it 5 times, once per digit. The nonsense digits that you see at the beginning of the number is that extra integer that you do not want to display, but that you DO display in your program. You did not initialize your array to 0, so that memory location has not been touched by your program. If the number 45 was in there before the program started, that's what will be outputted, in addition to the REAL digits.
•
•
Join Date: Feb 2008
Posts: 20
Reputation:
Solved Threads: 0
Eureka...!!!!!!!!Thank you all for the help and the little bits of knowledge that i have gained due to your efforts......
this is the finished product... seems i was wrong if the input is 0 the number becomes a large - number
this is the finished product... seems i was wrong if the input is 0 the number becomes a large - number
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main(){ int dec,olddec,remain,i,p[50]; cout<<"please input a decimal to be converted to binary:"<<endl; cin>>dec; cout<<endl; i=0; while(dec > 0){ olddec = dec; remain = dec % 2; p[0+i]=remain; i++; dec /=2; cout<<olddec<<"/2 = "<<dec<<" "<<"remainder = "<<remain<<endl; } for (;i-1>=0;i--){ cout<<p[i-1]; } cout<<endl; return 0; }
Last edited by Crazycfk; Mar 16th, 2008 at 2:47 am. Reason: found problem within program so had to change the claim of it being finished
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
•
•
•
•
Eureka...!!!!!!!!Thank you all for the help and the little bits of knowledge that i have gained due to your efforts......
this is the finished product... seems i was wrong if the input is 0 the number becomes a large - number
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main(){ int dec,olddec,remain,i,p[50]; cout<<"please input a decimal to be converted to binary:"<<endl; cin>>dec; cout<<endl; i=0; if (dec == 0) // trivial case. Just display 0. cout << 0; else // start of else { while(dec > 0){ // start of while olddec = dec; remain = dec % 2; p[0+i]=remain; i++; dec /=2; cout<<olddec<<"/2 = "<<dec<<" "<<"remainder = "<<remain<<endl; } // end of while for (;i-1>=0;i--){ cout<<p[i-1]; } } // end of "else" cout<<endl; return 0; }
Not a huge problem. If the number is 0, there's nothing to calculate, so I would just short-circuit the problem with an if-else statement. If the number entered equals 0, just display 0. If it's larger than 0, go through your while loop. I added an if-else loop above. The bulk of your original code is in the "else" part.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: C and C++ help on struct arrays
- Next Thread: read text file into a structure
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






