Problem with my code

Hi. I am very new to C++(just started today) and oopied this example from a very outdated book(C++ primer 2nd edition) and would like to know why it is not working

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
  #include <iostream>
void read2(int&, int&);

void writeMax(int);
//using namespace std;

int main() {
	int val1, val2;
	read2(val1, val2);
	int maxVal = max(val1, val2);
	writeMax(maxVal);
	return 0;
}

void read2(int& v1, int& v2)
{
	std::cout << "Please enter two numeric values: ";
	std::cin >> v1 >> v2;
}

int max(int v1, int v2)
{
	if (v1 > v2)
		return v1;
	else
		return v2;
}

void writeMax(int val)
{
	std::cout << val
		<< " is the largest value..\n";
}
You forgot to forward declare the max function as your other two function are.
2
3
4
void read2(int&, int&);
int max(int, int);
void writeMax(int);

In the future please tell us EXACTLY what is wrong. Saying "is not working" is not helpful.
What is helpful is something like
error C3861: 'max': identifier not found
Thank you and will do
I am adding "oopied" to my internet leetspeak
"to copy from something incorrect" maybe ...
Last edited on
"oopied" could be something related to working with OOP, maybe?
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
#include <iostream>

void read2(int&, int&);
void writeMax(int);
int max(int, int);

int main() {
	int val1 {}, val2 {};

	read2(val1, val2);
	writeMax(max(val1, val2));
}

void read2(int& v1, int& v2) {
	std::cout << "Please enter two numeric values: ";
	std::cin >> v1 >> v2;
}

int max(int v1, int v2) {
	return v1 > v2 ? v1 : v2;
}

void writeMax(int val) {
	std::cout << val << " is the largest value..\n";
}

Topic archived. No new replies allowed.