Help needed. Simple C++ program.

This is a discussion on Help needed. Simple C++ program. within the C++ Programming forums, part of the General Programming Boards category; How do i make a program using functions to get a "long" type number from the user and display it ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    36

    Help needed. Simple C++ program.

    How do i make a program using functions to get a "long" type number from the user and display it back to the screen with commas to seperate the digits.

    For example, if the user puts in a "1982633", then it should print back to the screen, "1,982,633". It should do this for any number up till 10 digits. '
    It should be simple, basic and easy to understand for i am a newbie and i dont know C++ very well.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    figure a count method for how long the number the user is entering is, then by doing so place a comma every 3 digits.
    When no one helps you out. Call google();

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    figure a count method for how long the number the user is entering is, then by doing so place a comma every 3 digits.
    Would you like to show us how you plan to output commas in the middle of a long?


    It's do-able, but it might be a tad complex for a newbie. I've done it before so I can help you out. The logic is quite simple once you see it in action, but it might take a while to get used to the applications of some of the lesser-used concepts, like modulus (remainder)You'll need to convert the number to an array of chars first, but I don't know of any function that will do this for you (of course you can go the other way with cstdlib). I suggest you use a loop and the modulus operator (think, "10's place, 100's place, etc..." - PM me if you need some more help), and you can fill in the digits one by one. Once you've done that, start at the end of your array and work toward the beginning. Into a new, larger array, read in 3 digits from your old array, then a comma, and repeat until you can get to the beginning of your old array in less than 3 jumps.

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    > Would you like to show us how you plan to output commas in the middle of a long

    I think he didn't mean the identifier of type long, hence the quotes....by "long" he meant, a number with the commas.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    36
    Yeah, show me how u put commas between a long without using arrays or anything complex. The most complex thing i've learned in functions (basic functions)

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    > Yeah, show me how u put commas between a long without using arrays or anything complex. The most complex thing i've learned in functions (basic functions)

    no one will "show" you much unless you put forth some effort - except if quzah wants to put it on a silver platter

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    try writing it out first - find out in detail what it is you really need to do here... for example, you know that you want to seperate the numbers into three sections and put commas in between those three sections. that's the obvious part.

    now you have to think more in-depth about it. computers can do math billions of times faster than you, but you can recognize most patterns billions of times faster than a computer can. case in point: breaking up a number into three sections.

    now you can do this several ways. either way, you want to start at the righ side (ones place) of the number and count off by threes moving to the left until you have less than three left. again, this is what you need to figure out.

    you need to come up with an algorithm, or a method of doing something. for example, say I want to swap two numbers. I can just say to 'swap' them and it's that simple. but in computer terms, you need to come up with an algorithm to do that. in this case, it would go something like this:

    1. create a third temporary position
    2. put item one in that position
    3. put item two in item one's position
    4. put the item in the temporary position in position two


    now it's up to you to do something like that for your situation. again, you know you need to segment the number into sections of three. I recommend you look into the use of the mod operator (%), strings and/or arrays.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I think he didn't mean the identifier of type long, hence the quotes....by "long" he meant, a number with the commas.
    Even so. The same applies to any of the standard numerical types.

  9. #9
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    If you want to go to the dark side, try this:
    Code:
    #include <iostream>
    #include <cstring>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    // Commasplice large numbers
    static char *commaSplice (char *sz);
    
    int main ()
    {
      // User input commasplice variable
      char c[255];
      // Used by for loop
      int i;
    
    // If the user enters a bad number to be commaspliced
    ENTER_NUMBER_AGAIN:
    
      // Ask for number
      cout << "Enter a number to be spliced please:" << endl;
      cin.getline (c, 255);
    
      // Make sure the string is all numerical
      for (i = 0; i < strlen (c) && isdigit (c[i]); i++);
      if (!isdigit (c[--i]))
        goto ENTER_NUMBER_AGAIN;
    
      // Convert the string number into commaspiced art
      strncpy (c, commaSplice (c), sizeof (c));
    
      // Print out the spliced version of the number input
      cout << "Comma Spliced:\n" << c << endl;
    
      return 0;
    }
    
    // Commasplice large numbers
    static char *commaSplice (
        char *sz)     // The string that needs to get spliced
    {
      // How long the string is; also used as a current pointer to our original string
      int l = strlen (sz);
    
      // Incrementer on how many numbers we've covered
      int i = 0;
    
      // How many commas are going to be needed?
      int c = (l - 1) / 3;
    
      // Our commaSpliced version of the incoming string.
      char *szS = new char[strlen (sz) + 1 + c];
    
      // Current location of current string PLUS commas
      int lS = strlen (sz) + c;
    
    // Work backwards of the string we need to commasplice and
    // add ',' where appropriate.
    NEXT_CHARACTER:
    
      // Make sure our current location is inside the original string
      if (--l < 0)
        return szS;
    
      // Increment the total numbers we've moved
      i++;
    
      // Add the latest char from original to commaSpliced string
      szS[--lS] = sz[l];
    
      // Add comma when needed
      if (i % 3 == 0)
        szS[--lS] = ',';
    
      // Proceed to the next character to move
      goto NEXT_CHARACTER;
    }

  10. #10
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    I'm curious why you chose to use goto when all you're doing is simulating the effect of a loop. Wouldn't it more clearly show your intentions if you just used a loop in the first place?
    Kampai!

  11. #11
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Not exactly, check out my post on the goto thread here.

    I can't find another way to make it so that the indenting is only 1 level to the right. I'm attemping to use Siewald's "Pillars of Pretty Code" method. He says to use goto if needed, and it seemed needed. I could use a loop, but it would mean my code would be flushed more to the right, and that's bad for the code structuring I'm trying to attempt.

  12. #12
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    my code would be flushed more to the right
    I doubt it

    edit: Your code may be formatted nicely, but it's a kick in the pants to try and follow what you're doing with each goto. Loops, man...

  13. #13
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    Kleid-0, I don't care that you're posting solutions dude, but you're teaching bad habits - stop using goto's, or at least, in the beginning of your post, worn the op that you are using poor coding style.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I can't find another way to make it so that the indenting is only 1 level to the right.
    Hm? If you're trying not to have any indentation, loops aren't going to force you to indent. It's just about the same readability if you use a loop without indentation as if you use goto to avoid indentation. Heck, if I was forced to use goto as a loop mechanism like that, I'd indent it anyway just to make it clear that that particular block of code goes in one piece.

    In short, I think Mr. Seiwald is an idiot, unless he's a renegade sadist who enjoys destroying the lives of maintenance programmers, or unless he's writing about techniques for obfuscated code contests. In which case he's a freaking genius.

    >>Rearrange conditionals so that the block with the quickest exit comes first....
    That's got nothing to do with goto.
    Last edited by Hunter2; 01-26-2005 at 09:31 PM.
    Just Google It. √

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

  15. #15
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by Hunter2
    >>Rearrange conditionals so that the block with the quickest exit comes first....
    That's got nothing to do with goto.
    I was just quoting most of the paragraph.

    Quote Originally Posted by Hunter2
    It's just about the same readability if you use a loop without indentation as if you use goto to avoid indentation.
    Not necessarily, loops require brackets lol (I'm starting to grasp some things here...).

    Quote Originally Posted by Hunter2
    If you're trying not to have any indentation, loops aren't going to force you to indent.
    Yes but it's customary and higher readability if you do indent on loops.

    The whole goal of Mr Seiwald's Pretty Code on page 63 of the care bears magazine is that TOO much indentation is going to eat out our brains and we're all going to explode!! I don't care if this is the Nazi party, but we must follow together as a family! We need our code to be as readable as possible, and goto was required by state law.

Page 1 of 2 12 LastLast
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. Mystery bug in simple program? - I am beginner
    By archie123 in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2005, 07:23 AM
  5. Help with small simple program
    By Sonor in forum C Programming
    Replies: 5
    Last Post: 04-02-2005, 06:40 AM

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