First, I'll post my program:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int term = 5;
struct house {
int house_cost, fuel_cost;
double tax_rate;
} ;
house houses[3];
double total_cost(house h) {
double total = h.house_cost;
for(int i = 0; i < term; i++) {
total += h.fuel_cost;
total += h.tax_rate*h.house_cost/1000;
}
return total;
}
int sort_houses(house h[]) {
int least = 0;
for(int i = 0; i < sizeof(h)/sizeof(house); i++) {
if(total_cost(h[i]) < total_cost(h[least])) {
least = i;
}
}
return least;
}
int main()
{
houses[0].house_cost = 67000;
houses[0].fuel_cost = 2300;
houses[0].tax_rate = .025;
houses[1].house_cost = 62000;
houses[1].fuel_cost = 2500;
houses[1].tax_rate = .025;
houses[2].house_cost = 75000;
houses[2].fuel_cost = 1850;
houses[2].tax_rate = .02;
cout<<"Initial house cost"<<setw(20)<<"Annual fuel cost"<<setw(20)
<<"Tax rate per $1000"<<setw(16)<<"Total cost\n"
<<"=================="<<setw(20)<<"================"<<setw(20)
<<"=================="<<setw(16)<<"==========\n";
for(int i = 0; i < 3; i++) {
cout<<setw(7)<<'$'<<fixed<<setprecision(2)<<houses[i].house_cost
<<setw(16)<<'$'<<setprecision(2)<<houses[i].fuel_cost
<<setw(15)<<'$'<<setprecision(3)<<houses[i].tax_rate
<<setw(13)<<'$'<<setprecision(2)<<total_cost(houses[i])<<endl;
}
if(sizeof(houses)/sizeof(house) > 1) {
cout<<"\n\nThe best buy is house #"<<sort_houses(houses)+1<<'.';
}
cout<<"\n\nPress <Enter> to exit.";
char myLine[100];
cin.getline(myLine, 100);
return 0;
}
Now, my problem is in the function sort_houses, isolated below:
Code:
int sort_houses(house h[]) {
int least = 0;
for(int i = 0; i < sizeof(h)/sizeof(house); i++) {
if(total_cost(h[i]) < total_cost(h[least])) {
least = i;
}
}
return least;
}
What the program should do is, given an array of the struct 'house', and after labeling the house at index 0 as the least expensive (looking at the total_cost), compare each house's total_cost in the array to the total_cost of the house which is being pointed to by the variable 'least', and if that house costs less, label that new house as 'least'.
It's a simple sorting program, and by the end of it, 'least' should hold the index of the house that has the lowest total_cost.
When I run the program, the function runs without error, but when it returns 'least', the variable is still equal to '0', when in this case the variable should end up equal to '1'.
What I think is happening is that the 'least' variable inside the 'for' loop isn't sticking once the 'for' loop terminates. I don't know how to fix that, though.
Any answers to the question I asked would be appreciated. Any comments and tips regarding my code's structure, my programming abilities, or my style, will be less appreciated, but still considered.
Finally, some of you may be wondering why I'm using a 'for' loop when I already know the length of the array, and it is three. The answer is, although my teacher already gave me the data I'd need to put into the program, I wanted to build a program that could handle any number of houses. Otherwise I'd just use a bunch of if/else's to determine the lowest cost. Or, better yet, just manually set the index to '1'. :P
- Arandur