I just started writing C++ and i only know the basics, if there is anyway to improve my simple caluculator or if you have any functions I could add to it let me know. Here is the code I wrote feel free to take bits.

#include <cstdlib>
#include <iostream>
#define _USE_MATH_DEFINES
#include <cmath>

using namespace std;

int main()
{
    double num;
    double num2;
    char choice;
    const double PI = 3.141592;    
    for (;;){
     do {
    cout<<"Welcome to then Calculator.\n";
    cout<<"Please choose an option by entering the number, press q to quit\n";
    cout<<"1 - Addition\n";
    cout<<"2 - Subtraction\n";
    cout<<"3 - Division\n";
    cout<<"4 - Multiplication\n";
    cout<<"5 - Square root\n";
    cout<<"6 - Area of a circle\n";

    cin>>choice;
    } while ( choice < '1' || choice > '7' && choice != 'q');
    if (choice == 'q') break;
    switch (choice) {
           case '1':
                cout<<"Please enter a number\n";
                cin>>num;
                cout<<"Another number to be added\n";
                cin>>num2;
                cout<< "Your answer is equal to  " <<num + num2;
                cout<<"\n";
                break;
           case '2':
                cout<<"Please enter a number\n";
                cin>>num;
                cout<<"Another number to be subtracted\n";
                cin>>num2;
                cout<<" Your answer is equal to  " <<num - num2;
                cout<<"\n";
                break;
           case '3':
                cout<<"Please enter a number\n";
                cin>>num;
                cout<<"Another one to be divided\n";
                cin>>num2;
                cout<< " Your answer is equal to  " <<num / num2;
                cout<<"\n";
                break;
           case '4':
                cout<<"Please enter a number\n";
                cin>>num;
                cout<<"Another one to be multiplied\n";
                cin>>num2;
                cout<< "The product of your two numbers is equal to  " << num * num2;
                cout<<"\n";                     
                break;
           case '5':
                cout<<"Please enter your number\n";
                cin>>num;
                cout<<"The Squre root of your number is equal to " << sqrt(num);    
                cout<<"\n";               
                break;
           case '6':
                cout<<"Please enter the radius of your circle in meters.\n";
                cin>>num;
                cout<<"The area of your circle is equal to "<<pow(num,2)*PI <<"M^2";
                cout<<"\n";
                break;          
                default:

                    cout<<"That is not an option";

                }

}
return 0;


}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Remember to organize, don't left align, ummm use comments // or */ /*

And read up on functions and how to make your calculator modular.

Example:

#include <cstdlib>
#include <iostream>

using namespace std;

// Prototypes
void calSlope(int, int, int, int);

int main ()
{

// Name Variables
    int firstx,
        secondx,
        firsty,
        secondy;
        
// Title
    cout << "Simple Slope Calculator" << endl;
    cout << "-----------------------" << endl;  
    
// Ask user for input
    cout << "Please input y2 and y1 with a space inbetween." << endl;
        cin >> secondy >> firsty;
        endl(cout);
        cout << "----------------" << endl;  
        endl(cout);
        cout << "Please input x2 and x1 with a space inbetween." << endl;
        cin >> secondx >> firstx;
        endl(cout);
        cout << "----------------" << endl;  
        endl(cout);

// Send information to other function
    calSlope(firstx, firsty, secondx, secondy);
    
    
    system("pause");
}



void calSlope(int xfirst, int yfirst, int xsecond, int ysecond)
{      
       int slopey,
           slopex;
// Calculate slope
        slopey = ysecond - yfirst;
        slopex = xsecond - xfirst;
// Check for negative sign location
         if (slopey < 0 && slopex < 0)
         {
            slopey *= -1;
            slopex *= -1;
         }
         else if (slopey > 0 && slopex < 0)
         {
            slopey *= -1;
            slopex *= -1;
         }       
// Display
        cout << "Your slope is " << slopey << "/" << slopex << "." << endl;
     endl(cout);
        
}
This article has been dead for over six months. Start a new discussion instead.