Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > File Input/Output

Reply
Thread Tools

File Input/Output

 
 
nkomli@gmail.com
Guest
Posts: n/a
 
      07-24-2007
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-24-2007
http://www.velocityreviews.com/forums/(E-Mail 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
ThreeRight ThreeRight is offline
Junior Member
Join Date: Jul 2007
Posts: 12
 
      07-25-2007
try using peek() and putback();
http://www.java2s.com/Code/Cpp/File/...inifstream.htm
 
Reply With Quote
 
nkomli@gmail.com
Guest
Posts: n/a
 
      07-31-2007
On Jul 24, 2:21 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> (E-Mail 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
> --
> 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

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-31-2007
* (E-Mail 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.

--
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?
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-31-2007
(E-Mail Removed) wrote:
> On Jul 24, 2:21 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
>> (E-Mail 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
>> --
>> 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


 
Reply With Quote
 
Sjouke Burry
Guest
Posts: n/a
 
      07-31-2007
Victor Bazarov wrote:
> (E-Mail Removed) wrote:
>> On Jul 24, 2:21 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
>>> (E-Mail 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
>>> --
>>> 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.
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      08-05-2007
In article <46afa934$0$25485$(E-Mail Removed)>,
(E-Mail Removed)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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting JPG file ppt file [Powerpoint] file zxcvar Digital Photography 7 06-22-2009 07:54 PM
Reading of file by next of map file and by next of file descriptor. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki C++ 1 07-10-2007 02:46 AM
How to create MDF file (.mdf) file from XML file. Dave ASP .Net 1 06-07-2007 11:32 PM
In file parsing, taking the first few characters of a text file after a readfile or streamreader file read... .Net Sports ASP .Net 11 01-17-2006 12:44 AM
An Automated process of watching a network file folder, reading a file in it and deleting the file using ASP.NET ? Luis Esteban Valencia Muņoz ASP .Net 3 06-04-2005 10:56 AM



Advertisments