File Input/Output

Discussion in 'C++' started by nkomli@gmail.com, Jul 24, 2007.

  1. Guest

    Okay, I want to allow the user to define their own variables and
    equations and then use them to run a program. The user picks the
    option to create a list of variables


    If the user enters in r d and s at the prompt
    how would you define each as a double variable?



    Secondly I want to allow the user to enter in an equation that would
    stored in a file than spit out when the program is running to
    calculate the values. Let's say the user put

    r+d+s


    I was thinking of opening up a filestream and reading the user entered
    equation into a file as a string

    cin >> equation

    fileout << equation


    then reading it back into the string variable once the user chooses
    the option to calculate values.

    filein >> equation

    and using the variables and the equation to calculate x.

    x = equation

    But I think it will give me errors since x just might spit out the
    string. Can someone help me figure out a way to get this to work?

    Thanks
    , Jul 24, 2007
    #1
    1. Advertising

  2. wrote:
    > Okay, I want to allow the user to define their own variables and
    > equations and then use them to run a program. The user picks the
    > option to create a list of variables
    > [..]


    Programming an interpreter has been covered in literature for quite
    some time now. Please google for "C++ expression interpreter".

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 24, 2007
    #2
    1. Advertising

  3. ThreeRight

    Joined:
    Jul 9, 2007
    Messages:
    12
    ThreeRight, Jul 25, 2007
    #3
  4. Guest

    On Jul 24, 2:21 pm, "Victor Bazarov" <> wrote:
    > wrote:
    > > Okay, I want to allow the user to define their own variables and
    > > equations and then use them to run a program. The user picks the
    > > option to create a list of variables
    > > [..]

    >
    > Programming an interpreter has been covered in literature for quite
    > some time now. Please google for "C++ expression interpreter".
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask



    Actually, I only get mostly this and related comments back with
    google. I only want to parse equations and numerical values so
    hopefully it won't be too complicated
    , Jul 31, 2007
    #4
  5. * :
    > Okay, I want to allow the user to define their own variables and
    > equations and then use them to run a program. The user picks the
    > option to create a list of variables
    >
    >
    > If the user enters in r d and s at the prompt
    > how would you define each as a double variable?
    >
    >
    >
    > Secondly I want to allow the user to enter in an equation that would
    > stored in a file than spit out when the program is running to
    > calculate the values. Let's say the user put
    >
    > r+d+s
    >
    >
    > I was thinking of opening up a filestream and reading the user entered
    > equation into a file as a string
    >
    > cin >> equation


    Use std::getline, you don't want tokens you want the whole line.



    > fileout << equation
    >
    >
    > then reading it back into the string variable once the user chooses
    > the option to calculate values.


    Uh, what's that got to do with anything? You could let the program do
    anything in the meantime. You could let it play Tetris.

    >
    > filein >> equation
    >
    > and using the variables and the equation to calculate x.
    >
    > x = equation
    >
    > But I think it will give me errors since x just might spit out the
    > string. Can someone help me figure out a way to get this to work?


    Simplest is to use the user's equation to generate code in some
    programming language (which could even be C++), then let your program
    compile it if necessary, and run it.

    Alternatively, look at e.g. the Boost Spirit parsing framework.

    There's probably an example somewhere of using Spirit to do just what
    you ask, a simple expression parser.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jul 31, 2007
    #5
  6. wrote:
    > On Jul 24, 2:21 pm, "Victor Bazarov" <> wrote:
    >> wrote:
    >>> Okay, I want to allow the user to define their own variables and
    >>> equations and then use them to run a program. The user picks the
    >>> option to create a list of variables
    >>> [..]

    >>
    >> Programming an interpreter has been covered in literature for quite
    >> some time now. Please google for "C++ expression interpreter".
    >>
    >> V
    >> --
    >> Please remove capital 'A's when replying by e-mail
    >> I do not respond to top-posted replies, please don't ask

    >
    >
    > Actually, I only get mostly this and related comments back with
    > google. I only want to parse equations and numerical values so
    > hopefully it won't be too complicated


    Get a hold of a copy of TC++PL by Stroustrup and open chapter 6
    ("Expressions"). It contains an example of expression interpreter
    you could model/extend on.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 31, 2007
    #6
  7. Sjouke Burry Guest

    Victor Bazarov wrote:
    > wrote:
    >> On Jul 24, 2:21 pm, "Victor Bazarov" <> wrote:
    >>> wrote:
    >>>> Okay, I want to allow the user to define their own variables and
    >>>> equations and then use them to run a program. The user picks the
    >>>> option to create a list of variables
    >>>> [..]
    >>> Programming an interpreter has been covered in literature for quite
    >>> some time now. Please google for "C++ expression interpreter".
    >>>
    >>> V
    >>> --
    >>> Please remove capital 'A's when replying by e-mail
    >>> I do not respond to top-posted replies, please don't ask

    >>
    >> Actually, I only get mostly this and related comments back with
    >> google. I only want to parse equations and numerical values so
    >> hopefully it won't be too complicated

    >
    > Get a hold of a copy of TC++PL by Stroustrup and open chapter 6
    > ("Expressions"). It contains an example of expression interpreter
    > you could model/extend on.
    >
    > V

    Get c-snips from
    http://www.cs.umu.se/~isak/snippets/
    They have code for eval.c a simple command line calculator.
    Also a ton of other small, usefull c cource.
    Enjoy.
    Sjouke Burry, Jul 31, 2007
    #7
  8. Jerry Coffin Guest

    In article <46afa934$0$25485$>,
    l says...

    [ ... ]

    > Get c-snips from
    > http://www.cs.umu.se/~isak/snippets/
    > They have code for eval.c a simple command line calculator.
    > Also a ton of other small, usefull c cource.


    Just FWIW, the home of this collection is www.snippets.org.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Aug 5, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Emel
    Replies:
    2
    Views:
    5,867
    nish a
    Jan 29, 2010
  2. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Read Input, Write Output (File) with Umlaute

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, May 26, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    2,111
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=
    May 26, 2005
  3. Krick
    Replies:
    1
    Views:
    1,455
    John C. Bollinger
    Aug 15, 2003
  4. Replies:
    3
    Views:
    392
  5. Tarkeshwar
    Replies:
    2
    Views:
    189
Loading...

Share This Page