combining digits into a number

This is a discussion on combining digits into a number within the C Programming forums, part of the General Programming Boards category; Hi, im getting digits from a file, but i need to know how to combine these digits into a number ...

  1. #1
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69

    combining digits into a number

    Hi,

    im getting digits from a file, but i need to know how to combine these digits into a number so i can store it as an int. So for example my file is

    1234

    i then read digit by digit like so..

    current1 = fgetc( inFile ); /* =1 */
    current2= fgetc( inFile ); /* =2 */
    current3 = fgetc( inFile ); /* =3 */
    current4 = fgetc( inFile ); /* =4 */

    now how do i form these into a single number again.. is it something like..

    int finalnum = current1 +current2 +current3 +current4 + something..;

    im not sure.. please help

    thanks.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    57
    atoi converts a char to a int.

    Instead of going character by character you could try to reading in a char string, or just keep appending to one you have off to the side. Then run atoi

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Remember that 1 is not necessarily '1' in ascii

    Do what zxcv said, but read it in as a string (or char array) then convert the entire string rather than each char.

  4. #4
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    575
    1, in ASCII, is actually 49. 48+number gives the actual ASCII position. If you want to read single digits, then you can take the value that is read and subtract 48 from it. To read a group of digits as a single number, read the full string as needed then use atoi or atof, depending on the format you're using.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by ulillillia View Post
    1, in ASCII, is actually 49. 48+number gives the actual ASCII position. If you want to read single digits, then you can take the value that is read and subtract 48 from it. To read a group of digits as a single number, read the full string as needed then use atoi or atof, depending on the format you're using.
    Not always, For most 'normal' systems yes. The only thing that *shouldn't* change is the ordering of the table.

    So you'd be better to do:

    Code:
    signed char val = myvar - '0';

  6. #6
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    cheers, ill give it ago

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting number of digits in a variable
    By Saeid87 in forum C Programming
    Replies: 6
    Last Post: 06-05-2009, 01:13 PM
  2. five digit number with different digits
    By khdani in forum C Programming
    Replies: 11
    Last Post: 05-03-2009, 11:33 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. Count the number of vowels, consonants, digits etc.
    By kumar14878 in forum C Programming
    Replies: 3
    Last Post: 05-09-2005, 12:34 AM
  5. Extracting individual digits from a short
    By G'n'R in forum C Programming
    Replies: 9
    Last Post: 08-30-2001, 10:30 AM

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