| | |
Converting digits..need help!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2009
Posts: 5
Reputation:
Solved Threads: 0
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
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)
#include<iostream> using namespace std; int convertNum(int n){ int digits=0; int remainder; for(int i=n;n!=0;n/=10) digits=digits+1; for(int j=1;j<=digits;j++){ n=n/10; remainder=n%10; if(remainder==2) remainder=2; if(remainder!=2) remainder=0; } return remainder; } int main() { int a; cout<<"Please enter a positive integer"<<endl; cin>>a; cout<<"The converted version of "<<a<<" is ----> "<<convertNum(a); return 0; }
Last edited by Ancient Dragon; Jan 10th, 2009 at 9:23 am. Reason: add code tags
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
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
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Oct 2008
Posts: 44
Reputation:
Solved Threads: 11
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
Hope that i am not twisting the question.
Then why not he input to the function be a string
such that
C++ Syntax (Toggle Plain Text)
string convertNum(string num) { //1.set counter to 0 //2.Check if it is the end of the string(using counter's position). // 3.if yes goto goto step 4 // else check if the character in the string is other than 2 // if yes, fill a zero in the string //increment the counter // goto step 2 //4.Display or return your string }
Hope that i am not twisting the question.
![]() |
Similar Threads
- Converting equation from infix to postfix using stack (C++)
- Converting char into int (C++)
- Converting a String to an int array (C++)
- C++, issues with converting a decimal to fraction (C++)
- converting a float to a fraction. (C++)
- converting int to char in C (C)
- Converting a set of numbers seperated by commas...HELP!!! (C)
- Converting Dec to Hex without using IntToHex... (Pascal and Delphi)
Other Threads in the C++ Forum
- Previous Thread: exceptions fall back to std::exception
- Next Thread: searching for keywords in multiple text files
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






