A C++ question

edited September 2003 in Internet & Media
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

Comments

  • TheBaronTheBaron Austin, TX
    edited September 2003
    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...
  • EMTEMT Seattle, WA Icrontian
    edited September 2003
    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)
  • mondimondi Icrontian
    edited September 2003
    youve probably already got it but here goes
    #include <iostream.h>
    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...
  • edited September 2003
    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.
  • a2jfreaka2jfreak Houston, TX Member
    edited September 2003
    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.
Sign In or Register to comment.