How can I improve comma insertion code?

This is a discussion on How can I improve comma insertion code? within the C++ Programming forums, part of the General Programming Boards category; Here is the code I have written. It works fine except if the input is a negative six-digit number, a ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    7

    How can I improve comma insertion code?

    Here is the code I have written. It works fine except if the input is a negative six-digit number, a comma appears just after the negative sign like so: -,963,562

    This is part of a larger program, obviously, and one of the other things I need to do is have negative numbers like -45 recognized as valid but numbers like 4-5 not recognized as valid. I hope this is enough information for someone to help me.

    Code:
    string commaString()
           {
                if (validInput = true);
                {
                string comString = inString;
                int length = comString.length();
    
    
                int i = (comString.length() - 3);
                while (i > 0)
                {
                    comString.insert(i, 1, ',');
                    i = i - 3;
                }
    
    
                return comString;
                }
            
           }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    >>if (validInput = true);
    You are assigning instead of comparing.
    You also have an erroneous ending semicolon which should not be there.

    Are you writing some parsing program? Then it might be good to understand how parsing is typically done. You write a regular expression for what is syntactically correct. Then you translate that into a NFA, and then from that, a DFA. These automatons will essentially accept or reject what syntax is valid or not.
    From that, you would need a parser to parse the actual meaning of your syntax, which is typically an LL parser or LR parser. That also means you have to define some grammar for your language.
    There are tools out there that does all this for you. LLVM might be a good place to look, I think, if you are interested.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need ideas on how to improve my code
    By NumLock in forum C Programming
    Replies: 5
    Last Post: 08-10-2011, 02:53 PM
  2. Can I improve my code?
    By advancedk in forum C Programming
    Replies: 1
    Last Post: 07-27-2008, 08:47 AM
  3. Help me improve my code!
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 09-19-2006, 11:04 AM
  4. How to improve code
    By rugby in forum C Programming
    Replies: 3
    Last Post: 04-15-2003, 10:24 AM
  5. help improve my code
    By lambs4 in forum C Programming
    Replies: 3
    Last Post: 11-21-2001, 11:33 AM

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