Basic Calculator

Hi i am very very new to the c++ and coding world and from one or two videos iv seen the inputs in my code are the only ones that i know of. If you can point more variables and inputs i could use it would be very helpful but from my understandings iv gotten this far. From the forum iv seen the calculator done different ways but those inputs such as case, result, break and float are beyond my understandings.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  #include <iostream>

using namespace std;

int main()
{
    int sum;
    int difference;
    int multiply;
    int divide;
    int a;
    int b;

    cout << "what you trynna do \n";
    cin >> + >> - >> * >> /;




    sum = a + b;
    difference = a - b;
    multiply = a * b;
    divide = a / b;

    if(+) {
        cout <<"you total is..." << sum << endl;

    }

    if(-) {
        cout <<"you total is..." << difference << endl;

    }

    if(*) {
        cout <<"you total is..." << multiply << endl;

    }

    if(/) {
        cout <<"you total is..." << divide << endl;

    }





    return 0;
}

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
29
30
#include <iostream>

using namespace std;

int main()
{
   int num1, num2, ansver;
   char operation;
   
   cout << "What you trynna do \n";
   cin >> num1 >> operation >> num2;
   
   if(operation == '+')
   {
       cout << num1 + num2;
   }
   if(operation == '-')
   {
       cout << num1 - num2;
   }
      if(operation == '*')
   {
       cout << num1 * num2;
   }
   if(operation == '/')
   {
       cout << num1 / num2;
   }
    return 0;
}
Last edited on
note that:
 
cout << num1 / num2;

will not give you want you think.
http://mathworld.wolfram.com/IntegerDivision.html
closed account (1vD3vCM9)
If you want the results to be like:
3:2 = 1.5
You need to make:
1
2
3
4
5
6
float sum;
float divide;
float difference;
float multiply;
float a;
float b;

Correct me if I'm wrong. But for me it worked fine
Last edited on
Can be done like this also :

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>

using namespace std;

float calc(float a,float b,int ch)
{
	if(ch==1)
		return (a+b);
	if(ch==2)
	{
		if(a>b)
			return (a-b);
		if(b>a)
			return (b-a);
		if(a==b)
			return 0;
	}
	if(ch==3)
		return (a*b);
	if(ch==4)
	{
		if(a>b)
			return (a/b);
		if(b>a)
			return (b/a);
		if(a==b)
			return 1;
	}
}

int main()
{
	float x,y;int choice;
	cout<<"\n\t\tCalculator";
	cout<<"\n\t\t----------";
	cout<<"\n\n1.Add\n2.Difference\n3.Product\n4.Ratio\n5.EXIT\n\n";
	cin>>choice;
	if(choice==5)
		exit(1);
	cout<<"\nEnter 2 Numbers : ";
	cin>>x>>y;
	switch(choice)
	{
		case 1:
			cout<<"\nSum : "<<calc(x,y,1)<<"\n";
			break;
		case 2:
			cout<<"\nDifference : "<<calc(x,y,2)<<"\n";
			break;
		case 3:
			cout<<"\nProduct : "<<calc(x,y,3)<<"\n";
			break;
		case 4:
			cout<<"\nRatio : "<<calc(x,y,4)<<"\n";
			break;
		default:
			cout<<"\nTHANK YOU FOR USING!!!\n\tCOME AGAIN !!!\n";
	}
	return 0;
}
Last edited on
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
29
30
#include <iostream>

using namespace std;

int main()
{
   float num1, num2;
   char operation;
   
   cout << "What you trynna do \n";
   cin >> num1 >> operation >> num2;
   
   if(operation == '+')
   {
       cout << num1 + num2;
   }
   if(operation == '-')
   {
       cout << num1 - num2;
   }
      if(operation == '*')
   {
       cout << num1 * num2;
   }
   if(operation == '/')
   {
       cout << num1 / num2;
   }
    return 0;
}

this works and there is no integerDivision here, I didn't know about that thanks.
@oren drobitsky
Last edited on
Or This can be Done like this :

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>

using namespace std;

float calc(float a,float b,char ch)
{
	if(ch=='+')
		return (a+b);
	if(ch=='-')
	{
		if(a>b)
			return (a-b);
		if(b>a)
			return (b-a);
		if(a==b)
			return 0;
	}
	if(ch=='*')
		return (a*b);
	if(ch=='/')
	{
		if(a>b)
			return (a/b);
		if(b>a)
			return (b/a);
		if(a==b)
			return 1;
	}
	return 0;                  //here NULL can also be used (if one wishes to have no warnings)
}

int main()
{
	float x,y;char choice;
	cout<<"\n\t\tCalculator";
	cout<<"\n\t\t----------";
	cout<<"\n\n1.Add\n2.Difference\n3.Product\n4.Ratio\n5.EXIT\n";
	cin>>choice;
	if((choice=='5')||(!((choice>=49)&&(choice<=52))))
		exit(1);
	cout<<"\nEnter 2 Numbers : ";
	cin>>x>>y;
	switch(choice)
	{
		case '1':
			cout<<"\nSum : "<<calc(x,y,'+');
			break;
		case '2':
			cout<<"\nDifference : "<<calc(x,y,'-');
			break;
		case '3':
			cout<<"\nProduct : "<<calc(x,y,'*');
			break;
		case '4':
			cout<<"\nRatio : "<<calc(x,y,'/');
			break;
	}
	cout<<"\nTHANK YOU FOR USING !!!\n\tCOME AGAIN !!!";
    system("pause");
	return 0;   
}
Last edited on
This is the best way, I think.
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
29
30
31
32
#include <iostream>

using namespace std;

float calc(float a,float b,char c)
{
    if(c == '+')
        a = a + b;
    if(c == '-')
        a = a - b;
    if(c == '*')
        a = a * b;
    if(c == '/')
        a = a / b;
	return a;
}

int main()
{
	float x, y;
	char operation;

	cout << "Enter first number: ";
	cin >> x;
	cout << "Enter operation: ";
	cin >> operation;
	cout << "Enter second number: ";
	cin >> y;
	
	cout << "Ansver: " << calc(x, y, operation);
	return 0;   
}


@TheCplusplusMan
It's a bit hard for a human to read that.
Last edited on
Topic archived. No new replies allowed.