0% found this document useful (0 votes)
18 views10 pages

Basic Calculator with User History

The document is a C++ program for a basic calculator that allows users to perform arithmetic operations and save their calculation history. It includes features such as user authentication, sound effects for actions, and a simple text-based interface. Users can view their calculation history and exit the program with a saving animation.

Uploaded by

dizonivy1807
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views10 pages

Basic Calculator with User History

The document is a C++ program for a basic calculator that allows users to perform arithmetic operations and save their calculation history. It includes features such as user authentication, sound effects for actions, and a simple text-based interface. Users can view their calculation history and exit the program with a saving animation.

Uploaded by

dizonivy1807
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include <iostream>

#include <fstream>
#include <vector>
#include <string>
#include <windows.h>
#include <cmath>

using namespace std;

// ================= GLOBALS =================


string currentUser;
vector<string> history;

// ================= SOUNDS ==================


void soundType() { Beep(800, 30); }
void soundOk() { Beep(1200, 80); }
void soundError() { Beep(400, 200); }
void soundExit() { Beep(600, 100); Beep(400, 150); }

// ================= COLORS ==================


void color(int c) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

// ================= ANIMATION =================


void typePrint(const string& text, int delay = 30) {
for (char c : text) {
cout << c;
soundType();
Sleep(delay);
}
cout << endl;
}

// ================= ACCOUNT =================


bool userExists(const string& username) {
ifstream file(username + "_history.txt");
return [Link]() && [Link]() != ifstream::traits_type::eof();
}

void loadHistory() {
[Link]();
ifstream file(currentUser + "_history.txt");
string line;
while (getline(file, line)) {
history.push_back(line);
}
}
void saveHistory(const string& entry) {
history.push_back(entry);
ofstream file(currentUser + "_history.txt", ios::app);
file << entry << endl;
}

// ================= WELCOME =================


void welcome() {
system("cls");
color(11);
cout <<
"=====================================\n"
" WELCOME TO CODING CALCULATOR \n"
"=====================================\n\n";

color(14);
cout << "Enter username: ";
getline(cin, currentUser);

bool returningUser = userExists(currentUser);


loadHistory();

color(10);
if (returningUser) {
typePrint("Welcome back, " + currentUser + "!");
} else {
typePrint("Hello " + currentUser + "! Your calculator is ready.");
}

soundOk();
Sleep(800);
}

// ================= CALC LOGIC =================


double evaluate(vector<double>& nums, vector<char>& ops) {
double res = nums[0];
for (size_t i = 0; i < [Link](); i++) {
switch (ops[i]) {
case '+': res += nums[i + 1]; break;
case '-': res -= nums[i + 1]; break;
case '*': res *= nums[i + 1]; break;
case '/':
if (nums[i + 1] == 0) {
soundError();
return res;
}
res /= nums[i + 1];
break;
case '%':
res = fmod(res, nums[i + 1]);
break;
}
}
return res;
}

// ================= CALCULATOR =================


void calculator() {
system("cls");
color(10);

cout << "===== BASIC CALCULATOR =====\n";


cout << "Example: 5 + 2 * 3 =\n\n";

vector<double> nums;
vector<char> ops;

double num;
char op;

cout << "Expression: ";


cin >> num;
soundType();
nums.push_back(num);

while (true) {
cin >> op;
soundType();

if (op == '=') break;

if (string("+-*/%").find(op) == string::npos) {
soundError();
cout << "Invalid operator!\n";
Sleep(800);
return;
}

cin >> num;


soundType();

ops.push_back(op);
nums.push_back(num);
}
double result = evaluate(nums, ops);
soundOk();

color(14);
cout << "\nResult: " << result << "\n";

saveHistory("Result = " + to_string(result));


system("pause");
}

// ================= HISTORY =================


void showHistory() {
system("cls");
color(11);

cout << "===== HISTORY (" << currentUser << ") =====\n\n";

if ([Link]()) {
cout << "No history yet.\n";
} else {
for (size_t i = 0; i < [Link](); i++) {
cout << i + 1 << ". " << history[i] << endl;
}
}
system("pause");
}

// ================= EXIT =================


void exitAnim() {
system("cls");
color(12);
cout << "Saving data...\n";
Sleep(500);

for (int i = 3; i > 0; i--) {


cout << i << "...\n";
soundExit();
Sleep(500);
}

cout << "Goodbye " << currentUser << "!\n";


Sleep(700);
}

// ================= MAIN =================


int main() {
welcome();
int choice;
while (true) {
system("cls");
color(11);

cout << "===== MAIN MENU =====\n";


cout << "User: " << currentUser << "\n\n";
cout << "1. Calculator\n";
cout << "2. View History\n";
cout << "3. Exit\n";
cout << "Choose: ";

cin >> choice;


soundType();

switch (choice) {
case 1: calculator(); break;
case 2: showHistory(); break;
case 3: exitAnim(); return 0;
default:
soundError();
cout << "Invalid choice!\n";
Sleep(800);
}
}
}

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <windows.h>
#include <cmath>

using namespace std;

// ================= GLOBALS =================


string currentUser;
vector<string> history;

// ================= SOUNDS ==================


void soundType() { Beep(800, 30); }
void soundOk() { Beep(1200, 80); }
void soundError() { Beep(400, 200); }
void soundExit() { Beep(600, 100); Beep(400, 150); }

// ================= COLORS ==================


void color(int c) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

// ================= ANIMATION =================


void typePrint(const string& text, int delay = 30) {
for (char c : text) {
cout << c;
soundType();
Sleep(delay);
}
cout << endl;
}

// ================= ACCOUNT =================


bool userExists(const string& username) {
ifstream file(username + "_history.txt");
return [Link]() && [Link]() != ifstream::traits_type::eof();
}

void loadHistory() {
[Link]();
ifstream file(currentUser + "_history.txt");
string line;
while (getline(file, line)) {
history.push_back(line);
}
}

void saveHistory(const string& entry) {


history.push_back(entry);
ofstream file(currentUser + "_history.txt", ios::app);
file << entry << endl;
}

// ================= WELCOME =================


void welcome() {
system("cls");
color(11);
cout <<
"=====================================\n"
" WELCOME TO CODING CALCULATOR \n"
"=====================================\n\n";

color(14);
cout << "Enter username: ";
getline(cin, currentUser);

bool returningUser = userExists(currentUser);


loadHistory();

color(10);
if (returningUser) {
typePrint("Welcome back, " + currentUser + "!");
} else {
typePrint("Hello " + currentUser + "! Your calculator is ready.");
}

soundOk();
Sleep(800);
}

// ================= CALC LOGIC =================


double evaluate(vector<double>& nums, vector<char>& ops) {
double res = nums[0];
for (size_t i = 0; i < [Link](); i++) {
switch (ops[i]) {
case '+': res += nums[i + 1]; break;
case '-': res -= nums[i + 1]; break;
case '*': res *= nums[i + 1]; break;
case '/':
if (nums[i + 1] == 0) {
soundError();
return res;
}
res /= nums[i + 1];
break;
case '%':
res = fmod(res, nums[i + 1]);
break;
}
}
return res;
}

// ================= CALCULATOR =================


void calculator() {
system("cls");
color(10);

cout << "===== BASIC CALCULATOR =====\n";


cout << "Example: 5 + 2 * 3 =\n\n";

vector<double> nums;
vector<char> ops;

double num;
char op;

cout << "Expression: ";


cin >> num;
soundType();
nums.push_back(num);

while (true) {
cin >> op;
soundType();

if (op == '=') break;

if (string("+-*/%").find(op) == string::npos) {
soundError();
cout << "Invalid operator!\n";
Sleep(800);
return;
}

cin >> num;


soundType();

ops.push_back(op);
nums.push_back(num);
}

double result = evaluate(nums, ops);


soundOk();

color(14);
cout << "\nResult: " << result << "\n";

saveHistory("Result = " + to_string(result));


system("pause");
}

// ================= HISTORY =================


void showHistory() {
system("cls");
color(11);

cout << "===== HISTORY (" << currentUser << ") =====\n\n";

if ([Link]()) {
cout << "No history yet.\n";
} else {
for (size_t i = 0; i < [Link](); i++) {
cout << i + 1 << ". " << history[i] << endl;
}
}
system("pause");
}

// ================= EXIT =================


void exitAnim() {
system("cls");
color(12);
cout << "Saving data...\n";
Sleep(500);

for (int i = 3; i > 0; i--) {


cout << i << "...\n";
soundExit();
Sleep(500);
}

cout << "Goodbye " << currentUser << "!\n";


Sleep(700);
}

// ================= MAIN =================


int main() {
welcome();

int choice;
while (true) {
system("cls");
color(11);

cout << "===== MAIN MENU =====\n";


cout << "User: " << currentUser << "\n\n";
cout << "1. Calculator\n";
cout << "2. View History\n";
cout << "3. Exit\n";
cout << "Choose: ";

cin >> choice;


soundType();

switch (choice) {
case 1: calculator(); break;
case 2: showHistory(); break;
case 3: exitAnim(); return 0;
default:
soundError();
cout << "Invalid choice!\n";
Sleep(800);
}
}
}

You might also like