Calculator

Discussion in 'C++' started by Max, Jul 2, 2007.

  1. Max

    Max Guest

    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!
     
    Max, Jul 2, 2007
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Jul 2, 2007
    #2
    1. Advertising

  3. 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
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jul 2, 2007
    #3
  4. Max

    osmium Guest

    "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;
    > }
     
    osmium, Jul 2, 2007
    #4
  5. Max

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Jul 2, 2007
    #5
  6. 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
     
    Juha Nieminen, Jul 3, 2007
    #6
    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. makko

    calculator

    makko, Feb 28, 2005, in forum: Perl
    Replies:
    2
    Views:
    2,959
    makko
    Mar 1, 2005
  2. =?Utf-8?B?bWFoc2E=?=

    Re: distance calculator

    =?Utf-8?B?bWFoc2E=?=, Jun 4, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    375
    =?Utf-8?B?bWFoc2E=?=
    Jun 5, 2004
  3. Maria Laura Re

    URGENT Help With Scientific Calculator!

    Maria Laura Re, Dec 13, 2003, in forum: Java
    Replies:
    4
    Views:
    518
    Andrew Thompson
    Dec 14, 2003
  4. Ike

    Calculator Applet

    Ike, Feb 18, 2004, in forum: Java
    Replies:
    1
    Views:
    431
    Andrew Thompson
    Feb 19, 2004
  5. Mc Kiernan, Daniel Kian

    Freeware Class File Programmer's Calculator

    Mc Kiernan, Daniel Kian, Jul 17, 2004, in forum: Java
    Replies:
    0
    Views:
    480
    Mc Kiernan, Daniel Kian
    Jul 17, 2004
Loading...

Share This Page