Comparing Values with Different Data types and getting the largest value

Right now my program works but I would like the program to return the fielddata[i].Field value with the highest Yph value.

FieldID are int's and Yph are floats

this is the piece of code i need to fix
1
2
3
4
5
6
7
for (int i = 0; i < 8; i++)
	{
		if (fielddata[i].Yph > highyph)
			highyph = fielddata[i].Field;
	}

	cout << "The Field with the Highest Yield is " << highyph << endl;


and the whole program is below

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
 

#include<iostream>
#include<fstream> 
 

using namespace std;
std::ifstream infile("fields.txt");

int initialise(int field, int crop, float size, float yof, float yph);

struct Fields {
	

	int Field;
	int Crop;
	float Size;
	float Yof;
	float Yph;

	int initialise(int field, int crop, float size, float yof, float yph)
	{
		Field = field;
		Crop = crop;
		Size = size;
		Yof = yof;
		Yph = yph;

	};

};



int main() {

	Fields fielddata[8];

	
	ifstream file("fields.txt");
	if(file.is_open())
	{
		//Fields fielddata[8];
	
		int a, b, i = 0;
		float c, d, e;
		while (infile >> a >> b >> c >> d >> e)
		{
			fielddata[i].Field = a;
			fielddata[i].Crop = b;
			fielddata[i].Size = c;
			fielddata[i].Yof = d;
			fielddata[i].Yph = e;

			++i;
		}
	
	
	}




float highyph = 0;



	cout << "Field\t" << "Crop\t" << "Size\t" << "YOF\t" << "YPH\t" << endl;

	for (int i = 0; i < 8; i++) {


		cout << fielddata[i].Field << "\t" << fielddata[i%3].Crop << "\t" << fielddata[i].Size << "\t" << fielddata[i].Yof << "\t" << fielddata[i].Yph << "\t" << endl;
	}
	

	for (int i = 0; i < 8; i++)
	{
		if (fielddata[i].Yph > highyph)
			highyph = fielddata[i].Field;
	}

	cout << "The Field with the Highest Yield is " << highyph << endl;





	system("Pause");
		return 0;
}
Last edited on
You say FieldID is an int and is where your problem is, but at no point does it appear in the code you posted.
fielddata.Field[i] is what i was referring too
This is inconsistent:
79
80
    if (fielddata[i].Yph > highyph)
        highyph = fielddata[i].Field;

On line 79, fielddata[i].Yph is of type float, therefore highyph should be of type float.

On line 80, fielddata[i].Field is of type int, therefore highyph should be of type int.

That's a pretty big clue that something is wrong there. Even if all were float, or all were int, the same error still applies, there is a mismatch.
It is hard to follow with the variable names you used, but it looks like you are trying to get a real number from an integer. An integer value does not go past the the decimal point. In other words, there is no difference between 2.1 and 2.9, they both equal 2.
1
2
3
4
5
6
7
if (fielddata[i].Yph > highyph)
	highyph = fielddata[i].Field;
//if (float > float)
//	float= integer;
// all that aside, if I understand right, this would be a better start
if(fielddata[i].Yph > highyph)
        highyph = fielddata[i].yph;    // now just designate field as highest 

Another inconsistency:
40
41
    ifstream file("fields.txt");
    if(file.is_open())

note the name of the ifstream variable: file

Now at line 47:
 
    while (infile >> a >> b >> c >> d >> e)

note the name of the ifstream variable: infile

In other words, you are reading data from a completely different stream than the one you just opened.

Just one reason why global variables should be avoided, they are hard to keep track of. (there are other reasons to avoid them). Delete the variable definition at line 8:
std::ifstream infile("fields.txt");
You need two separate variables, one to hold the highest value of Yph and another to hold the corresponding value of Field. Whenever you change the value of one, change the other too.



Or alternatively - just use a variable of type Fields

instead of
 
    float highyph = 0;
you could have
1
2
    Fields highf;
    highf.Yph = 0;


Then store the whole object when a new high value is found: highf = fielddata[i];
Last edited on
Thanks a lot to both, will take these and study them.
Slightly different from Cervil's suggestion, I'd just maintain a variable that is the position of the best item in the array. This is better because it's just one variable instead of two, and it's a small variable (a size_t) instead of a potentially giant one (a Field).
1
2
3
4
5
6
7
size_t pos = 0;
for (int i = 0; i < 8; i++) {
	if (fielddata[i].Yph > fielddata[pos].Yph) {
		pos = i;
	}
}
cout << "The Field with the Highest Yield is " << fielddata[pos].Field << endl;

Topic archived. No new replies allowed.