Thread: array problem

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    92

    array problem

    Hi, I ma having some trouble with arrays, I can't fill them up, the value entered replaces the first one, and the rest of array have value 0, what did I do wrong.
    Code:
    #include<stdio.h>
    
    
    int hour_in[200], min_in[200];
    int hour_out[200], min_out[200];
    int in_AM_PM[200], out_AM_PM[200];
    int charge[200];
    int hour[200];
    int overnight[200];
    int total_hours=0;
    int amount_collected=0;
    int i;
    
    
    void get_time_in_and_out(void)
    {
    	int i=0;
    
    	printf("Enter time in\n");//enter time in
    	printf("\tHour:");
    	scanf("%i",&hour_in[i]);
    
    		
    	printf("\n\tMinuts:");
    	scanf("%i",&min_in[i]);
    
    	printf("\n0 - AM ; 1 - PM\t");
    	scanf("%i",&in_AM_PM[i]);
    			
    		if(in_AM_PM[i]==1&&hour_in[i]<12)
    		{
    			hour_in[i]+=12;
    		}
    		else if(in_AM_PM[i]==0&&hour_in[i]==12)
    		{
    			hour_in[i]+=12;
    		}
    		else
    		{
    			hour_in[i]+=0;
    		}
    
    	printf("\nEnter time out\n");//enter time out
    	printf("\tHour:");
    	scanf("%i",&hour_out[i]);
    
    	printf("\n\tMinuts:");
    	scanf("%i",&min_out[i]);
    		
    	printf("\n0 - AM ; 1 - PM\t");
    	scanf("%i",&out_AM_PM[i]);
    			
    		if(out_AM_PM[i]==1&&hour_out[i]<12)
    			hour_out[i]+=12;
    		else if(out_AM_PM[i]==0&&hour_out[i]==12)
    			hour_out[i]+=12;
    		else
    			hour_out[i]+=0;
    	
    }
    void find_overnight(int x)
    {	
    	for(i=0; i<x; ++i)
    	{
    	if (hour_in[i]>4&&hour_in[i]<16)
    		overnight[i]=1;
    	else
    		overnight[i]=hour[i]*0;
    	}
    
    }
    
    
    void calculate_hours(int x)
    {
    	int total, i;
    	for(i=0; i<=x; ++i)
    	{
    	total=(((hour_out[i]*60)+min_out[i])-((hour_in[i]*60)+min_in[i]));
    
    	hour[i]=total/60;
    	total_hours+=hour[i];
    	}
    	//for(i=0;i<car;++i)//printf("%i\n",hour[i]);
    }
    
    void calculate_charge(int x)
    {
    	int i;
    
    	for(i=0; i<x; ++i)
    	{
    	if(overnight[i]==0)
    			charge[i]=11;
    		else if((hour[i]*overnight[i])>10&&(hour[i]*overnight[i])<=24)
    			charge[i]=27;
    		else if((hour[i]*overnight[i])>=0&&(hour[i]*overnight[i])<=1)
    			charge[i]=8;
    		else if((hour[i]*overnight[i])<2)
    			charge[i]=12;
    		else if((hour[i]*overnight[i])<3)
    			charge[i]=16;
    		else 
    			charge[i]=22;
    	}
    
    	for(i=0;i<x;++i)
    	{
    		amount_collected+=charge[i];
    	}
    }
    
    void print_table(int x)
    {
    	printf("Car\tTime in\t  Time out\tHours\tAmount\n");
    
    	for(i=0; i<x; ++i)
    	{
    		printf("%2i\t%4i:%i\t  %4i:%i\t %i\t%3i\n",i+1,hour_in[i],min_in[i],hour_out[i],min_out[i],hour[i],charge[i]);
    	}
    	printf("Total Hours Parked:\t%i\n",total_hours);
    	printf("Amount Collected:\t%i\n",amount_collected);
    
    }
    void main(void)
    {
    	int Done=1;
    	int car=0;
    
    	do
    	{
    	++car;
    	printf("Car#%i:\n\n", car);
    
    	get_time_in_and_out();
    
    	
    
    	printf("===============================");
    
    	printf("\n1 -Next car\n0 -Done   ");
    	scanf("%i",&Done);
    	printf("===============================\n\n");
    
    	}while(Done==1);
    
    	calculate_hours(car);
    
    	find_overnight(car);
    	
    	calculate_charge(car);
    
    	print_table(car);
    
    	
    	
    }
    Thanks in advance

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, in get_time_in_and_out, i is always and forever 0. Perhaps you could pass which car you're looking at from main and use that instead of i.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Thank I'll try

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What's your plan of attack poa on this?

    Would you consider making each car it's own "object" as a struct?

    Each car has certain characteristics we want to track:
    Code:
    struct car { 
       int time_in;
       int time_out;
       int onight_charge;
       //any other characteristics we need to track here.
    }cars;
    
    struct cars[50];
    
    int main(void)  {
    
    
    //etc.

    and I would use just an integer for these, btw. Also, I would use 24 hour time.
    It's just way easy that way. For 12 hour time, you make a simple adjustment when
    you want to print out the 12 hour time.

    it's simple, but I don't want to push you into something ahead of where you are. It can be
    done without structs for the cars.

    Either way, we'll be using one or more arrays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM