Problem with my code
Dec 24, 2021 at 10:20pm UTC
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" ;
}
Dec 24, 2021 at 10:35pm UTC
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
Dec 26, 2021 at 6:43pm UTC
Thank you and will do
Dec 27, 2021 at 4:05am UTC
I am adding "oopied" to my internet leetspeak
"to copy from something incorrect" maybe ...
Last edited on Dec 27, 2021 at 4:06am UTC
Jan 5, 2022 at 9:10pm UTC
"oopied" could be something related to working with OOP, maybe?
Jan 7, 2022 at 12:20pm UTC
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.