Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calculator

Reply
Thread Tools

Calculator

 
 
Max
Guest
Posts: n/a
 
      07-02-2007
I am working on a calculator program. It is very simple (in
implementation, not use), but I am having trouble with getting input.
How can I take input and run it. I should note that I want all of the
math available in C++ to be available to the user (casting, shift,
etc.). Can anyone help me.
--
Here's the code:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#define newl "\n"
using namespace std;

int main(int argc, char *argv[]);

string i = "";

int main(int argc, char *argv[]){
cout << "? ";
cin >> i;
cout << i << newl;
cout << "";
getchar();
return 0;
}

AdvTHANKSance!

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-02-2007
Max wrote:
> I am working on a calculator program. It is very simple (in
> implementation, not use), but I am having trouble with getting input.
> How can I take input and run it.


Not sure what you mean by "run it".

> I should note that I want all of the
> math available in C++ to be available to the user (casting, shift,
> etc.). Can anyone help me.


It's not as simple as you might want to think. Programming an interpreter
(that's what it sounds like) is a complicated task. I recommend starting
with Chapter 6 of TC++PL.

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
 
 
 
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      07-02-2007
On 2007-07-02 19:18, Max wrote:
> I am working on a calculator program. It is very simple (in
> implementation, not use), but I am having trouble with getting input.
> How can I take input and run it. I should note that I want all of the
> math available in C++ to be available to the user (casting, shift,
> etc.). Can anyone help me.
> --
> Here's the code:
>
> #include <cstdio>
> #include <cstdlib>


Do you use any of those?

> #include <iostream>
> #include <cmath>


Or this one?

> #define newl "\n"


Don't do that. C++ has the wonderful keyword const for just such things.
Replace with

const char newl = '\n';

> using namespace std;
>
> int main(int argc, char *argv[]);


You don't need a prototype of main.

>
> string i = "";


You have not included <string>.

> int main(int argc, char *argv[]){


If you don't plan to use the arguments then just 'int main()' will do
just fine.

> cout << "? ";
> cin >> i;
> cout << i << newl;
> cout << "";
> getchar();
> return 0;
> }


How simple is simple? The simplest reads in the operation first and then
asks for operands, which is easily implemented with a switch-statement.

--
Erik Wikström
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      07-02-2007
"Max" write:

>I am working on a calculator program. It is very simple (in
> implementation, not use), but I am having trouble with getting input.
> How can I take input and run it. I should note that I want all of the
> math available in C++ to be available to the user (casting, shift,
> etc.). Can anyone help me.


I think you should back off and do some of the exercises in the first few
chapters of a C++ book before you do this.

> --
> Here's the code:
>
> #include <cstdio>
> #include <cstdlib>
> #include <iostream>


Choose an I/O method and use *it*, not two vastly differnt ways, C and C++

> #include <cmath>
> #define newl "\n"


Are you aware of the difference between a char and a string?

> using namespace std;
>
> int main(int argc, char *argv[]);


Why that complicated form? Why a prototype?

>
> string i = "";


string knows how to make a default ctor, use it.

> int main(int argc, char *argv[]){
> cout << "? ";
> cin >> i;


i is a very bad name for a sting. You can name a boy Shirley, but it is
still not a good idea.

> cout << i << newl;
> cout << "";


If you want a blank line, the usual way is to write

cout << '\n';

> getchar();
> return 0;
> }



 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      07-02-2007
Max wrote:

> I am working on a calculator program. It is very simple (in
> implementation, not use), but I am having trouble with getting input.
> How can I take input and run it.


It's not fully clear what your mean. Should your problem be to read a whole
line of input instead of just all characters to the next white space, then
you might want to read up on std::getline().

> I should note that I want all of the
> math available in C++ to be available to the user (casting, shift,
> etc.). Can anyone help me.


[snip]

> int main(int argc, char *argv[]);
>
> string i = "";


Why global?

>
> int main(int argc, char *argv[]){
> cout << "? ";
> cin >> i;


This will stop at the first white space.

while ( std::getline( std::cin, i ) ) {
}

will read whole lines of input one by one.

> cout << i << newl;
> cout << "";
> getchar();
> return 0;
> }



Best

Kai-Uwe Bux

 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      07-03-2007
Max wrote:
> How can I take input and run it.
> I should note that I want all of the
> math available in C++ to be available to the user (casting, shift,
> etc.). Can anyone help me.


You can't. C++ creates a binary. It doesn't create an interpreter.

There exist libraries which can do what you want, for example
this one: http://iki.fi/warp/FunctionParser/
However, if your understanding of C++ is minimal, it may be quite
a task to write a program which uses a library like that, even though
using it is relatively simple.

There's a simple example program on how to use the library there:

http://iki.fi/warp/FunctionParser/example.cc
 
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
calculator makko Perl 2 03-01-2005 06:06 AM
Re: distance calculator =?Utf-8?B?bWFoc2E=?= ASP .Net 3 06-05-2004 09:31 PM
Calculator Applet Ike Java 1 02-19-2004 06:41 AM
URGENT Help With Scientific Calculator! Maria Laura Re Java 4 12-14-2003 05:59 AM
bandwidth usage calculator Sai Cisco 5 11-07-2003 08:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57