Converting digits..need help!

Reply

Join Date: Jan 2009
Posts: 5
Reputation: winry is an unknown quantity at this point 
Solved Threads: 0
winry winry is offline Offline
Newbie Poster

Converting digits..need help!

 
0
  #1
Jan 10th, 2009
Hi everyone!I've started to learn C++ couple of months ago and having some trouble with the code i'm trying the write.. I have to write a function that converts every digit of the parameter to zero,except 2.And number of digits is unknown..
I made this but its's output is always 0.Even i enter a number that contains 2...I'll be glad if you could help..Thanks

  1. #include<iostream>
  2. using namespace std;
  3.  
  4.  
  5. int convertNum(int n){
  6.  
  7. int digits=0;
  8. int remainder;
  9.  
  10.  
  11. for(int i=n;n!=0;n/=10)
  12. digits=digits+1;
  13.  
  14.  
  15.  
  16.  
  17. for(int j=1;j<=digits;j++){
  18. n=n/10;
  19. remainder=n%10;
  20. if(remainder==2)
  21. remainder=2;
  22. if(remainder!=2)
  23. remainder=0;
  24.  
  25. }
  26.  
  27. return remainder;
  28.  
  29.  
  30. }
  31.  
  32.  
  33.  
  34. int main()
  35. {
  36. int a;
  37.  
  38. cout<<"Please enter a positive integer"<<endl;
  39. cin>>a;
  40.  
  41. cout<<"The converted version of "<<a<<" is ----> "<<convertNum(a);
  42.  
  43.  
  44. return 0;
  45. }
Last edited by Ancient Dragon; Jan 10th, 2009 at 9:23 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Converting digits..need help!

 
0
  #2
Jan 10th, 2009
I'd be glad if you could read all the intro threads which describe how to post code.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,839
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 266
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Converting digits..need help!

 
0
  #3
Jan 10th, 2009
What I could get out of your unformatted code was
if(remainder==2)
remainder=2;
if(remainder!=2)
remainder=0;
should be better :
if(remainder==2)
remainder=2;
leave this away-->if it IS 2 why set it to 2 ?
if(remainder!=2) remainder=0;-->+-OK, because if you want to print 2 it will print 0
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 18
Reputation: da penguin is an unknown quantity at this point 
Solved Threads: 5
da penguin's Avatar
da penguin da penguin is offline Offline
Newbie Poster

Re: Converting digits..need help!

 
0
  #4
Jan 10th, 2009
Lines 11,12. n is 0 after them and also i don't get why u use for not while and why u need that int i
No, ma'am, we are musicians.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 5
Reputation: winry is an unknown quantity at this point 
Solved Threads: 0
winry winry is offline Offline
Newbie Poster

Re: Converting digits..need help!

 
0
  #5
Jan 10th, 2009
ok,thanks for your help..
i'll fix it..
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 44
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: Converting digits..need help!

 
0
  #6
Jan 10th, 2009
You say that you don't know the exact number of digits.
Then why not he input to the function be a string
such that
  1.  
  2. string convertNum(string num)
  3. {
  4. //1.set counter to 0
  5. //2.Check if it is the end of the string(using counter's position).
  6. // 3.if yes goto goto step 4
  7. // else check if the character in the string is other than 2
  8. // if yes, fill a zero in the string
  9. //increment the counter
  10. // goto step 2
  11. //4.Display or return your string
  12.  
  13. }

Hope that i am not twisting the question.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 5
Reputation: winry is an unknown quantity at this point 
Solved Threads: 0
winry winry is offline Offline
Newbie Poster

Re: Converting digits..need help!

 
0
  #7
Jan 10th, 2009
Thank you so much zalezog..I've made it..I used string and it's way much easier...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC