Problem with Function Program

What this program is supposed to do is have the user to enter values for a, b, c, d, and e. Then to find the largest of the five and print them. The program seems to be correct and using the right functions. However, when it comes to finding the largest it will print out "The largest of the five numbers is 16". You can input any numbers and the output will keep printing "The largest of the five numbers is: 16".

Here's the program if it helps:
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
using namespace std;

void get_value (int &a, int &b, int &c, int &d, int &e);
void find_largest (int a, int b, int c, int d, int e, int &largest);
void print_largest (int largest);

int main ()
{
	int a, b, c, d, e, largest;
	
	get_value ( a,  b, c, d,  e);
	find_largest (a,  b,  c,  d,  e,  largest);
	print_largest ( largest);
	
	return 0;
}

void get_value (int &a, int &b, int &c, int &d, int &e)
{
	cout << "Enter the value for A: " << endl;
	cin >> a;
	
	cout << "Enter the value for B: " << endl;
	cin >> b;
	
	cout << "Enter the value for C: " << endl;
	cin >> c;
	
	cout << "Enter the value for D: " << endl;
	cin >> d;
	
	cout << "Enter the value for E: " << endl;
	cin >> e;
}

void find_largest (int a, int b, int c, int d, int e, int &largest)
{
	largest == a;
	
	if (b > a)
	{
		largest = b;
	}
	
	else if (c > b)
	{
		largest = c;
	}
	
	else if (d > c)
	{
		largest = d;
	}
	
	else if (e > d)
	{
		largest = e;
	}
}

void print_largest (int largest)
{
	cout << "The largest of the five numbers is: " << largest << endl;
}
your else if logic is bad.
what you want is
largest = a;
if b> largest largest = b;
if c > largest largest = c;
... no elses


now that I said that, do you see why the elses break what you wanted?
what is c > b, and e is the largest of all?
it gets 'true' for c>b ... and stops... the elses don't fire..
Last edited on
The way you try to find the largest is questionable. Step trogh it with a=16, b=1, and c=2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void find_largest (int a, int b, int c, int d, int e, int &largest)
{
	largest == a;
	
	if (b > a)
	{
		largest = b;
	}
	
	else if (c > b)
	{
		largest = c;
	}
...

First, largest is set to 16, next b is less than a so largest stays 16, next c is larger than b and largest is set to 2. I doubt you wanted it like this.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
   int a, b, c, d, e;
   cout << "Enter the values for A, B, C, D, E: " << '\n';
   cin >> a >> b >> c >> d >> e;
   cout << "The largest of the five numbers is: " << max( { a, b, c, d, e } ) << '\n';
}



1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

template<typename T> T largest( T a, T b ) { return a > b ? a : b; }
template<typename T, typename... Tail> T largest( T a, Tail... b ){ return largest( a, largest( b... ) ); }

int main()
{
   int a, b, c, d, e;
   cout << "Enter the values for A, B, C, D, E: " << '\n';
   cin >> a >> b >> c >> d >> e;
   cout << "The largest of the five numbers is: " << largest( a, b, c, d, e ) << '\n';
}
Last edited on
Thank you, everyone, for trying to help! I just now figured out what I did wrong and have corrected it.

This is the updated version of the program:
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
using namespace std;

void get_value (int &a, int &b, int &c, int &d, int &e);
void find_largest (int a, int b, int c, int d, int e, int &largest);
void print_largest (int largest);

int main ()
{
	int a, b, c, d, e, largest;
	
	get_value ( a,  b, c, d,  e);
	find_largest (a,  b,  c,  d,  e,  largest);
	print_largest ( largest);
	
	return 0;
}

void get_value (int &a, int &b, int &c, int &d, int &e)
{
	cout << "Enter the value for A: " << endl;
	cin >> a;
	
	cout << "Enter the value for B: " << endl;
	cin >> b;
	
	cout << "Enter the value for C: " << endl;
	cin >> c;
	
	cout << "Enter the value for D: " << endl;
	cin >> d;
	
	cout << "Enter the value for E: " << endl;
	cin >> e;
}

void find_largest (int a, int b, int c, int d, int e, int &largest)
{
	largest = a;
	
	if (b > a)
	{
		largest = b;
	}
	
	if (c > b)
	{
		largest = c;
	}
	
	if (d > c)
	{
		largest = d;
	}
	
	if (e > d)
	{
		largest = e;
	}
}

void print_largest (int largest)
{
	cout << "The largest of the five numbers is: " << largest << endl;
}


The problem with the program was that at Line 39 I had typed out largest == a where instead I should have been largest = a
Try your code with (in order): 5 1 2 3 4

You have corrected a minor typo and completely ignored the problems with logic that others have been trying to tell you about. Your code is manifestly still incorrect.
Last edited on
Topic archived. No new replies allowed.