Hi everybody,
I wanna learn how can I solve this problem of undefined symbols while trying to load them from another function. Here's the code to explain:

void maxmin(struct ma mx)
{ int max=mx.ev[0];
int min=mx.od[0];
for (int i=0;i<10;i++)
if (mx.ev[i]>max)
max=mx.ev[i];
for (int j=0;j<10;j++)
if (mx.od[j]<min)
min=mx.od[j];
cout<<"The biggest even number is:"<<max<<endl;
cout<<"The smallest odd number is:"<<min<<endl;
}
void minmax(struct ma my)
{ int max2=my.ev[0];
int min2=my.od[0];
for (int i=0;i<10;i++)
if (my.ev[i]<max2)
max2=my.ev[i];
for (int j=0;j<10;j++)
if (my.od[j]>min2)
min2=my.od[j];
cout<<"The smallest even number is:"<<max2<<endl;
cout<<"The biggest odd number is:"<<min2<<endl;
if (max>max2)
cout<<"The biggest number of all is:"<<max;
else
cout<<"The biggest number of all is:"<<max2;
if (min>min2)
cout<<"The smallest number of all is:"<<min2;
else
cout<<"The smallest number of all is:"<<min;
}

My problem is in lines: 24, 28.

I'm trying to get max, min from another function to compare them with max2, min2 which are from the current function, I think the return method won't work cuz it's a void function. So can anyone give me an idea? Thanks.

Functions can not use variables that were declared in other functions. You have two choices: pass min and max variables as parameters or make them global so that all functions can use them. Passing as parameters is the preferred way to solve the problem.

Functions can not use variables that were declared in other functions. You have two choices: pass min and max variables as parameters or make them global so that all functions can use them. Passing as parameters is the preferred way to solve the problem.

And how can I pass them? Can you give me an example or anything cause I'm still new on this one.

That seems to be the least of your problems.

You'll notice that this won't compile:

#include <iostream>

void maxmin(struct ma mx);

int main()
{
  
  return 0;
}
void maxmin(struct ma mx)
{ 
  int max=mx.ev[0];
  int min=mx.od[0];
  for (int i=0;i<10;i++)
  if (mx.ev[i]>max)
  max=mx.ev[i];
  for (int j=0;j<10;j++)
  if (mx.od[j]<min)
  min=mx.od[j];
  cout<<"The biggest even number is:"<<max<<endl;
  cout<<"The smallest odd number is:"<<min<<endl;
}

Is your struct 'ma' defined anywhere?

This is the closest thing to what you had that I could get to compile:

#include <iostream>

struct ma
{
  int ev[1];
  int od[1];
};

void maxmin(ma mx);

int main()
{
  
  return 0;
}
void maxmin(ma mx)
{ 
  int max=mx.ev[0];
  int min=mx.od[0];
  for (int i=0;i<10;i++)
  if (mx.ev[i]>max)
  max=mx.ev[i];
  for (int j=0;j<10;j++)
  if (mx.od[j]<min)
  min=mx.od[j];
  std::cout<<"The biggest even number is:"<<max<<std::endl;
  std::cout<<"The smallest odd number is:"<<min<<std::endl;
}

Maybe that will help you get started.

Dave

And how can I pass them? Can you give me an example or anything cause I'm still new on this one.

Pass them just like you did the structure parameter. Declare the two variables in main(), then pass them by reference to maxmin() so that it can change the values, then pass them to minmax() so that it can use them. void maxmin(struct ma mx, int &min, int &max)

AncientDragon, I don't get this syntax:

void func(struct ma mx)

shouldn't it just be

void func(ma mx)

?

Dave

Look at line 1 of your original post. That space will cause a syntax error so change it to whatever you want it to be.

Which space? Line 1 is just an include, right?

Look at line 1 of your original post

void maxmin(struct ma mx)

I'm still confused. That is line #1 of the very first post in the thread, and I'm still not sure which space you're talking about.

There is a space between "ma" and "mx". I know I'm going blind, but I'm not that blind yet :)

ah, I thought the struct was called 'ma' and the instance of it was called 'mx', that's why I had recommended using just

void func(ma mx)

In c++ you can do it either way.

That seems to be the least of your problems.

You'll notice that this won't compile:

#include <iostream>

void maxmin(struct ma mx);

int main()
{
  
  return 0;
}
void maxmin(struct ma mx)
{ 
  int max=mx.ev[0];
  int min=mx.od[0];
  for (int i=0;i<10;i++)
  if (mx.ev[i]>max)
  max=mx.ev[i];
  for (int j=0;j<10;j++)
  if (mx.od[j]<min)
  min=mx.od[j];
  cout<<"The biggest even number is:"<<max<<endl;
  cout<<"The smallest odd number is:"<<min<<endl;
}

Is your struct 'ma' defined anywhere?

This is the closest thing to what you had that I could get to compile:

#include <iostream>

struct ma
{
  int ev[1];
  int od[1];
};

void maxmin(ma mx);

int main()
{
  
  return 0;
}
void maxmin(ma mx)
{ 
  int max=mx.ev[0];
  int min=mx.od[0];
  for (int i=0;i<10;i++)
  if (mx.ev[i]>max)
  max=mx.ev[i];
  for (int j=0;j<10;j++)
  if (mx.od[j]<min)
  min=mx.od[j];
  std::cout<<"The biggest even number is:"<<max<<std::endl;
  std::cout<<"The smallest odd number is:"<<min<<std::endl;
}

Maybe that will help you get started.

Dave

What I pasted in the thread in just a sample of my program. I already defined everything in main(), I just didn't copy the whole program ; just what I have a problem in.

Pass them just like you did the structure parameter. Declare the two variables in main(), then pass them by reference to maxmin() so that it can change the values, then pass them to minmax() so that it can use them. void maxmin(struct ma mx, int &min, int &max)

Still I didn't understand it all, sorry.

Can you give me another example ?

There is a space between "ma" and "mx". I know I'm going blind, but I'm not that blind yet :)

It's not a full name for the structure, it's another structure named "mx" that has the same contents as the original one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.