kvarnerexpress 0 Report post Posted March 13, 2005 Hi I am trying to code a program that will input five digit numbers and output it on the reverse order, The code will output the reverse, I still cant find how to check if the input is five digit or not, i have tried using " % / " but i couldn't make it to work, any hint will be appreciated. Thanks.This is my codeCode:#include <iostream>#include <stdlib.h>using namespace std;int main(){ int a,b,c,d,e,sum,find=0; cout << "Enter Your Number of Five Digits: " << endl; cin >> sum; a = (sum % 10); b = (sum % 100); b = b / 10; c = (sum % 1000); c = c / 100; d = (sum % 10000); d = d / 1000; e = (sum % 100000); e = e / 10000; cout << "The Reverse Order is: " << endl; cout << a << b << c << d << e << endl; } Share this post Link to post Share on other sites
theRealSheep 0 Report post Posted March 13, 2005 I can't give you the code for it off the top of my head without trying it myself, but you could do this... You have e = (sum % 100000);. If e >= 1 then sum is six or more digits, If e < 0.1 then sum is only four or less digits. Just a couple of if statements should do the trick. Hope that helps... Share this post Link to post Share on other sites
cse-icons 0 Report post Posted March 15, 2005 Hi theRealSheep,As far as I know, the modulo or % operator works only for int and also returns an int, so no way you can get 0.1 or something..also, the operator would wrap around i.e., the number would get negative and all that ( but not sure). what I suggest is take the input as a string. Then use strlen function to find out the length.After u do that use a for loop and convert all the string characters to numbers. cin >> str;int i = strlen(str);int t =0;if(i==5){ for(int j=0;j<5;j++){ if(isdigit(str[j]){ t = t*10+(str[j]-'0'); else break; // not a number }}else{// do something else coz length not equal to 5} This code should work. though I have not tested it.If u want to print reverse just run the for loop in reverse i.e, j =4 to 0Check it out and confirm if it helped.Cheers. Share this post Link to post Share on other sites
dexter 0 Report post Posted March 15, 2005 Soooo, you want to know if it has 5 digits... why not just test the bounds of a 5 digit number... 10000 - 99999....?It all depends on what the input can be, though. Can it be 02180, or does it have to be an actual 5 digit number. If it is the latter, then the very first thing I said applies to your case. Share this post Link to post Share on other sites
theRealSheep 0 Report post Posted March 15, 2005 Erm, yes. I was wrong with what I posted. Thanks, cse I was thinking of the standard division operator /, instead of the modulo operator.if ( (sum/100000) >= 1 ) is true, then sum is too bigif ( (sum/100000) < 0.1 ) is true, then sum is too smallSorry about any inconvenience caused. Share this post Link to post Share on other sites