How do I get input from the user besides just integers? I'm trying to make a simple calculator program and I want to let the user decide which operator to use in the program. (+,-,*,/)
Here is my code so far. I want to take the First_Number int variable and use whichever operator the user chooses on the Second_Number int variable. Thanks for any assistance!
#include <iostream>
using namespace std;
int main()
{
cout << "Directions:\n";
cout << "Choose a number & press enter.\n";
cout << "Choose another number & press enter.\n";
cout << "Choose operator (+,-,*,/) & press enter.\n";
int First_Number;
int Second_Number;
char Operator;
I guess the problem that I am having is figuring out how to use the operator that the user chooses in the program. After they pick an operator, how do I apply it to the other two variables?
hey if you want I can post my calculator code here. It con divide by 0 and not get an error it just says its an infinite number. which if you think about it is correct because: 10 / .5 = 20 and if we go smaller the answer gets even bigger like 10 / .000005 = 2000000 so if you think about it anything divided by zero is an infinite number.
bboy 212, you can post your calculator code if you want. I figured out how to do a 4 function calculator except I haven't set up an error if the user tries to divide by 0. I want to compare my code to yours. Here is mine:
//4 Function Calculator
#include <iostream>
using namespace std;
int main()
{
cout << "CALCULATOR\n";
cout << endl;
cout << "Directions: \n";
cout << endl;
cout << "1. Enter a number then press enter.\n";
cout << "2. Enter an operator (+,-,*,/) then press enter.\n";
cout << "3. Enter another number then press enter.\n";
cout << endl;
//Variables Used
double number1;
double number2;
char op;
double result;
cin >> number1;
cin >> op;
cin >> number2;
if (op=='+')
result = number1 + number2;
else if (op=='-')
result = number1 - number2;
if (op=='*')
result = number1 * number2;
else if (op=='/')
result = number1 / number2;
It con divide by 0 and not get an error it just says its an infinite number. which if you think about it is correct because: 10 / .5 = 20 and if we go smaller the answer gets even bigger like 10 / .000005 = 2000000 so if you think about it anything divided by zero is an infinite number.
Not exactly. The limit of 1/x as x approaches 0 is infinity, but that doesn't say anything about 1/0. (Which in fact, is defined as undefined).