Please check my code!

Hello,

here is my code, I have problem with Max operator

For instance, 13 M 47
would be 47? how can I do that !

I don't know how I can use it
don't tell me as function, is there other way !

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
int main () {

	cout <<" Fixed-point expression (the available 7 operations are):" <<endl;
	cout <<" Addition       (1 + 1) "			<<endl;
	cout <<" Subtraction    (1 - 1) "			<<endl;
	cout <<" Multiplication (1 * 1) "			<<endl;
	cout <<" Division       (1 / 1) "			<<endl;
	cout <<" Modulo         (1 % 1) "			<<endl;
	cout <<" Less Than      (1 < 2)"			<<endl;
	cout <<" Maximum        (1 M 2)"			<<endl;

	unsigned num1, num2, ans;
	char op;
//	bool ok = true;

	cin >>num1 >>op >>num2;

	if (!cin) die("Input failure");

	switch (op) {
	case '+':
		ans = num1 + num2;
		break;
	case '-':
		if (num2 < 0) die("The ans cannot be nagative");
		ans = num1 - num2;
		break;
	case '*':
		ans = num1 * num2;
		break;
	case '/':
		if (num2 == 0) die("You cannot divide by zero");
	//	ok = false;
		
		ans = num1 / num2;
		break;
	case '%':
		if (num2 == 0) die("You cannot use the operation");
		ans = num1 % num2;
		break;
	case '<':
		ans = num1 < num2;
		break;
	case 'Max':
		if (num1 > num2)
			cout << num1 << endl;
		else 
			cout << num2 <<endl;
	default:
		die(string("I don't recognize ") + op + " as a valid operator");
		
	}

	cout << num1 << " " << op << " " << num2 << " = " << ans <<endl;
Last edited on
if (num2 < 0) die("The ans cannot be nagative"); this would only be true if num2 > num1.

As far as your problem 1) 'Max' would be a string not a character. So you will have to find a new name for it maybe 'M.' As for fixing this simply replace the output (cout) statements with ans = variable;
Last edited on
44
45
46
47
48
case 'M':
	if (num1 > num2)
		cout << num1 << endl;
	else 
		cout << num2 <<endl;
Still didn't fix the problem I don't get errors but also cannot get the right answer

1
2
3
4
5
case 'M':
		if (num1 > num2)
			ans = num1;
		else 
			ans = num2;
If you do it that way or the way smac did you will have to create a special case where you don't output this: cout << num1 << " " << op << " " << num2 << " = " << ans <<endl; if that is the problem you are referring to. A simple if statement should do the trick.
You forgot a break statement:

1
2
3
4
case 'M':
case 'm':
    ans = num1 > num2 ? num1 : num2;
    break;
thank you guys, it worked

1
2
3
4
5
6
case 'M':
		if (num1 > num2)
			ans = num1;
		else 
			ans = num2;
		break;
Topic archived. No new replies allowed.