Home > Back-end >  I am here to ask on how can I store the calculation history of my calculator in c
I am here to ask on how can I store the calculation history of my calculator in c

Time:11-24

I am new to the coding community and I am here to ask on how can I store the calculations that have been done in my calculator. I am currently new in this space and I am currently looking for answers T-T. my code goes like this thank you everyone! T-T pls be gentle on me.

#include<iostream>
#include<stdlib.h>

using namespace std;

int calculator();
void history();
void choice();

int main()
{
    int x;
    cout << "\t\t\t\tWhat do you want to do?\n\n";
    cout << "\t\t\t\t[1] Standard calculator\n";
    cout << "\t\t\t\t[2] History\n";
    cout << "\t\t\t\t[3] Exit\n\n\n\n";
    cout << "\t\t\t\tChoice: ";
    cin >> x;

    switch (x)
    {
    case 1:
        calculator();
        break;
    case 2:
        history();
        break;
    case 3:
        cout << "\n\n\nThank you for using my calculator!";
        exit(4);
        break;

    default:
        cout << "Enter a correct choice";
        main();
    }
}

int calculator()
{
    double x, y;
    float sum = 0.0, dif = 0.0, prod = 1.0, quo = 1.0;
    int i;

    char op, back;
    do {
        system("CLS");
        cout << "\t\t#CALCULATOR#" << endl;
        cout << endl << endl;

        cout << "Calculate 2 numbers: (example 1   1 or 2 * 2)\n";
        cin >> x >> op >> y;

        switch (op) {

        case ' ':
            sum = x   y;
            cout << "The sum of " << x << " and " << y << " is " << sum;
            break;

        case '-':
            dif = x - y;
            cout << "The difference of " << x << " and " << y << " is " << dif;
            break;

        case '*':
            prod = x * y;
            cout << "The product of " << x << " and " << y << " is " << prod;
            break;

        case '/':
            if (y == 0)
                cout << "Undefined";
            else
                quo = x / y;
            cout << "The quotient of " << x << " and " << y << " is " << quo;
            break;

        default:
            cout << "Invalid operator";
            break;
        }

        cout << "\nContinue[Y/N]: ";
        cin >> back;
        cout << endl << endl;
        if (back != 'Y' && back != 'y' && back != 'N' && back != 'n')
        {
            cout << "Please enter a correct choice" << endl;
            choice();
        }
        else if (back == 'N' || back == 'n')
        {
            system("pause");
            system("CLS");
            main();
        }
    } while (back == 'y' || back == 'Y');

    cout << "Thank you";
    system("pause");
    system("CLS");
}

void choice()
{
    char c;
    do
    {
        cout << "Do you want to continue? [Y/y or N/n]" << endl;
        cin >> c;

        if (c == 'Y' || c == 'y')
            calculator();
        else if (c == 'N' || c == 'n')
        {
            system("pause");
            system("cls");
            main();
        }
        else
            cout << "Please enter a correct choice\n";

        choice();


    } while (c != 'y' || c != 'Y' || c != 'N' || c != 'n');
    cout << "Enter a correct choice";
}


void history()
{
    cout << "I still dont know how T - T";
}

I wanted to store the past calculations using arrays but i dont know how to actually put it in my code T-T pls help

CodePudding user response:

You want to #include <vector> and make a history std::vector, that holds strings:

std::vector<std::string> history;

When you do the calculcation, don't output to cout, but to a std::ostringstream first (located in <sstream>):

std::ostringstream caclulation_stream;
switch (op) {
case ' ':
    sum = x   y;
    caclulation_stream << "The sum of " << x << " and " << y << " is " << sum;
    break;

case '-':
    dif = x - y;
    caclulation_stream << "The difference of " << x << " and " << y << " is " << dif;
    break;

case '*':
    prod = x * y;
    caclulation_stream << "The product of " << x << " and " << y << " is " << prod;
    break;

case '/':
    if (y == 0)
        caclulation_stream << "Undefined";
    else
        quo = x / y;
    caclulation_stream << "The quotient of " << x << " and " << y << " is " << quo;
    break;

default:
    caclulation_stream << "Invalid operator";
    break;
}

this way, you can save the string to history. push_back() adds the string to the vector. Don't forget to also print out the string, so it's still displayed in the console.

auto calculation_string = caclulation_stream.str();
history.push_back(calculation_string);

std::cout << calculation_string;

to display the history, you can loop over the vector and print out the elements:

void show_history()
{
    for (const auto& entry : history) {
        std::cout << entry << '\n';
    }
}

this should give you the basic ideas on how to implement this.


Read here why you shouldn't be using namespace std;.

  • Related