Calculator Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • IgorZ
    New Member
    • Sep 2009
    • 11

    Calculator Question

    I made 1 calculator in C++ but it looks preatty simple. If some 1 sees it, he will tell that i made it in 2 mins... How can i change the design and looks of the C++ programs? I mean i want my calculator to look like this 1 but i don't know how to modify it to my needs. Should i try learning C# for that? I need some advise, btw im only a beginner...

    Code:

    Code:
    #include<iostream>
    #include<windows.h>
    #include<mmsystem.h>
    
    using namespace std;
    
    int main(void)
    {
     double n1;
     double n2;
     char redo;
     char operation;
     do
     {
          system("CLS"); // If a person enters y it returns the program to this line.
          system("TITLE Calculator"); // Title of Program
          system("Color B4"); // Writing Color
          cout << "Please enter your desired calculation: " << endl;
          cin >> n1 >> operation >> n2;
          switch(operation)
          {
          case '+':
                     cout << "Your result is: " << n1 + n2 << endl;
                     break;
          case '-':
                     cout << "Your result is: " << n1 - n2 << endl;
                     break;  
          case '*':
                     cout << "Your result is: " << n1 * n2 << endl;
                     break;
          case 'x':
                     cout << "Your result is: " << n1 * n2 << endl;
                     break;
          case 'X':
                     cout << "Your result is: " << n1 * n2 << endl;
                     break;
          case '/':
                     if(n2 == 0){
                     cout << "That is an invalid operation" << endl;
                     }else{   
                     cout << "Your result is: " << n1 / n2 << endl;
                     }
                     break;
          default:
              cout << "That is an invalid operation" << endl;
              break;               
          }
          cout << "Do you want to calculate something else?(Y/N)" << endl;
          cin >> redo;
     }while(redo == 'y' || redo == 'Y'); // || means or. If a person enters y or Y the program starts again and return to the system CLS line
     system("PAUSE");
     return 0;      
    }
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Are you talking about creating a GUI (graphical user interface)? If so, you do not need to use C# - check out one of the many GUI libraries available for C/C++ - a quick google will help you out (I recommend GTK).

    Here's a link to the C++ Library for GTK: http://www.gtkmm.org/

    Comment

    • IgorZ
      New Member
      • Sep 2009
      • 11

      #3
      What do u mean? How can i use them. I told you im a begginer can u give me step by step explanation? I think it will take 2 minutes of your valuable time...

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by IgorZ
        What do u mean? How can i use them. I told you im a begginer can u give me step by step explanation? I think it will take 2 minutes of your valuable time...
        First of all, my time's not that valuable - were it, I wouldn't be on here.

        Second of all, I mean: is the root of your question that you would like a graphical interface for your application? That is, do you want fancy pointy-clicky stuff for the user?

        Thirdly, I'm not a C/C++ developer - so any advice I can give wouldn't be reliable other than that I _know_ GTK+ is a very good UI library. The link I provided you has resources for documentation, downloads, help, etc. You can find them by looking - I won't do that for you.

        Mark.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          When using a GUI you normally have some sort of editor to create the GUI. In fact the video you link to shows the GUI editor for Visual C# but the GUI editor for Visual C++ is much the same and in fact most GUI editors have similar features, an area to design your Window on, a box of tools you can place on your Window.

          Once you have place tools on the Window then you add code to run when the tool is used, for instance when a button is clicked. Then you compile and link in the usual way and that is about it.

          If you drill down into the link provided by Markus you will find the documentation which explains the basics of how to use GTK

          Comment

          • IgorZ
            New Member
            • Sep 2009
            • 11

            #6
            @ Banfa: I use Dev C++ program for compiling

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Hmmm, it's a bit out of date but doesn't Dev-C++ include a GUI interface editor?

              Comment

              • whodgson
                Contributor
                • Jan 2007
                • 542

                #8
                If you are familiar with BASIC you could use MS Visual Basic to achieve your objective ( like THIS) quite simply. If you want to persevere with C++ I think as a beginner you are heading into difficult waters unless you only want to employ '+' and '-' .
                The difficulty is that if you want to enter say - 6.0 - 2.7 / 21.3 +5%2 from the keyboard and send a result -4.1268 (I hope that is correct!) to the screen you will have to deal with arithmetical precedence and probably parethisis. Bjarne Stroustrup devotes a couple of chapters (6 & 7) to this subject in his book Programming Principles and Practice Using C++.
                hth`s

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Originally posted by whodgson
                  ... you will have to deal with arithmetical precedence and probably parethisis ...
                  The classic way to handle precedence and parentheses is to construct an operator/operand stack and load it with the Reverse-Polish equivalent of the input infix expression.

                  If you're not already familiar with the terms infix and postfix then a full-blown calculator may be a bit too big a project for a beginner.

                  Comment

                  Working...