Results 1 to 5 of 5

Thread: Executing a string in C++

  1. #1
    Ah, Mrs. Peel! mike_w's Avatar
    Join Date
    Oct 2003
    Location
    Hertfordshire, England
    Posts
    3,326
    Thanks
    3
    Thanked
    9 times in 7 posts

    Executing a string in C++

    Is there any way in C++ to execute a string? For example, if I stored

    variable = 4*4

    inside string, is there anywat in which I could then execute the string? I know in Python that you can simply use the exec function, but I've had no luck searching on Google for a solution to this.

    Any help is appreciated.

    Mike.
    "Well, there was your Uncle Tiberius who died wrapped in cabbage leaves but we assumed that was a freak accident."

  2. #2
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable
    afaik, there's no simple way of doing it in standard C++, you'd need to find some mathematics pharsing library.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  3. #3
    Ah, Mrs. Peel! mike_w's Avatar
    Join Date
    Oct 2003
    Location
    Hertfordshire, England
    Posts
    3,326
    Thanks
    3
    Thanked
    9 times in 7 posts
    Dang, that's annoying. It also makes sense when I think about the differences between C++ and Python (i.e. compilation)
    "Well, there was your Uncle Tiberius who died wrapped in cabbage leaves but we assumed that was a freak accident."

  4. #4
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,164
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts
    tbh, its not too hard to roll your own.

    a recursive function that implments BIDMAS is all you really need. (Brackets Indecies devision multiplication addition subtraction.

    my C++ is terrible of late (too much c#) but basically you'd go for something like this

    public int ParseMaths (string s) {
    if (int.parse(s).toString() == s) {
    return (int);
    // escape condition
    }
    if (-1 != s.indexOf('+')) {
    // now find the nearest space after a number has happened, in .net i'd use a regexp for this

    // now find the nearest space before the + but after a number has appeared. again regexp in c#

    // now add the two, and put the string either side.

    // now call again
    ParseMaths(s);
    }
    }

    note the use of recursion, as its tail, it can be easily replaced by itteration once you've got it working and are happy with it.
    throw new ArgumentException (String, String, Exception)

  5. #5
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    If your Strings are always formatted a certain way, then it should not be too difficult to do as TheAnimus says. If you have no control over their format - difference in whitespace etc - then I imagine there is a dandy library out there somewhere capable of managing such inconsistencies.
    To err is human. To really foul things up ... you need a computer.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VB String Manipulation
    By PHTC in forum Software
    Replies: 12
    Last Post: 01-02-2006, 06:51 PM
  2. SQL Server Connection string?
    By Stoo in forum Software
    Replies: 7
    Last Post: 04-05-2005, 05:48 PM
  3. Just got my first 12 string
    By Alex in forum Consumer Electronics
    Replies: 8
    Last Post: 10-07-2004, 01:00 PM
  4. Zakky - Thanks for the string dude :)
    By Tumble in forum General Discussion
    Replies: 3
    Last Post: 03-06-2004, 03:01 PM
  5. String and rope......
    By Zak33 in forum General Discussion
    Replies: 17
    Last Post: 31-01-2004, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •