how to make a calculator inc++
// ONLY 29 LINES OF CODE IN TOTAL
// For better calculator scroll down on this post
#include <iostream>
using namespace std;
int main() {
cout << "Please enter the first number: ";
double n1 = 0;
cin >> n1;
cout << "Please enter an operator (+, -, *, /): ";
char op = '0';
cin >> op;
cout << "Please enter a second number: ";
double n2 = 0;
cin >> n2;
double answer = 0;
switch (op) {
case '+':
answer = n1 + n2;
break;
case '-':
answer = n1 - n2;
break;
case '*':
answer = n1 * n2;
break;
case '/':
answer = n1 / n2;
break;
}
cout << "Thanks for using my calculator your answer is: " << answer;
// this is the better calculator:
#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
char jim = 'j';
do {
cout << "Please enter the first number: ";
double n1 = 0;
cin >> n1;
cout << "Please enter an operator (+, -, *, /): ";
char op = '0';
cin >> op;
cout << "Please enter a second number: ";
double n2 = 0;
cin >> n2;
double answer = 0;
switch (op) {
case '+':
answer = n1 + n2;
break;
case '-':
answer = n1 - n2;
break;
case '*':
answer = n1 * n2;
break;
case '/':
answer = n1 / n2;
break;
}
cout << "Thanks for using my calculator your answer is: " << answer << " do you want to run the program again? (y or n) ";
cin >> jim;
} while (jim = 'y' && jim != 'n');
if (jim = 'n') {
cout << "Thanks for using my calculator here is sum epic music";
Beep(329, 300);
Beep(493, 300);
Beep(698, 300);
Beep(659, 600);
Beep(783, 300);
Beep(698, 300);
Beep(659, 600);
Beep(329, 100);
Beep(493, 300);
Beep(698, 300);
Beep(659, 600);
Beep(392, 250);
Beep(440, 200);
Beep(587, 300);
Beep(349, 250);
Beep(587, 500);
Beep(329, 300);
Beep(493, 300);
Beep(698, 300);
Beep(659, 600);
Beep(783, 300);
Beep(698, 300);
Beep(659, 600);
Beep(329, 100);
Beep(493, 300);
Beep(698, 300);
Beep(659, 600);
Beep(392, 250);
Beep(440, 200);
Beep(587, 300);
Beep(349, 250);
Beep(587, 400);
}
return 0;
}
Are there any code examples left?
New code examples in category C++
-
C++ 2023-04-28 17:44:25
-
C++ 2022-03-27 19:20:39 lists occurrences of characters in the string c++
-
C++ 2022-03-27 18:00:14 variabili in c++
-
C++ 2022-03-27 17:10:08 repeat character n times c++
-
C++ 2022-03-27 15:50:07 delete an array c++
-
C++ 2022-03-27 15:40:12 C++ pointer to base class
-
C++ 2022-03-27 12:15:21 find the graph is minimal spanig tree or not
-
C++ 2022-03-27 11:30:15 multi variable assignment cpp
-
C++ 2022-03-27 11:05:17 c++ pi float
-
C++ 2022-03-27 10:20:12 why exceptions can lead to memory leaks