Using other forms of user input besides int

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;

cin >> First_Number;
cin >> Second_Number;
cin >> Operator;

cout << First_Number Operator Second_Number;


int h;
cin >> h;
return 0;

}
Last edited on
1
2
3
4
char op ; // for +,-,/,*
int number1,number2;

cin>>number1>>op>>number2;
Last edited on
Thanks for the reply. Can you explain how this works? I am still having trouble getting it to work.
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?
You can check for the value of op using a switch or if-else ladder,then according to it perform the necessary operation as below:
1
2
3
4
5
6
7
if(op=='+')
        result=number1+number2;
else if(op=='-')
      result=number1-number2;
..........
.........
.........


In case of division,check for division by zero (2nd numbe can not be zero) and casting to float.
You may want to add a check for division by zero case.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>


using namespace std;
int main()
{
    char op;
    int num1,num2;

    cout<<"Enter operation (format 5+2 ): ";
    cin>>num1>>op>>num2;  //enter num1,op sign,num2

    switch(op)
    {
        case '+':
            cout<<num1<<op<<num2<<"="<<num1+num2;
            break;
        case '-':
            cout<<num1<<op<<num2<<"="<<num1-num2;
            break;
        case '*':
            cout<<num1<<op<<num2<<"="<<num1*num2;
            break;
        case '/':
            cout<<num1<<op<<num2<<"="<<float(num1)/float(num2);
    }
    return 0;
}
Thanks very much! This helps a lot!
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;



cout << "= ";
cout << result;
cout <<endl;
cout <<endl;

int end;
cin >> end;
return 0;
}
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).
+1 firedraco ;)
EDIT: Although in fairness I used to think 1/0 = infinity as well...
Last edited on
well my old source code directory is to scary to dig through sorry :( did not realise how bad it was
Topic archived. No new replies allowed.