Ternary Operators

I'm using Microsoft Visual Studio as my IDE, and I have this build error from my compiler saying that the max is undeclared. However, in my textbook they just declared max as (a > b) ? a : b; . I copied the code exactly from my textbook and I don't understand this build error or what other error that could be causing this issue. Any help or suggestions would be great.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include "conio.h"
using namespace std;

int main() {

	int a = 1, b = 2;

	cout << "Variable a value is: ";
	cout << ((a != 1) ? "not one, " : "one, ");
	cout << ((a % 2 != 0) ? "odd" : "even");

	cout << endl << "Variable b value is: ";
	cout << ((b != 1) ? "not one, " : "one, ");
	cout << ((b % 2 != 0) ? "odd" : "even");

	max = (a > b) ? a : b;  // Insert statements to output the greater of the two stored variable values.
	cout << endl << "Greater value is: " << max << endl;
	
	_getch();
	return 0;
}
You didn't declare max. Try

int max = (a>b)? a: b;
Okay, thanks Shawn Lau.
Topic archived. No new replies allowed.