Pls help me with a code im new

this is supposed to be a simple calculator but it either returns a random number or 0
here is the code

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
#include <iostream>
using namespace std;

int math (int a, int b, char c)
{
    int r;
    if (c=1)
    {r=a+b;}
    if (c=2)
    {r=a-b;}
    if (c=3)
    {r=a*b;}
    if (c=4)
    {r=a/b;}
    return (r);
} 
int main ()
{
 char yon;
 int first, second, third, type;
 cout << "would you like to do a simple math problem";
 cin >> yon ;
 do
 {
	cout << "Enter type\n";
    cin >> type;
    cout << "You chose " << type << "\n";
    cout << "Enter first number\n";
    cin >> first;
    cout << "First number is " << first << "\n";
    cout << "Enter second number\n";
    cin >> second;
    cout << "Second number is " << second << "\n";
    third = math (first, second, type);
    cout << "Answer is " << third << "\n" ;
 } while  (yon = 'y');};


if you see anything wrong pls post

Last edited on
Hi!

I think the problem is because you only have one = sign in the type check in your math function. It shall be two when compairing:


Ex:
if (c==1) {
r=a+b;
}
thanks so much it worked
Also, don't use "\n", just use endl.

cout << "You chose " << type << endl;

That makes it more cross-platform.
Topic archived. No new replies allowed.