Commas, I'm Stumped

This is a discussion on Commas, I'm Stumped within the C++ Programming forums, part of the General Programming Boards category; I've been trying to figure out how to do this for a couple weeks and I cannot figure out how ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    59

    Commas, I'm Stumped

    I've been trying to figure out how to do this for a couple weeks and I cannot figure out how to do for the life of me. I have to get a program to do this:

    Enter a 4-6 digit number: 1234
    The number you entered is: 1,234

    or like this 12345 into 12,345

    I've been told I can do this without an if-else statement and with just simple c++ I can do it. I DO NOT want the answer I'm just wondering if anyone has any hints for me or can lead me in the right direction..

    I've set it up so each number the computer reads is its own variable the problem is inserting the comma when its either 12,345 or 1,234 or 123,456... The only thing I can think of is someone inserting the comma always 3 digits before the end of the decimal place. Thanks in advanced.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I'm just wondering if anyone has any hints for me
    Hint: You can convert integer values into strings of chars that represent that number.

  3. #3
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    [HINT]
    Well, I would take a look at the modulus operator (%) and powers of 10, aka 100, 1000, 100000.
    [/HINT]
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    I've done the first suggestion. I've never been able to figure out the purpose of modulus. This is just so annoying, I just can't seem to think of anyway to do it.

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    If you did in actual fact do the first suggestion, then why don't you just go through the string backwards, and read in each element to a new string? Every third element, place a comma in the new string.

  6. #6
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    hint: if (number > value && number < anothervalue)
    EDIT
    If you do the other suggestion then just remember to check if the number has another digit after the , else you'll have something like ,123

  7. #7
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Well the first suggestion makes things pretty easy.

    1. get number from user
    2. convert number into char array
    3. count backwards from the end of the array
    a. every 3rd number there should be a ',' inserted into the array

    edit - FAQ int to char array
    Last edited by andyhunter; 02-12-2005 at 03:30 PM.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Quote Originally Posted by MadCow257
    hint: if (number > value && number < anothervalue)
    EDIT
    If you do the other suggestion then just remember to check if the number has another digit after the , else you'll have something like ,123
    That would be nice but I can't use if statements yet.

  9. #9
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    If you can't use ifs then I really doubt you can use strings. I am assuming you need to use the % then. If you think hard enough the answer will reveal itself...

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    I figured I would end up using modulus but I haven't been able to figure that out. lol... I know it takes the reminder but I don't know looks like I got a lot of guessing to do..

  11. #11
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Look dude, just play around with it. You'll have fun. Below DOES NOT SOLVE YOUR PROBLEM. Simply points you in the right direction. You'll have to figure out the whole 0 thing:
    Code:
    #include <iostream>
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main() {
    
        int myNumber;
    
        cout << "Enter a number: ";
        cin >> myNumber;
    
        cout << (myNumber/1000) % 100000 <<" " << myNumber % 1000 << endl;
    
        return 0;
    }
    And my output:
    Attached Images Attached Images  
    Last edited by andyhunter; 02-12-2005 at 06:25 PM.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    You can do it using locales and facets but that is definitely an advanced topic. I posted an example of that in this thread a couple weeks back. It's ugly but it works. For beginners, the easiest way might be to do as suggested and convert to a character representation and work with that.
    "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

  13. #13
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Thanks, at least I'm moving in a good direction now. Now I have to figure out how to add the zeros when needed.

  14. #14
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    I've been messing around with a bunch of combinations of % and / and adding new variables multipling and divivding the first and second part of the calculations. I tried to logically write down a piece of paper what would do it. When I enter 1200 I get 1,200 but when I got up to one more zero I get this, I enter 12,000 and get 12,0 and when I enter 120,000 I get 120,0 ... Looks like there are 2 zeros missing.. Very Confusing. lol.. Well thanks for your help everyone hopefully I'll be able to get it tommorow.

  15. #15
    Registered User
    Join Date
    Sep 2004
    Posts
    36
    I was stumped with the same problem. This is what I did.
    Declare a variable called big and set it equal to 1000000000. This will be the largest number that can be entered. Then use a while loop to divide big by 10 each time the loop is run. Loop while the number entered is < big. Then make another while loop to divide number by big and store that value into x. Loop until x==0.1. Then divide big by 10. And then x=number%big.
    This makes u print the number one digit at a time. If big=1000, 1000000 or 1000000000 then print a comma. I will get the program to you ASAP.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking for commas in imported files
    By Ness757 in forum C Programming
    Replies: 2
    Last Post: 03-23-2006, 12:49 PM
  2. Problems with commas as input
    By Yojimbo III in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2003, 08:18 PM
  3. questions about commas again
    By CuriousNoob in forum C Programming
    Replies: 14
    Last Post: 01-12-2003, 12:32 AM
  4. help with stl search/find, or something else- stumped!
    By Terranc in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2002, 03:11 PM
  5. commas in large numbers
    By HKR in forum C Programming
    Replies: 7
    Last Post: 03-06-2002, 06:08 PM

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