-1

I have recently gotten into C++ and have been making a relatively basic yet overloaded calculator to learn new concepts in c++. I am at a point where this problem cannot be ignored. I want to have it so a user can operate with more than 2 values, an example being 3+3+3.

Is it possible to this? If so, is there a guide or website to learn more about this method? Thanks in advance.

Here is the code I have for my calculator:

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <time.h>
#include <fstream>
#include <string>

using namespace std;

void display_history()
{
        string getcontent;
        ifstream calchistory("calchistory.txt");
        if(calchistory.is_open())
        {
            while (! calchistory.eof())
            {
                    getline(calchistory, getcontent);
                    cout << getcontent <<endl;
            }
    }else cout << "could not display history, does calchistory.txt exist? do you have read permissions?"<< endl;

}

int main()
{
ofstream calcHistory;
calcHistory.open("calchistory.txt", ios::app);
const long double PI = 3.141592653589793238L;
long double a, b, answer, r, area, circum;
int choice, i = 0;
char again, clear;
time_t _tm =time(NULL );

struct tm * curtime = localtime ( &_tm );
calcHistory <<asctime(curtime) << endl;
do{
cout << "What would you like to do?" << endl;
cout << "1.Addition" << endl;
cout << "2.subtraction" << endl;
cout << "3.multiplication" << endl;
cout << "4.divison" << endl;
cout << "5.powers" << endl;
cout << "6.circle calculator" << endl;
cout << "7.view history" << endl;
cout << "8.clear history" << endl;
cout << "9.to quit" << endl;
cout << "choice: ";
cin >> choice;


switch(choice){
    case 1:
i = i+1;
    cout << "enter two numbers you want to use(press enter after you enter each number): ";
    cout << endl;
    cin >> a >> b;
    answer = a + b;
    cout << "the answer is: " << a << " + " << b << " = " << answer << endl;
calcHistory << i<<": " << a <<" + " <<b <<" = "<< answer << endl<<endl;
    break;
    case 2:
i = i+1;
    cout << "enter two numbers you want to use(press enter after you enter each number): ";
    cout << endl;
    cin >> a >> b;
    answer = a - b;
    cout << "the answer is: " << a << " - " << b << " = " << answer << endl;
    calcHistory << i<<": "<<a <<" - " <<b <<" = "<< answer << endl<<endl;
    break;
    case 3:
i = i+1;
    cout << "enter two numbers you want to use(press enter after you enter each number): ";
    cout << endl;
    cin >> a >> b;
    answer = a * b;
    cout << "the answer is: " << a << " * " << b << " = " << answer << endl;
    calcHistory <<i<<": "<<a <<" * " <<b <<" = "<< answer << endl<<endl;
    break;
    case 4:
i = i+1;
    cout << "enter two numbers you want to use(press enter after you enter each number): ";
    cout << endl;
    cin >> a >> b;
    answer = a / b;
    cout << "the answer is: " << a << " / " << b << " = " << answer << endl;
    calcHistory <<i<<": "<<a <<" / " <<b <<" = "<< answer << endl<<endl;
    break;
    case 5:
i = i+1;
    cout << "please enter two numbers you want to find the power of (1st number is base 2nd number is exponent)" << endl;
    cout << "be sure to press enter after each number" << endl;
    cin >> a >> b;
    answer = pow(a, b);
    cout << "the power of " << a << " and " << b << " is " << answer << endl;
    calcHistory <<i<<": "<<a <<" ^ " <<b <<" = "<< answer << endl<<endl;
    break;
case 6:
i = i+1;
    cout<<"Enter the radius of the circle :";
    cin>>r;
    area=PI*r*r;
    circum=2*PI*r;
    cout<<"Area of the circle = "<<area<<"\nCircumference of the circle = "<<circum<<"\n";
    calcHistory <<i<<": "<< "Area of the circle = "<<area<<"\nCircumference of the circle = "<<circum<<"\n"<<endl;
break;
    case 7:
    display_history();
    break;
case 8:
cout << "are you sure you want to do this? this action cannot be undone. (y/n) ";
    cin >> clear;
    if(clear == 'Y'||clear =='y')
    {
    if(calcHistory.is_open())
        {
         calcHistory.close();
         calcHistory.open("calchistory.txt", ios::trunc);
         calcHistory.close();
         cout << "history cleared!" << endl;
        }
}
break;
    case 9:
    if (calcHistory.is_open())
    {
    cout << "session saved in file calchistory.txt" << endl;
    }else cout << "could not write to file, do you have write permissions?" << endl; 
    cout << "exiting...." << endl << endl;
    cin.get();
    calcHistory.close();
    return 0;
    break;
default:
    cout << choice <<" is not a valid option" << endl;
    break;
}
cout << "would you like to continue? (y or n) ";
cin >> again;
cout << endl;
}while(again == 'y' ||again == 'Y');

cin.get();
if (calcHistory.is_open())
{
cout << "session saved in file calchistory.txt" << endl;
}else cout << "could not write to file calchistory.txt, do you have write permissions?" << endl;
cout << "exiting...."<< endl << endl;
calcHistory.close();
return 0;
}

PS: Tips to make my code more efficient or use less lines?

  • 2
    To answer the question asked, yes it is possible to do so, [and here's the guide/website to learn more about](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Apr 22 '17 at 01:07
  • First step: respect [indentation](https://en.wikipedia.org/wiki/Indent_style) and standard code formatting. Also, I think you can ask such of question on [code review](https://codereview.stackexchange.com/) – Garf365 Apr 25 '17 at 08:40

1 Answers1

0

I remember when I was also a bit new to c++.

I also tried making Calculator program, which can take many operation user wants.

Good news, I always save my code to dropbox.

After searching for 10 min I at last Found It,

I know my code is not neat, as it was made year ago when I was just new to programming,

BUT still, you can take reference from it.

 #include<iostream>
int main() {
float number;
char symbol;
float solution = 0;
std::cin >> solution;
while (true) {
    std::cin >> symbol;
    if (symbol == '=') {
        std::cout << solution;
        break;
    }
    std::cin >> number;
    if (symbol == '+') {
        solution += number;
    }
    if (symbol == '-') {
        solution -= number;
    }
    if (symbol == '*') {
        solution *= number;
    }
    if (symbol == '/') {
        solution /= number;
    }

}
return 0;
}
Prashant Bhardwaj
  • 86
  • 1
  • 2
  • 11