Thread: How can I improve comma insertion code?

  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
    Location
    Inside my computer
    Posts
    24,654
    >>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, 01:53 PM
  2. Can I improve my code?
    By advancedk in forum C Programming
    Replies: 1
    Last Post: 07-27-2008, 07:47 AM
  3. Help me improve my code!
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 09-19-2006, 10:04 AM
  4. How to improve code
    By rugby in forum C Programming
    Replies: 3
    Last Post: 04-15-2003, 09:24 AM
  5. help improve my code
    By lambs4 in forum C Programming
    Replies: 3
    Last Post: 11-21-2001, 11:33 AM