Skip to content
Open
Changes from all commits
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
42 changes: 42 additions & 0 deletions exercises/calculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,45 @@
Multiplication: 8
Division: 2
*/


#include <iostream>
#include <cstdlib> // per EXIT_FAILURE
using namespace std;

int main() {
double num1, num2;

// Input del primo numero
cout << "Insert first number: ";
if (!(cin >> num1)) {
cerr << "Invalid input." << endl;
return EXIT_FAILURE;
}

// Input del secondo numero, controllo divisione per zero
while (true) {
cout << "Inserisci secondo numero: ";
if (!(cin >> num2)) {
cerr << "ERRORE" << endl;
return EXIT_FAILURE;
}
if (num2 != 0) break; // numero valido
cout << "Inserisci un'altro numero" << endl;
}

// Operazioni
double sum = num1 + num2;
double difference = num1 - num2;
double multiplication = num1 * num2;
double division = num1 / num2;

// Output
cout << "SUM: " << sum << endl;
cout << "Difference: " << difference << endl;
cout << "Multiplication: " << multiplication << endl;
cout << "Division: " << division << endl;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

potresti implementare il codice in più funzioni separate e non scrivere tutto il codice nel main


return EXIT_SUCCESS;
}