maximum and minimum between loops

Why I can't run the program after comparing maximum and minimum after each loops? What is the right way to do them?
int maxFee=0,minFee=100000, TpricePT3tax, TpriceSPMtax;

if (TpriceSPMtax > maxFee)
maxFee= TpriceSPMtax;
if (TpricePT3tax > maxFee)
maxFee = TpricePT3tax;
if (TpriceSPMtax < minFee)
minFee = TpriceSPMtax;
if (TpricePT3tax < minFee)
minFee = TpricePT3tax;
if (TpriceSPMtax > TpricePT3tax)
maxFee = TpriceSPMtax;
if (TpriceSPMtax < TpricePT3tax)
minFee = TpriceSPMtax;
if (TpricePT3tax > TpriceSPMtax)
maxFee = TpricePT3tax;
if (TpricePT3tax < TpriceSPMtax)
minFee = TpricePT3tax;
You have no loops in your code.

I don't see a mistake (assuming the fees are between 0 and 100000). The first four if statements look OK. The last four look unnecessary.

You could write it a bit simpler as

1
2
int maxFee = std::max(TpricePT3tax, TpriceSPMtax);
int minFee = std::min(TpricePT3tax, TpriceSPMtax);

or

 
auto [minFee, maxFee] = std::minmax(TpricePT3tax, TpriceSPMtax);

Last edited on
@Peter87 In the main program, there is if else statement. TpricePT3tax came from if, TpriceSPMtax came from else if. Let's say the first output I chose TpricePT3tax, it will update TpricePT3tax as the maximum and min=0. When I did the second loop and chose TpriceSPMtax, the total is higher and TpricePT3tax become lower so now the maximum is updated as Max= TpriceSPMtax, but the minimum should be updated as TpricePT3tax but it is now Min=0. noted: I've removed the last 4 if statement and we couldn't use the suggested coding from you as we didn't learned it in class.
Registered users can post here. Sign in or register to post.