# Variable assignment in a 'for' loop won't stick.

• 11-12-2010
wyntrewolfe
Variable assignment in a 'for' loop won't stick.
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
• 11-12-2010
rags_to_riches
You can't use sizeof on a passed array, because it decays to a pointer and is therefore the sizeof a pointer. Because you've declared houses as a global, you can just use sizeof(houses) inside the function. The other, better alternative is to pass the size of the array to the function along with the array. Please note also for future reference that sizeof() is a COMPILE-TIME operator, not a runtime one, so it will not work on dynamic arrays either.
• 11-12-2010
wyntrewolfe
... wow. That did the trick perfectly, thanks! I guess I should research my fundamentals a bit more before messing with abstractions I don't understand. :3 Thanks a ton!
• 11-14-2010
Elysia
I suggest you use std::array instead of a native array. Then you will always have the size available by calling the .size() member function. Don't forget to pass it by reference!