How do you convert characters from ASCII to Int w/o using the atoi function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Andrea Robinson
    New Member
    • Feb 2011
    • 2

    How do you convert characters from ASCII to Int w/o using the atoi function?

    I need to convert data that is input from a file from ASCII chars to Ints, but the catch is no atoi and I can ONLY convert number characters and ignore the others, and the # symbol has to act like a break.
    For example: if my file inputs $1,956#%34,9 the output will be
    1956
    349
    So I know I need to subtract 0 from the ASCII car to get the int, but how do I get it to skip the other characters and have the # symbol act as a nwln?
    I can't use functions. Just loops. *Sigh*
    So confused...
    I appreciate any advice.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I would use a switch statement to test for the ten digits and the hash symbol with a default that will drop the rest.

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      you can have a loop checking each character, if it is # terminate the loop, if it is a digit add to current value else ignore, e.g.
      Code:
      loop
         read character
         if (character in range '0' to '9') add to value
      until (character is '#')
      test the algorithm by reading characters from the keyboard then convert it to process files

      Comment

      Working...