Thread: Convert String to arithmetic

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    39

    Convert String to arithmetic

    I've got this string:

    Code:
    char * string = "2+2";
    I want to get an integer value = 4 from this. How would I go about doing this.

    Thanks

  2. #2
    Registered User
    Join Date
    Nov 2013
    Posts
    31
    You will have to parse the string. Anytime you come across an character that is an int, add it to a string. Then you can use atoi() function to convert it to an integer. That should get you started. You will have to think about how you account for operators. If you get stuck come back here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 40
    Last Post: 06-03-2010, 12:45 PM
  2. Replies: 5
    Last Post: 05-09-2010, 10:58 AM
  3. convert char** (c string array) to std::string[]
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2008, 05:52 AM
  4. string array arithmetic
    By qubit67 in forum C Programming
    Replies: 2
    Last Post: 09-12-2007, 03:58 AM
  5. Arithmetic with a string.
    By spaceboo in forum C Programming
    Replies: 1
    Last Post: 12-12-2002, 10:32 AM