Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: small C++ calculator!!

  1. #1
    dox
    Guest

    small C++ calculator!!

    hi guys

    been playing a little with c++

    try to make a small calculator but didnt work!! i hope u can tell me what i did wrong and what for possibility there are!! what i try is that the user enter "4+8" for example and then comes the result!!

    Code:
    #include <iostream.h>
    
    
    int main()
    {
    int calculation;
    
    cout << "Enter your calculation using + - / * \n";
    
    cin >> calculation;
    
    cout << "The result is: " << calculation;
    
    return 0;
    
    }
    thanks

  2. #2
    Senior Member
    Join Date
    Nov 2001
    Posts
    304

    Re:small C++ calculator!!

    Ok...
    First things first:

    #include <iostream.h>
    is outdated... the new standard is this:

    #include <iostream>

    The problem with the new standard is, you need to do a "using" declaration for items you want to use from the includes. An example follows:

    using std::cin;
    using std::cout;
    using std::endl;

    Some people teach you to use the following instead:
    using namespace std;

    The if you do:
    #include <iostream>
    using namespace std;

    you are doing the equivalent of the old style:
    #include <iostream.h>

    This is not normally desirable. People can argue this, but I've already defended my stance on this syntax in a previous thread.

    Now for the problem you asked about:

    when you do "cin >> calculation" all it grabs is the first input that can fit inside the variable. In other words... when the user types "4 + 5" all that gets stored is the 4...

    What you'll need to do is learn to grab a string from the command line (or learn to grab one character at a time until you reach a newline...)

    Calculators aren't as easy as you would think... the easiest calculator to implement is a reverse polish notation calculator which uses a stack to compute the answer...

    If you can write that, you can write a program to convert the input into reverse polish notation (again using a stack) and then use your RPN calculator to do the computation......

  3. #3
    Senior Member
    Join Date
    Nov 2001
    Posts
    304

    Re:small C++ calculator!!

    Now... having said all of that........

    Following is a not-very-robust calculator to do what you were trying to do... it will ONLY do calculations on "number operator number" input... so if you type "4 + 7" it will generate "11"...

    Code:
    #include <iostream>
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main( void ) {
    int i, j, calculation;
    char c;
    cout << "Enter your calculation using + - / *" << endl;
    cin >> i;
    cin >> c;
    cin >> j;
    cout << "The result is: ";
    switch ( c ) {
    case '+': {
    calculation = i + j;
    cout << calculation << endl;
    }
    break;
    case '-': {
    calculation = i - j;
    cout << calculation << endl;
    }
    break;
    case '*': {
    calculation = i * j;
    cout << calculation << endl;
    }
    break;
    case '/': {
    if ( j == 0 ) {
    cout << "Error: divide by zero" << endl;
    } else {
    if ( i < j ) {
    cout << "0" << endl;
    } else {
    calculation = i / j;
    cout << calculation;
    calculation = i % j;
    if ( calculation != 0 ) {
    cout << " remainder " << calculation;
    }
    cout << endl;
    }
    }
    }
    break;
    default: {
    cout << "Error: " << c << " is not an acceptable operator" << endl;
    }
    break;
    }
    return 0;
    }

  4. #4

    Re:small C++ calculator!!

    i made a calculator in c but i suppose that doesnt help you too much...
    AMD Athlon XP 2500+<br />DFI NFII Ultra Infinity(gigabyte sucks)<br />OCZ 2 x 512 PC3200<br />ATI Radeon AIW 9600 PRO<br />16x DVD-Rom (not in use)<br />LG 8x DVD-Burner<br />LG 40x12x40 CD-RW<br />WD 120GB<br />420W PSU<br />Tweaked out case<br />:)

  5. #5
    dox
    Guest

    Re:small C++ calculator!!

    thanks for your replys!

    well youre calc helps me understand a little more about the if and else thing!! ;D

    what do you think about C++ as my first language???

    well the small online tutorials on c++ i read had #include <iostream.h> in it, thats why i used it!! can you explain me what difference it makes whether one uses the old way or the newer way you mentioned?

    why they change the standard? does it make any difference/sence?

    the newer way seems to make it more difficult and thats all. probably c++ īs inventor was bored and desided to change standards :P :P

    oh do you know of any resource where there are small C++ samples for learning purpose!!

    thanks in advance

  6. #6

    Re:small C++ calculator!!

    i suppose the newer one allows for you to load only what you need into a program so it takes less memory.
    AMD Athlon XP 2500+<br />DFI NFII Ultra Infinity(gigabyte sucks)<br />OCZ 2 x 512 PC3200<br />ATI Radeon AIW 9600 PRO<br />16x DVD-Rom (not in use)<br />LG 8x DVD-Burner<br />LG 40x12x40 CD-RW<br />WD 120GB<br />420W PSU<br />Tweaked out case<br />:)

  7. #7
    Senior Member
    Join Date
    Nov 2001
    Posts
    304

    Re:small C++ calculator!!

    The old style of includes is a remnant of the C language. The old style didn't use namespaces at all, which made it possible to have naming conflicts between variables and functions if you included two different files that used the same naming schemes for global variables or functions. The new method uses namespaces to keep things organized. The old method included everything by default (by the standard) which could cause your programs to be much larger than necessary. The new standard (which was designed by a standards commitee) uses namespaces to allow you to pick and choose individual pieces of the namespace you want to include. With the old style:
    #include <iostream.h>
    everything in the std namespace (yes it existed, but was fairly useless) was included

    A lot of (lazy) people use the new style:
    #include <iostream>
    using namespace std;
    which also includes everything in the std namespace.

    The problem is, not every program you write will make use of cin, cout, endl, ios, ifstream, ofstream, etc..... the list goes on... all of those things belong to the namespace "std"

    That said... MOST C++ compilers using the old style were smart enough to ONLY include what you used in your program (but the standard for C++ didn't have any convention for this...)

    As for "why do it now?" If you write your program using the old style, it will not compile on many new C++ compilers... so if you want your code to be portable, you should get used to the new style. The old style will NOT compile on Visual Studio .NET 2003 for example... you HAVE to use the new style if you use that compiler (I know... bad example, but here at my school, our programs have to compile on that compiler, because that's what the instructors use to test our programs)

    So far as I know, the GNU compiler still knows what to do if it sees old style includes... but that may change in the future...

    Personally, I like the new style better, because it makes the code more readable (from a "where do you get the 'cin' from" standpoint)

    I hope I've helped clarify rather than confuse you more. The reason you see a lot of tutorials using the old style, is the new style isn't really that old... it was released a few years ago.

  8. #8

    Re:small C++ calculator!!

    well i was right on part of it.
    AMD Athlon XP 2500+<br />DFI NFII Ultra Infinity(gigabyte sucks)<br />OCZ 2 x 512 PC3200<br />ATI Radeon AIW 9600 PRO<br />16x DVD-Rom (not in use)<br />LG 8x DVD-Burner<br />LG 40x12x40 CD-RW<br />WD 120GB<br />420W PSU<br />Tweaked out case<br />:)

  9. #9

    Re:small C++ calculator!!

    Dear god my head hurts. I sooooooooo do not miss C++ at all. It was crap like that that always pissed me off. C++ is a good language to learn don't get me wrong, and if your going to possibly make a career out of programming it is a necessity, but I do warn you, learning it yourself off of online tutorials is suicide. You are going to learn BAD BAD habits from every tutorial I have seen on the net. Best option is either pick an easier language and learn from the manual(ahem php or python, both of these will ease your transition into C++ later on, ESPECIALLY php), or you need to go out and buy about 5 different books on C++ and start to reading and typing. Do the hello world program, then edit it a little bit, do everything step by step. In all honesty and Gax my argue me on this, but I've been around the block with it too, if you miss one step in the learning process of C++ you could be royally screwed. Every one of those learning steps is essential to what your going to be doing in just about anything, so take each step slowly and carefully and make sure to absorb, because it can be a bitch going back to 1st grade after graduating high school.

    Aragorn
    If you give a man a fire he'll be warm, if you light the man on fire he'll be warm for life.

  10. #10
    Senior Member
    Join Date
    Nov 2001
    Posts
    304

    Re:small C++ calculator!!

    C (and by extension C++) are very very powerful languages... they give you high level tools to do things quickly, but give you enough low level control (rope) to hang yourself easily.

    As Aragorn mentioned, it is very difficult to learn from online tutorials. And as he mentioned, you can learn a lot of bad habits (and bad coding practices) from them as well. This is why I emphasize doing it the right way (with individual using statements) whenever I see someone ask a question, and then post code using the old format, or "using namespace std;"

    I'm willing to help you learn C and C++ if you want to continue learning it. Just be ready for criticism I do try to explain why something is "BAD(TM)" and "GOOD(TM)" if I see something like that, but if I don't explain myself well enough, you'll need to let me know so I can reword the explanation And it would be easier to answer questions in IRC, but I'm not there very often. I'm working on getting my bot that has a paging function back into the room, or adding the paging function to the one that is already in the room. I just haven't had the time to fix them yet

Similar Threads

  1. Working into a small LAN
    By toastrack in forum Linux - Hardware, Networking & Security
    Replies: 1
    Last Post: 11-10-2006, 02:55 PM
  2. setting up small lan help plz
    By waterborne in forum Linux - Hardware, Networking & Security
    Replies: 6
    Last Post: 09-07-2006, 05:01 AM
  3. A small network in a lab
    By Anonymous in forum Linux - General Topics
    Replies: 2
    Last Post: 05-29-2004, 11:34 PM
  4. What is the best graphing calculator??
    By in forum General Chat
    Replies: 3
    Last Post: 11-08-2003, 07:55 PM
  5. Help - I need to make a calculator
    By ph34r in forum Linux - Software, Applications & Programming
    Replies: 2
    Last Post: 12-07-2001, 11:12 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •