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?