Calculator

M

Max

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!
 
V

Victor Bazarov

Max said:
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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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 said:
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.
 
O

osmium

"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';
 
K

Kai-Uwe Bux

Max said:
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
 
J

Juha Nieminen

Max said:
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
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top