Thread: How is my code ? calculator c++

  1. #1
    Registered User danielcplusplus's Avatar
    Join Date
    Aug 2014
    Location
    earth
    Posts
    14

    How is my code ? calculator c++

    Hey, i started learning C++ week ago, and having fun , anyway i wrote a simple C++ Calculator it works ^^, just wondering, how is my c++ code ? i mean from a professional perspective..
    this is how it looks: Gyazo - ac0f873000fd200013e860f00873fec1.png
    Code:
    // DECLARATIONS ---------------------------
    #include <iostream>
    #include <string>
    #include <conio.h>
    #include <fstream>
    // FUNCTION DECLARATIONS ------------------
    bool DRAW_INTERFACE();
    int math();
    // MAIN FUNCTION --------------------------
    int main() {
        if (!DRAW_INTERFACE()) return 0;
        _getch();
        math();
        return 0;
        std::cin.get();
        system("pause");
    }
    // DRAW INTERFACE -------------------------
    bool DRAW_INTERFACE() {
        std::cout<<"Checking INTERACE.TXT... ";
        std::ifstream FILE("INTERFACE.TXT");
        if (!FILE.good()) {
            std::cout<<"ERROR: FILE NOT FOUND.";
            return false;
        }
        else {
            system("cls");
            while (!FILE.eof()) {
                std::string FILE_LINE;
                std::getline(FILE, FILE_LINE);
                std::cout<<FILE_LINE<<std::endl;
            }
        }
        return true;
    }
    // MATH OPERATION -------------------------
    int math() {
        float a, b, result;
        char operation;
        std::cin >> a >> operation >> b;
        switch (operation) {
            case '+' :
                result = a + b;
                break;
            case '-' :
                result = a - b;
                break;
            case '*' :
                result = a * b;
                break;
            case '/' :
                result = a / b;
                break;
            default :
                std::cout << "Try entering an exercise for example( 16 * 16 )" << std::endl;
        }
        std::cout<<"Your answer is: "<<result<<std::endl;
    
    
        system("pause");
        return true;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think you should just make the header part of the program data. You won't lose it this way.
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    void DrawInterface()
    {
        static const char *FancyHeader = 
        "_________        .__               .__          __                \n"
        "\\_   ___ \\_____  |  |   ____  __ __|  | _____ _/  |_  ___________ \n"
        "/    \\  \\/\\__  \\ |  | _/ ___\\|  |  \\  | \\__  \\\\   __\\/  _ \\_  __ \\\n"
        "\\     \\____/ __ \\|  |_\\  \\___|  |  /  |__/ __ \\|  | (  <_> )  | \\/\n"
        " \\______  (____  /____/\\___  >____/|____(____  /__|  \\____/|__|   \n"
        "        \\/     \\/          \\/                \\/                  \n"
        ;
        static const char *Version = "v1.0";
        static const char *Author = "Daniel";
        cout<<FancyHeader<<'\n'<<"Calculator "<<Version<<" - Coded by: "<<Author<<'\n';
        cout<<setfill('_')<<setw(79)<<'\n';     // line
        cout<<setfill(' ');                     // back to normal
    }
    This function works. I will admit it is easier to dump that stuff into a file but it isn't too hard to encode as a string. Just remember to replace every backslash with \\. You can do that with a global search and replace.

    As for the rest of the code:

    Code written after a return statement will not be executed. Your compiler output should tell you about this.

    Be careful what you name things. I think your convention is confusing. Nothing you capitalized is a constant. FILE is a word already in use by the stdio library. FILE_LINE is dangerously close to __FILE__ and __LINE__ that are already defined.

    Do not ask for text input without some sort of prompt.

    Effort should be spent on how the program recovers when the user sends bad input. Imagine a user writes his algebra homework, or maybe "one plus two"! Consider how this FAQ applies here.

    Finally, I'm not convinced you need to use conio at all. system("pause"); isn't a great function but it will do what you are asking _getch() to do.
    Last edited by whiteflags; 08-30-2014 at 09:03 PM.

  3. #3
    Registered User danielcplusplus's Avatar
    Join Date
    Aug 2014
    Location
    earth
    Posts
    14
    Huge thanks for your feedback, i didn't quite understood when you said: "Code written after a return statement will not be executed." what do you mean

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Look at your main() function again. On line 14, you return 0. This ends the function, so the two lines after that will not be reached.

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    17
    I'm a newbie but well done. Prior programming experience outside of c++?

  6. #6
    Registered User danielcplusplus's Avatar
    Join Date
    Aug 2014
    Location
    earth
    Posts
    14
    Java, web development..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic Calculator code help
    By habbalock in forum C Programming
    Replies: 4
    Last Post: 01-16-2012, 11:47 AM
  2. need help with calculator code for c
    By cowboyz209 in forum C Programming
    Replies: 10
    Last Post: 04-30-2011, 10:03 PM
  3. Calculator code help
    By morpheous11 in forum C Programming
    Replies: 4
    Last Post: 12-15-2008, 08:34 PM
  4. help in calculator code
    By ctrainer in forum C++ Programming
    Replies: 4
    Last Post: 04-19-2007, 04:23 PM
  5. code for hexadecimal calculator
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-20-2002, 09:53 PM