|
|
|
|
![]() ![]() |
Mar 13 2005, 02:43 PM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 407 Joined: 13-December 04 Member No.: 2,696 |
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 code Code: #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; } |
|
|
|
Mar 13 2005, 04:21 PM
Post
#2
|
|
|
Member [Level 1] ![]() ![]() ![]() ![]() Group: Members Posts: 69 Joined: 12-March 05 From: Australia Member No.: 4,401 |
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... |
|
|
|
Mar 15 2005, 11:13 AM
Post
#3
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 351 Joined: 19-October 04 From: India Member No.: 1,824 |
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. CODE 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 0 Check it out and confirm if it helped. Cheers. |
|
|
|
Mar 15 2005, 01:06 PM
Post
#4
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 142 Joined: 24-December 04 From: Queensland, Australia Member No.: 2,902 |
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. |
|
|
|
Mar 15 2005, 01:13 PM
Post
#5
|
|
|
Member [Level 1] ![]() ![]() ![]() ![]() Group: Members Posts: 69 Joined: 12-March 05 From: Australia Member No.: 4,401 |
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 big if ( (sum/100000) < 0.1 ) is true, then sum is too small Sorry about any inconvenience caused. |
|
|
|
![]() ![]() |
Similar Topics
| Topics | Topics | |
|---|---|---|
|
|
|
|
Lo-Fi Version | Time is now: 30th August 2008 - 02:00 AM |