File Input/Output

N

nkomli

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
 
V

Victor Bazarov

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
 
N

nkomli

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


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
 
A

Alf P. Steinbach

* (e-mail address removed):
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.
 
V

Victor Bazarov

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


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
 
S

Sjouke Burry

Victor said:
(e-mail address removed) 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

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top