string to int conversion

This is a discussion on string to int conversion within the C Programming forums, part of the General Programming Boards category; Hello, I was wondering if there is any function or am I able to create a function that convert three ...

  1. #1
    app
    app is offline
    Registered User
    Join Date
    Aug 2005
    Posts
    2

    string to int conversion

    Hello, I was wondering if there is any function or am I able to create a function that convert three given string (ten, twenty and fifty) to the integer equivalent?

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

  3. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    You mean something like
    Code:
    char x[4] = "ten";
    int z = string_to_int_pray_they_spelt_correctly(x);
    putting "ten" into z?

    Or do you mean

    Code:
     char x[4] = "10";
    int z = atoi(x);
    assigning 10 to z?

    atoi exists, see http://www.acm.uiuc.edu/webmonkeys/b...2.13.html#atoi

    The former is more complicated. You are able to create a function that does this, though, but first you'd have to decide exactly what you want to do.

  4. #4
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Oh. Do you only have three possible strings, containing "ten", "twenty", and "fifty"?

    In that case, use the strcmp subroutine in a function to compare the strings, and if you find a match, return the appropriate value.

  5. #5
    app
    app is offline
    Registered User
    Join Date
    Aug 2005
    Posts
    2
    Quote Originally Posted by Rashakil Fol
    Oh. Do you only have three possible strings, containing "ten", "twenty", and "fifty"?

    In that case, use the strcmp subroutine in a function to compare the strings, and if you find a match, return the appropriate value.
    Yep, the program is asking me to type "ten", "twenty", or "fifty" and convert it to 10, 20, or 50 in integer value.

    Thanks for the tips.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 05:40 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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