Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: implementation of a calculator #302

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions exercises/calculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,54 @@
Multiplication: 8
Division: 2
*/

#include <iostream>
using namespace std;

float operations(){

float a,b;
cout << "Insert the first number: ";
cin >> a;
cout << endl << "Insert second number: ";
cin >> b;

int op;
float result;
while(true){
cout << endl << "Select the operation: " << endl << "1) SUM" << endl << "2)Difference" << endl << "3)Multiplication" << endl << "4)Division" << endl;
cin >> op;
if(op <1 || op >4)
cerr << "Wrong choice, please select a valid operation number [1-4]" << endl;
else if(op == 4 && b == 0)
cerr << "Can't divide a number for 0, please select another operation" << endl;
else
break;
}

switch(op){
case 1:
cout << endl << "SUM = ";
result = a + b;
break;
case 2:
cout << endl << "Difference = ";
result = a - b;
break;
case 3:
cout << endl << "Multiplication = ";
result = a * b;
break;
case 4:
cout << endl << "Division = ";
result = a / b;
break;
}
return result;
}

int main(){

cout << operations() << endl << endl;

}