Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How can i add commas to certain numbers

Write a program that reads a number greater than or equal to 1000 from the user and prints it out with a comma seperating the thousands. Here is a sample dialog; the user input in color:

Please enter an integer >= 1000 : 23456

23,456

Can anyone figure this out?






«1

Comments

  • #include
    using namespace std;
    int main()
    { cout << "Please enter an integer >= 1000 : ";
    double numbers;
    cin >> numbers;
    cout << numbers << endl;
    return 0;
    }


  • : Write a program that reads a number greater than or equal to 1000 from the user and prints it out with a comma seperating the thousands. Here is a sample dialog; the user input in color:
    :
    : Please enter an integer >= 1000 : 23456
    :
    : 23,456
    :
    : Can anyone figure this out?

    Well, you already have the number in string form (because you read it from the console). Now all you have to do is count off characters from the right and insert a comma after every three digits. You can insert the commas while copying the digits to a new string, or you can insert them in place. This will give you practice on looping and string handling at the character level. It's not going to be easy, but you can always post here for help.

    Cheers,
    Eric


  • #include
    #include

    using namespace std;

    int main()

    {
    cout << "Please enter an integer >= 1000 (ex:'23 456'): ";
    string number1;
    string comma;
    string number2;
    cin >> number1 >> comma >> number2;
    comma = ',';
    string final = number1.substr(0, 3) + comma.substr(0, 1) + number2.substr(3, 6);

    cout << "The interger you entered is: " << final << endl;

    return 0;
    }
    this is what i have so far but i dont know how to produce the last numbers? This is for my programming class and been working on this for days can someone help?

  • Sometimes it is just easier to use a little figuring out beforehand and then bruteforce the result.

    Think about this, find out how many digits would be at the very left of your number, e.g.:

    1,234 has one
    12,345 has two
    123,456 has three

    Okay, now you have that. Now, say for example you have two digits on the end.

    Write out the first two digits of your number and then add a comma. Something like,

    cout << Number.substr (0,2);

    Then output a comma,

    cout << ",";

    and then output three more digits and a comma, and continue doing that until you reach the end of the string. You don't even need to know how many digits are actually in your string to begin with, only how many are on the very left.

    -Xotor-

  • xotor,

    i did what you said but it wont show the rest of the #'s.
    ex: user inputs '1000'
    then the screen shows '10,'

    im really lost here.i need for the program to place the comma where is needed. 1,000 or 100,000

    ill really appreciate it if you can teach me this one thing...im stuck!

    -Michelle

  • : : Write a program that reads a number greater than or equal to 1000 from the user and prints it out with a comma seperating the thousands. Here is a sample dialog; the user input in color:
    : :
    : : Please enter an integer >= 1000 : 23456
    : :
    : : 23,456
    : :
    : : Can anyone figure this out?
    :
    : Well, you already have the number in string form (because you read it from the console). Now all you have to do is count off characters from the right and insert a comma after every three digits. You can insert the commas while copying the digits to a new string, or you can insert them in place. This will give you practice on looping and string handling at the character level. It's not going to be easy, but you can always post here for help.
    :
    : Cheers,
    : Eric
    :
    :
    I dont know how to do the looping thing...im getting very different answer freom people and im getting even more confused... can u do one example?

  • : : : Write a program that reads a number greater than or equal to 1000 from the user and prints it out with a comma seperating the thousands. Here is a sample dialog; the user input in color:
    : : :
    : : : Please enter an integer >= 1000 : 23456
    : : :
    : : : 23,456
    : : :
    : : : Can anyone figure this out?
    : :
    : : Well, you already have the number in string form (because you read it from the console). Now all you have to do is count off characters from the right and insert a comma after every three digits. You can insert the commas while copying the digits to a new string, or you can insert them in place. This will give you practice on looping and string handling at the character level. It's not going to be easy, but you can always post here for help.
    : :
    : : Cheers,
    : : Eric
    : :
    : :
    : I dont know how to do the looping thing...im getting very different answer freom people and im getting even more confused... can u do one example?

    Don't get too stressed, it's not as easy as it might first appear. The easiest way to do it is to output each character one at a time, inserting commas as needed. Since you're using std::string, to access individual characters you can use operator[], or using string::iterator. As in:

    [code]for (int = 0; i < s.length(); ++i)
    putchar (s[i]);[/code]or[code]for (string::iterator it = s.begin(); it != s.end(); ++it)
    putchar (*it);[/code]or you could use pointers, but basic_string::iterator really is just char* anyway, and the iterator is cleaner.

    Anyway, the real trick is knowing when to insert a comma. We need to know when we're at the end of a "three digit group". Well we know we need to insert commas after the digits 3,6,9,12,etc. Multiples of three, right? How do we find out in code if a number is a multiple of three? Just divide by 3; if there's no remainder, it's an even multiple. Of course, anytime we need the remainder of an integer division, we know we're going to be using the mod operator (%).

    Let's say we have the number:

    [code]84903928[/code]Let's number the digits, right to left:

    [code]the number : 84903928
    digits index: 87654321[/code]Digit 1: 1%3==1
    Digit 2: 2%3==2
    Digit 3: 3%3==0 .. end of a group of 3 digits, need a comma here
    Digit 4: 4%3==1
    Digit 5: 5%3==2
    Digit 6: 6%3==0 .. end of a group of 3 digits, need a comma here
    Digit 7: 7%3==1
    Digit 8: 8%3==2

    So now we have mathematical way of determining if we're at a digit that needs a comma.

    Small problem. In our 'legend' above we number the digits [N..1] (with the 'first' digit on the right). However, in C++ you index digits in a string [0..N-1] (with the 'first' digit on the left). So you'll have to reconcile that in your program. The easiest way is to loop from N to 1, as in "for (int i = length; i >= 1; --i)", or something like that. If (i % 3 == 0), we know we need to insert a comma.

    Hopeful that helps and doesn't just confuse the crap out of you. ;)

    Good luck,
    Eric







  • : i did what you said but it wont show the rest of the #'s.
    : ex: user inputs '1000'
    : then the screen shows '10,'
    :
    : im really lost here.i need for the program to place the comma where is needed. 1,000 or 100,000
    :
    : ill really appreciate it if you can teach me this one thing...im stuck!

    Have you determined how many characters on the left you need?

    You've need to have your program count the number of digits there are in the number, and then perform a modulo (%) of three on that number to get how many you need to print before simply printing three at a time.

    Pseudocode:

    NumDigits = Number of digits in number (10000 would have five).
    EndDigits = NumDigits % 3;
    If EndDigits = 0, EndDigits = 3;

    Output -EndDigit- amount of characters from the Numberstring.

    While there are still digits left to be printed:
    print a comma
    Read three more digits and print those out.
    Repeat

    If you're wondering how to read more digits, use your substr function to grab digits, and then move to the end of those digits:

    Value = Number.substr (i, 3);
    i += 3;

    -Xotor-

  • This is what i got so far...but im getting confused with the numbers...your telling me to read from right to left and xotor is telling me from left to right. I dont know!I'm i even going in the right direction. I think i got some of it.

    #include
    #include
    #include

    using namespace std;

    int main()

    {
    cout << "Please enter an integer >= 1000: ";
    int comma;
    int Digit1;
    int Digit2;
    int Digit3;
    int Digit4;
    int Digit5;
    int Digit6;
    cin >> Digit1 >> Digit2 >> Digit3 >> Digit4 >> Digit5 >> Digit6;

    Digit1 = 1%3==1;
    Digit2 = 2%3==2;
    Digit3 = 3%3==0;
    comma = ',';
    Digit4 = 4%3==1;
    Digit5 = 5%3==2;
    Digit6 = 6%3==0;

    for (int i = length; i >= 1; --i)

    return 0;
    }

  • : This is what i got so far...but im getting confused with the numbers...your telling me to read from right to left and xotor is telling me from left to right. I dont know!I'm i even going in the right direction. I think i got some of it.

    Well, Xotor gave you a better "road map" than I did. My code was probably a bit more theoretical I guess, trying to get you to understand how the mod operator would work for this situation, it was not really meant as example code. You don't want to address each digit individually like you're doing, you need to do this in a loop so that your program can handle number that are arbitrarily long.

    Xotor has already given you pseudocode. He's some psuedocode for another approach:

    [code]digit = length of string - 1
    c = first char of string
    while digit is greater than 0
    output c
    c = next char of string
    if (digit != 0 and digit mod 3 == 0)
    output ','[/code]
    Cheers,
    Eric

«1
Sign In or Register to comment.