Thread: Variable assignment in a 'for' loop won't stick.

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    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

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    ... 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!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined reference to.. probably Makefile problem
    By mravenca in forum C Programming
    Replies: 11
    Last Post: 10-20-2010, 04:29 AM
  2. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  3. C variable
    By webstarsandeep in forum C Programming
    Replies: 1
    Last Post: 10-23-2008, 01:26 AM
  4. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  5. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM