Help with program [C++]

Status
Not open for further replies.

iFargle

Linux / HPC / K8s SME
Staff member
Messages
4,403
Location
N/A
Now... Before we get all uppity here, yes, this is a homework problem. I'm not asking for an answer, but for direction.. Because I have no idea why it isn't working :tongue:

Code:
#include <iostream>
using namespace std;

int get_input(int &, int &);
int find_min(int, int);
int print_min(int);
	
int main()
{
	int num1, num2;

	get_input(num1, num2);
	print_min(find_min(num1, num2));
	
	return 0;
}

int get_input(int &num1, int &num2)
{
	cout << "Number 1:  "; cin >> num1;
	cout << "Number 2:  "; cin >> num2;
}
int find_min(int num1, int num2)
{
	if (num1 < num2)
		return num1;
	if (num2 < num1)
		return num2;
}
int print_min(int find_min())
{
	cout << "TEST OUTPUT" << endl;
}

Compiler output:
Code:
ifargle@mint /mnt/samba/CS221/HW 6 $ sudo g++ -o hw6 hw6.cpp
/tmp/ccfJSewM.o: In function `main':
hw6.cpp:(.text+0x35): undefined reference to `print_min(int)'
collect2: ld returned 1 exit status
(There are other outputs that I've stumbled across, but this is the one I'm getting now, after re-writing it again thinking I made a typo or something)

The problem bits of code I know are lines 6, 13, and 30. The homework instructions tell me to use a certain main function and function prototypes. Everything else can change, just not those.

Now, if I change line 30 to this,
Code:
int print_min(int num1)
it will output... I just don't know how to get the value stored in find_min() to carry over to print_min() without having extra variables.

This is a small part of a larger program.. And this is the only one I'm having trouble with, so instead of posting the entire thing, I'm just posting this.

Thank you for any help :thumbsup:

BTW: Admins, what are the chances we'll get syntax hi-lighting in the "code" tags? That'd be pretty neat.
 
Unless I'm missing something, you've answered your own question? Line 13 calls the find function, are you trying to store the returned variable from the find function IN the find function?! My brain is asploding!
 
Its what my homework tells me to do :( the find_min() function is never directly called in the main function.. only in print_min(find_min(num1, num2))

And that returns a different compiler error... something like...
Can't convert int to int (*)(int, int) or something odd. When I'm on my computer again I will post it
 
The last function? It has no guidelines, but I don't know how else to end it, I'm pretty new to C++ :tongue:
 
Code:
int print_min(int find_min())
{
	cout << "TEST OUTPUT" << endl;
}

You can't have a function call as an argument in the program definition, IIRC from C++. You need to have that as a variable.

Code:
print_min(find_min(num1, num2));
And since you're calling find_min already in print_min, and find_min returns as single integer, you just need the parameter as a single integer as well.

If you're confused by my explanation, let me know and I can try to explain it a different way.
 
Code:
int get_input(int &, int &);
int find_min(int, int);
int print_min(int);
	
int main()
{
	int num1, num2;

	get_input(num1, num2);
	print_min(find_min(num1, num2));
	
	return 0;
}

int get_input(int &num1, int &num2)
{
	cout << "Number 1:  "; cin >> num1;
	cout << "Number 2:  "; cin >> num2;
}
int find_min(int num1, int num2)
{
	if (num1 < num2)
		return num1;
	if (num2 < num1)
		return num2;
}
int print_min(int min)
{
	cout << min << endl;
}

You can't provide another function as a parameter.
 
Heck yes, it works, yay!

So let me see if I understand...
Code:
int get_input(int &, int &);
int find_min(int, int);
[B]int print_min(int);[/B]
	
int main()
{
	int num1, num2;

	get_input(num1, num2);
[SIZE=5]	print_min(find_min(num1, num2));[/SIZE]
	
	return 0;
}

int get_input(int &num1, int &num2)
{
	cout << "Number 1:  "; cin >> num1;
	cout << "Number 2:  "; cin >> num2;
}
int find_min(int num1, int num2)
{
	if (num1 < num2)
		return num1;
	if (num2 < num1)
		return num2;
}
[SIZE=5]int print_min(int min)[/SIZE]
{
	cout << min << endl;
}

The first enlarged text... assigns the value from find_min to the variable min in the second enlarged text? See this is where I was confused... I saw that I needed to use "print_min(find_min(num1, num2)), so I thought it would also be the same in both the main function and in the function itself.


So... again let me see if I understand lol

If I have these function prototypes...
int func(int, int, int);
int newfunc1(int, int);
int newfunc2(int, int);
int newfunc3(int, int);

and it's called in main as
func(newfunc1(num1, num2), newfunc2(num1, num2), newfunc3(num1, num2))

the "func" function would be...

int func(int var1, int var2, int var3)
{
. . .
}

which assigns var1 to newfunc1(), var2 to newfunc2(), and var3 to newfunc3()?

I really.. really hope I'm right... or at least on the right track :tongue:
 
Heck yes, it works, yay!

So let me see if I understand...
Code:
int get_input(int &, int &);
int find_min(int, int);
[B]int print_min(int);[/B]
	
int main()
{
	int num1, num2;

	get_input(num1, num2);
[SIZE=5]	print_min(find_min(num1, num2));[/SIZE]
	
	return 0;
}

int get_input(int &num1, int &num2)
{
	cout << "Number 1:  "; cin >> num1;
	cout << "Number 2:  "; cin >> num2;
}
int find_min(int num1, int num2)
{
	if (num1 < num2)
		return num1;
	if (num2 < num1)
		return num2;
}
[SIZE=5]int print_min(int min)[/SIZE]
{
	cout << min << endl;
}

The first enlarged text... assigns the value from find_min to the variable min in the second enlarged text? See this is where I was confused... I saw that I needed to use "print_min(find_min(num1, num2)), so I thought it would also be the same in both the main function and in the function itself.


So... again let me see if I understand lol

If I have these function prototypes...
int func(int, int, int);
int newfunc1(int, int);
int newfunc2(int, int);
int newfunc3(int, int);

and it's called in main as
func(newfunc1(num1, num2), newfunc2(num1, num2), newfunc3(num1, num2))

the "func" function would be...

int func(int var1, int var2, int var3)
{
. . .
}

which assigns var1 to newfunc1(), var2 to newfunc2(), and var3 to newfunc3()?

I really.. really hope I'm right... or at least on the right track :tongue:

It doesn't really assign var1 to newfunc1(), etc. newfunc1() would be called, and the return value would be a single integer value, which would be assigned to var1.
 
Status
Not open for further replies.
Back
Top Bottom