To talk on Icrontic, just register!

It only takes 30 seconds.

Have an account? Sign in:

Forgot?
Swettacular
Bah Humbug
Swettacular
30 Posts

A C++ question

I'm stuck on this problem and I was hoping maybe someone here could help me: Write a program that prompts a user for a five-digit integer number and then prints the individual digits of the numbers on a line with three spaces between the digits. I'm somehow supposed to use the remainder operator(%), but I don't know how to make it work. Any help would be greatly appreciated
TheBaron
Veteran Icrontian
TheBaron
2,777 Posts
well i could do the whole thing for you. but doesn't any integer%10 yield the last digit? just a thought.

that had BETTER be right, or else i'm going to hit myself in the face. i guess i haven't touched code in 2 years though...
EMT
Veteran Icrontian
EMT
1,070 Posts
Well the simplest way to do it is to keep dividing by ten. For example:

Input 59328

59328 % 10 = 8
59328 / 10 = 5932 (int will chop)
5932 % 10 = 2
5932 / 10 = 593
593 % 10 = 3
593 / 10 = 59
59 % 10 = 9
59 / 10 = 5
5 % 10 = 5
5 / 10 = 0 <-- done

The numbers you want are the bold ones (backwards order)
mondi
dot.
mondi
798 Posts

» Subscriber

youve probably already got it but here goes

Code:
#include 
int main(int argc, char* argv[])
{
	int input;
	int backwards[5];
	cin >> input;
for (int count=0;count<5;count++)
{
	backwards[count]=input%10; 
	input/=10;
}
for (count=5;count>0;count--)
	cout << backwards[count-1] << "   ";
	cout << "\n";
	return 0;
}


the #include is < iostream.h > ... it gets left out by the / code modifier for some reason

hooray for making things more complicated than they need be

of course youll need to add code to make sure the input is only 5 digits && numeric etc...
__________________

coj08

Swettacular
Bah Humbug
Swettacular
30 Posts
Thanks for all your help guys, I knew it was something like divide by ten but I just couldn't get it. Teach me to do my homework so late at night.
a2jfreak
madasiemanym
a2jfreak
3,351 Posts
This might be a bit nit-picky, but % is not called the remainder operator. % is called the modulus operator. Sure, it's pretty much the same thing, but if you go around asking someone in person how to do something with the remainder operator he may or may not know what you're talking about.

Sorry I didn't have an answer for you. I was AFK all day yesterday and by the time I saw this today you already had been answered.
__________________
One thing kids like is to be tricked. For instance, I was going to take my little nephew to Disneyland, but instead I drove him to an old burned-out warehouse. "Oh, no," I said. "Disneyland burned down." He cried and cried, but I think that deep down, he thought it was a pretty good joke. I started to drive over to the real Disneyland, but it was getting pretty late.
Jack Handey
[folding_sig1]Child Search Ministries.
A Christian Charity for Missing Children.

Get Firefox!
Go Back   Icrontic Forums > Tech: Software > Web & Digital Media
Jump to
This Thread Search this Thread
Search this Thread:

Advanced Search


Current time: 6:53am (GMT)
Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Get Vanilla instead. Trust me.