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; See I've been working on this problem for almost 2 weeks and we are only allowed to use things that ...

  1. #16
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    See I've been working on this problem for almost 2 weeks and we are only allowed to use things that we learned in class, so we can't use loops or even if else statements. This is a pain in the ass.

  2. #17
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Well you could use the ?: operator, but that's really just another if/else, so would it count.
    Also, you're not likely to have come across it.

    Code:
    cout << (x/1000)%1000;
    cout << ",";
    cout << x%1000;
    Something along these lines, but without if statements, you'll end up with output like
    000,000,123
    000,134,765
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #18
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by StarOrbs
    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.
    If you can't use conditional statements to decide when to print trailing zeros, I have to ask if you have had anything in class about formatting output to cout? (Specifically ostream member functions width() and fill().)

    If so, look at what this does, and maybe you can see how they could solve your problem:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    int main()
    {
      int i;
      int x = 1234;
      for (i = 1; i < 10; i++) {
        cout.width(i);
        cout << x << endl;
      }
      cout << endl;
    
      cout.fill('0');
      for (i = 1; i < 10; i++) {
        cout.width(i);
        cout << x << endl;
      }
    
      cout.width(3); cout.fill('0');
    
      cout << endl;
      x = 12;
    
      cout << x << endl;
    
      return 0;
    }
    Regards,

    Dave

  4. #19
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Gee, sucks that you can't use 'if'. Well, something like this might work (though I haven't tested it):

    **EDIT** Oops, didn't notice you can't use loops. Well, I'm out of ideas then

    Code:
    int lengthOfNewString = strlen(strNumber) + strlen(strNumber) / 3;  //Increase size for commas
    char* newString = new char[lengthOfNewString + 1];  //Leave room for '\0'
    newString[lengthOfNewString] = '\0';
    
    int pos = strlen(strNumber) - 1;
    int nsPos = lengthOfNewString - 1;
    
    int iterations = strlen(strNumber) / 3;
    for(int i = 0; i < iterations; ++i)
    {
       newString[nsPos] = strNumber[pos];
       newString[nsPos - 1] = strNumber[pos - 1];
       newString[nsPos - 2] = strNumber[pos - 2];
       newString[nsPos - 3] = ',';
       nsPos -= 4;
       pos -= 3;
    }
    
    int remainingDigits = strLen(strNumber) % 3;
    for(int i = 0; i < remainingDigits; ++i)
    {
       newString[nsPos] = strNumber[pos];
       --nsPos;
       --pos;
    }
    Last edited by Hunter2; 02-13-2005 at 12:59 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Page 2 of 2 FirstFirst 12
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