QUICK HELP: How to convert what str[i] points at into an integer??

This is a discussion on QUICK HELP: How to convert what str[i] points at into an integer?? within the C Programming forums, part of the General Programming Boards category; Hi! I have: char str[100]; int temp; I use fgets to read in like [2 3 +]. When i = ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    6

    QUICK HELP: How to convert what str[i] points at into an integer??

    Hi!

    I have:
    char str[100];
    int temp;

    I use fgets to read in like [2 3 +].
    When i = 0, str[i] points at the character 2.
    How can I get temp to be 2??

    temp = str[i] don't work because str[] is declared as a char.
    temp gets another value than 2!
    HOw do I convert what str[i] is pointing at into an integer?

    Would appreciate an answer

  2. #2
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    Simple, just write a little function to do the conversion for you:
    Code:
    int ctoi(char c) {
            return c - '0';
    }
    
    ...
    
    temp = ctoi(str[i]);
    Of course, since what you're writing looks like a reverse polish notation calculator, this solution hurts you more than it helps because you can't have numbers with more than one digit without doing something awkward. You'd be better off tokenizing the string and then doing your conversions.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156

    Re: QUICK HELP: How to convert what str[i] points at into an integer??

    Originally posted by valhall
    Hi!

    I have:
    char str[100];
    int temp;

    I use fgets to read in like [2 3 +].
    When i = 0, str[i] points at the character 2.
    How can I get temp to be 2??

    temp = str[i] don't work because str[] is declared as a char.
    temp gets another value than 2!
    HOw do I convert what str[i] is pointing at into an integer?

    Would appreciate an answer
    When i points to the start of a number, use:
    temp = atoi(&str[i]);

    Then
    skip over the number, looking for a nondigit,
    skip over the whitespace by looking for a digit,
    repeat the call to atoi()
    etc.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access Violation
    By Graham Aker in forum C Programming
    Replies: 100
    Last Post: 01-26-2009, 07:31 PM
  2. Replies: 8
    Last Post: 11-03-2008, 08:48 PM
  3. Yahtzee C++ programme help
    By kenneth_888 in forum C++ Programming
    Replies: 13
    Last Post: 09-05-2007, 02:14 PM
  4. CProg Fantasy Football version pi
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 155
    Last Post: 12-26-2006, 03:30 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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