Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (0)

Powered by Vanilla. Made with Bootstrap.
[C++] Maths Machine [basic]
  • Sh3llc0d3
    Posts: 1,910
    Another basic console program I made, kinda good for lazy people who need this stuff... It computes basic mathematical equations [+,-,/,*] it does squareroots & pythagoras theory. Mainly did it to make sure the basics were still sticking in :)

    Kind of a mish mash of previous programs ive done, I hope to make the simple math section optimized by using switches when I plough through the learning lol.

    /*
    Maths Machine v0.1 by Semtex-Primed :)
    --------------------
    -- 1.simple maths --
    -- 2.Square-roots --
    -- 3.pythagoras --
    -- --
    -- Enjoy --
    --------------------
    */
    #include <iostream>
    #include <cmath>

    int main()
    {
    //Variables
    char name[20];
    int func;
    char op;
    double num1, num2, num3, answer, answer2, answer3, answer4, answer5;

    std::cout << \"Enter first name: \";
    std::cin >> name;
    //menu of maths machine
    Menu:
    std::cout << \"Welcome to the Maths Machine \" << name << \".\n\";
    std::cout << \"1. Simple maths.\" << \"\n\";
    std::cout << \"2. Squareroots.\" << \"\n\";
    std::cout << \"3. Pythagoras theory.\" << \"\n\";
    std::cout << \"Enter the function you wish to use [1,2,3]: \";
    std::cin >> func;

    //simple maths function
    if(func == 1)
    {
    std::cout << \"Welcome to Maths Machine: Simple Maths\n\";
    std::cout << \"Enter the first number: \";
    std::cin >> num1;
    std::cout << \"Enter the second number: \";
    std::cin >> num2;
    std::cout << \"Enter operator [+,-,*,/]: \";
    std::cin >> op;

    //addition
    if(op == '+')
    {
    answer = num1 + num2;
    std::cout << \"The Answer to \" << num1 << op << num2 << \" = \" << answer;
    return 0;
    }
    //subtraction
    if(op == '-')
    {
    answer2 = num1 - num2;
    std::cout << \"The Answer to \" << num1 << op << num2 << \" = \" << answer2;
    return 0;
    }
    //multiplication
    if(op == '*')
    {
    answer3 = num1 * num2;
    std::cout << \"The Answer to \" << num1 << op << num2 << \" = \" << answer3;
    return 0;
    }
    //division
    if(op == '/')
    {
    answer4 = num1 / num2;
    std::cout << \"The Answer to \" << num1 << op << num2 << \" = \" << answer4;
    return 0;
    }
    }
    //squareroot menu
    if(func == 2)
    {
    std::cout << \"Welcome to Maths Machine: Squareroots\n\";
    std::cout << \"Enter the number you want to see the square root of: \";
    std::cin >> num1;

    answer = sqrt(num1);
    std::cout << \"The square root of \" << num1 << \" = \" << answer;
    return 0;
    }
    //pythagoras theory
    if(func == 3)
    {
    std::cout << \"Welcome to Maths Machine: Pythagoras theorum\n\";
    Again:
    std::cout << \"Enter length of the longest side leading away from the 90-degree angle: \";
    std::cin >> num1;

    if(num1 <= 0)
    {
    std::cout << \"Sorry, the inputted number for the longest side cannot be correct, enter a number above 0\" << \"\n\";
    goto Again;
    }

    Again2:
    std::cout << \"Enter length of the shorter side leading away from the 90-degree angle: \";
    std::cin >> num2;

    if(num2 <= 0)
    {
    std::cout << \"Sorry, the inputted number for the shortest side cannot be correct, enter a number above 0\" << \"\n\";
    goto Again2;
    }
    answer = sqrt(num1*num1 + num2*num2);

    std::cout << \"The length of your Hypotenuse is: \" << answer;
    return 0;
    }
    }
  • You should try to use switch case, helps the case instead of so many 'if else' statements :D
  • Sh3llc0d3
    Posts: 1,910
    /*
    Maths Machine v0.3 - Same features just
    more efficient... maybe lol
    */
    #include <iostream>
    #include <cmath>
    #include <string>

    void pause()
    {
    using std::cout;
    using std::cin;

    cout << \"Press \'Enter\' key to continue...\";
    cin.sync ();
    cin.clear();
    cin.get();
    return;
    }

    int main()
    {
    using std::cout;
    using std::cin;

    //variables
    std::string name;
    char op, choice;
    double num1, num2, num3, answer;

    //name
    cout << \"Enter your name: \";
    getline(std::cin, name);

    //menu
    cout << \"Welcome to Maths Machine \" << name << \"\n\n\";
    for(;;)
    {
    do
    {
    //Options
    cout << \"Maths Machine v0.3 Menu\n\";
    cout << \" 1. Simple Maths\n\";
    cout << \" 2. Square Roots\n\";
    cout << \" 3. Pythagoras\n\";
    cout << \"Enter a choice ('q' to quit): \";
    cin >> choice;

    } while( choice < '1' || choice > '3' && choice != 'q');

    //q = quit
    if(choice == 'q') break;

    cout << \"\n\n\";

    switch(choice)
    {
    case '1': //Option 1, Simple maths
    //Enter your numbers and operator for the calculation
    cout << \"Welcome to Simple Maths brought to you within the Maths Machine\n\";
    cout << \"Enter your first number: \n\";
    cin >> num1;
    cout << \"Enter your Operater (*,/,+,-): \n\";
    cin >> op;
    divretry:
    cout << \"Enter you second number: \n\";
    cin >> num2;

    //addition
    if(op == '+')
    {
    //calculation
    answer = num1 + num2;
    //answer
    cout << \"The Answer to \" << num1 << op << num2 << \" = \" << answer << \"\n\";
    break;
    }
    //subtraction
    if(op == '-')
    {
    //calculation
    answer = num1 - num2;
    //answer
    cout << \"The Answer to \" << num1 << op << num2 << \" = \" << answer << \"\n\";
    break;
    }
    //multiplication
    if(op == '*')
    {
    //calculation
    answer = num1 * num2;
    //answer
    cout << \"The Answer to \" << num1 << op << num2 << \" = \" << answer << \"\n\";
    break;
    }
    //division
    if(op == '/')
    {
    //cannot divide 1st number with zero error
    if(num2 <= 0)
    {
    cout << \"Sorry but you cannot have a number equal or less than zero [0] as you're second number.\n\";
    goto divretry;
    }
    //calculation
    answer = num1 / num2;
    //answer
    cout << \"The Answer to \" << num1 << op << num2 << \" = \" << answer << \"\n\";
    }
    case '2': //Option 2, Ssquare roots
    cout << \"Welcome to Square Roots brought to you within the Maths Machine\n\";
    //enter number you wish to find the square root of...
    cout << \"Enter the number you want to find the square root of: \n\";
    cin >> num1;

    //calculation
    answer = sqrt(num1);

    //answer
    cout << \"The square root of \" << num1 << \" = \" << answer << \"\n\";
    break;
    case '3': //Option 3, Pythagoras
    cout << \"Welcome to Pythagoras brought to you within the Maths Machine\n\";
    again:
    //enter number of the longest side
    cout << \"Enter the length of the longest side running away from the 90 degree angle: \n\";
    cin >> num1;
    again2:
    //enter length of the smallest side
    cout << \"Enter the length of the smaller side leading away from the 90 degree angle: \n\";
    cin >> num2;

    //error 1, longest side is too short
    if(num1 <= 0 || num1 <= num2)
    {
    cout << \"Sorry, the inputted number for the longest side cannot be correct, enter a number above 0 and longer than the second side\" << \"\n\";
    goto again;
    }
    //error 2, shortest side is too long
    if(num2 <= 0 || num2 >= num1)
    {
    cout << \"Sorry the inputter number for the smallest side cannot be larger than the first or equal '0'!\" << \"\n\";
    goto again2;
    }

    /*
    the pythagoras theory states that the length of
    a(the hypotenuse) = b(squared) + c(squared). But
    for our program we don't want the user to be left
    with the square number of the length. So we add the
    sqrt() function to automatically give the base
    length for the hypotenuse.
    */
    answer = sqrt((num1*num1) + (num2*num2));

    //answer
    cout << \"The length of the hypotenuse is \" << answer << \"\n\";
    break;
    }
    cout << \"\n\n\";
    }
    pause();
    return 0;
    }

    New code, however goto's are still in this version, I had a newer & better version 0.4 but it's on my laptop and cannot be bothered to look for it to be honest.
    S-P

    note: this is still an old code sample, due to the course i'm on at the moment I have had to learn vb.NET so haven't had time to finish projects. I have a hacking game (vb.NET) i'm working on and another app which I'll be releasing to people from the UK soon.
  • Xin
    Posts: 3,251
    Great share again semtex :) keep em coming
    Xin
  • Bursihido
    Posts: 406
    good work and thanks semtex-primed :)
  • undead
    Posts: 822
    nice share! i'm sure it will help many members here. ;)