Suming ints

This is a discussion on Suming ints within the C Programming forums, part of the General Programming Boards category; I am having trouble converting chars into ints: Code: char input[100]; int in, count, i; while ((in = fgets(fp) != ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    Suming ints

    I am having trouble converting chars into ints:
    Code:
    char input[100];
    int in, count, i;
    while ((in = fgets(fp) != EOF){
        input[count] = in; 
        ++count;
    }
    
    int sum;
    for(i=0,i < count; ++i){
      if (isdigit(input[i])){
       printf("&#37;c", input[i]); // this is ok, prints the actual number, if actual num is 1 then 1 is printed
       printf("%d", input[i]); // not ok, if actual num is 1, 49 is printed
       sum = 1 + input[i]  // if actual num is 1, sum should be 2
                                     // but it is 50
       printf("sum %d", sum);
    }
    }
    What is the proper way of doing this ?
    Last edited by Suchy; 04-10-2008 at 01:32 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    input[i] - '0' is the proper way.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to handle ints and strings when writing to file
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 04-23-2008, 04:44 AM
  2. Reading int's from file to array HELP plz
    By GARiMTO in forum C Programming
    Replies: 3
    Last Post: 12-14-2007, 05:12 AM
  3. reading 3 ints from one line, then 3 from another
    By Tokay in forum C++ Programming
    Replies: 10
    Last Post: 11-13-2005, 08:42 PM
  4. fseek and storing ints
    By paperbox005 in forum C Programming
    Replies: 3
    Last Post: 04-04-2005, 09:39 AM
  5. Bitmagic: Sum of 3 ints using a single "+"?
    By Nyda in forum C Programming
    Replies: 10
    Last Post: 04-25-2004, 04:46 AM

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