Help Writing Simple Program

This is a discussion on Help Writing Simple Program within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm taking an entry level C++ class to satisfy my computer engineering major. I had homework last night, which ...

  1. #1
    Registered User
    Join Date
    Sep 2007

    Help Writing Simple Program


    I'm taking an entry level C++ class to satisfy my computer engineering major. I had homework last night, which consisted of writing several programs, and I stumbled upon one, which after several hours, I cannot figure out. Here is the question.

    "Write a program that reads in an integer that is greater than 1,000 and less than 1,000,000. Recall that the number must be entered without a comma. Display the number with a comma inserted where it belongs. (Hint: use the % operator.)

    I can't figure out what variables I will need, and what values to assign them to. This one is really annoying me, so anyone that can offer any help will be appreciated. Thanks.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Well, here's basically what the code must do at the beginning.
    • Read in a number
    • Make sure the number is > 1000 and < 1000000. If not, perhaps read in another number.

    Likely you got that far and then got stuck.

    So how do you print a number with commas? This problem simplifies down to, how can I get the rightmost three digits of a number, and then the next rightmost three digits etc, which you can then print separated with commas.

    The &#37; (modulus) operator will work here. The modulus operator returns the remainder of a number divided by another number. For example, 8%3 is 2, because 8/3 is 2 with a remainder of 2.

    If you use num%10, you'll get the rightmost digit of a number. Think about it. Any number divided by ten will have a remainder between 0 and 9, which will be equal to the last digit.

    So, to get the three rightmost digits of a number, use num%1000. To get the next three rightmost digits, you could use (num/1000)%1000. Or you could just go num/=1000 and do num%1000 once again.

    One disadvantage of this is that you'll need to store the digits and print them in a different order than you got them. So perhaps you'll need to generate a string, and then print that, or use a different algorithm which gives you the leftmost three digits first. This is more complicated, though, because you have to count the number of digits before you know whether the first digits number one, two, or three.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2001
    you need an int that hold the number. then read from the user a number, check whether or not ((num> 1000) && (num < 1000000)) then output the number one section at a time.

    edit: beaten

  4. #4
    Registered User
    Join Date
    Sep 2007
    thanks dwks, i think that helped a lot. now to just test the program.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    Not that I'd recommend it for this program you're working on, but the language itself supports certain features that takes care of this (placing the commas separators in a number) for you. It looks like a lot of work just to get those stupid commas but it's there for consideration (for future reference):
    #include <iostream>
    #include <locale>
    #include <string>
    #include <iterator>
    int main()
        std::locale loc(std::locale("American_USA.1252"),
                        new std::num_put<char,std::back_insert_iterator<std::string> >);
        std::basic_ios<char> str(0);
        std::string s = "";
        std::use_facet<std::num_put<char,std::back_insert_iterator<std::string> > >(loc)
            .put(std::back_inserter(s),str,' ',static_cast<unsigned long>(12345678L));
        std::cout << s << std::endl;
        return 0;
    Tested under MS Visual Studio C++ 2005. I don't understand 100&#37; of what's going on there so don't ask.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  3. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  4. Need help with simple, simple program.
    By LightsOut06 in forum C Programming
    Replies: 5
    Last Post: 09-01-2005, 08:31 PM
  5. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21