943,813 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 458
  • C++ RSS
Jan 10th, 2009
0

Converting digits..need help!

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
winry is offline Offline
5 posts
since Jan 2009
Jan 10th, 2009
0

Re: Converting digits..need help!

I'd be glad if you could read all the intro threads which describe how to post code.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 10th, 2009
0

Re: Converting digits..need help!

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
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,738 posts
since Oct 2008
Jan 10th, 2009
0

Re: Converting digits..need help!

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
Reputation Points: 36
Solved Threads: 5
Newbie Poster
da penguin is offline Offline
18 posts
since Dec 2008
Jan 10th, 2009
0

Re: Converting digits..need help!

ok,thanks for your help..
i'll fix it..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
winry is offline Offline
5 posts
since Jan 2009
Jan 10th, 2009
0

Re: Converting digits..need help!

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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Jan 10th, 2009
0

Re: Converting digits..need help!

Thank you so much zalezog..I've made it..I used string and it's way much easier...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
winry is offline Offline
5 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: exceptions fall back to std::exception
Next Thread in C++ Forum Timeline: searching for keywords in multiple text files





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC