Confused as to how to pass array into if statement?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <iomanip>
 
using namespace std;
double calculateCharges(double hours[], double sizeofArray);//call function 
 
int main()
{
  //create variables for three customers
  double customers[3];
 
  //ask user for first customer hours and output hours
  cout<<"Enter hours for first customer: ";
  cin >>customers[0];
  //ask user for second customer hours and output hours
  cout<<"Enter hours for second customer: ";
  cin >>customers[1];
  //ask user for third customer hours and output hours
  cout<<"Enter hours for third customer: ";
  cin >>customers[2];
  //table labels
  cout<<"\nCar\tHours\tCharge\n";
  //output hours and charge for customer 1 with spacing aligned to the right
  cout<<"1"<<setw(12)<<right<<fixed<<setprecision(1)<<customers[0]<<setprecision(2)<<setw(9)<<right<<calculateCharges(customers[0])<<endl;
  //output hours and charge for customer 2 with spacing aligned to the right
  cout<<"2"<<setw(12)<<right<<fixed<<setprecision(1)<<customers[1]<<setprecision(2)<<setw(9)<<right<<calculateCharges(customers[1])<<endl;
  //output hours and charge for customer 3 with spacing aligned to the right
  cout<<"3"<<setw(12)<<right<<fixed<<setprecision(1)<<customers[2]<<setprecision(2)<<setw(9)<<right<<calculateCharges(customers[2])<<endl;   
  //output total hours and charge for all three customers with spacing aligned to the right
  cout<<"\n\nTOTAL"<<setw(8)<<right<<fixed<<setprecision(1)<<customers[0]+customers[1]+customers[2]<<fixed<<setprecision(2)<<setw(9)<<right<<calculateCharges(customers[0])+calculateCharges(customers[1])+calculateCharges(customers[2])<<endl;
 
   return 0;
}//end of main
 
//function charge calculation
double calculateCharges(double hours[], double sizeofArray)
{
  	double result[3];
		//if less then 3 hours return $2.00
		if(hours[]<3)
		{
			result[0]=2.00;
			return result[0];
		}
		//if more then 3 hours do the following
		else
		{
			//if less then 24 hours but more then 3 hours choose this statement added with initial 3 hour charge
			if (hours[]<24)
			{
				//calculation of total charge for less then 24 hours and return charge
				result[1]=2.00+(hours-3)*0.5;
				return result[1];
			}
			//if total time is equal to 24 hours return $10.00
			else
			{
				result[2]=10.00;
				return result[2];
			}
		}
  
}//end of function 

The issue I'am having is I don't know how I pass the information from the main into a array in the function and how to pass the array into the if statements. P.S. I'am a beginner at c++ programming.
@kdeng

When passing an array, you do not include the brackets, just the name. I then sent the location in the array. Hope the changes I made are understood, or, if not, ask about them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <iomanip>
 
using namespace std;

double calculateCharges(double hours[], int LocationInArray);//call function 
 
int main()
{
  //create variables for three customers
  double customers[3];
 
  //ask user for first customer hours and output hours
  cout<<"Enter hours for first customer: ";
  cin >>customers[0];
  //ask user for second customer hours and output hours
  cout<<"Enter hours for second customer: ";
  cin >>customers[1];
  //ask user for third customer hours and output hours
  cout<<"Enter hours for third customer: ";
  cin >>customers[2];
  //table labels
  cout<<"\nCar\tHours\tCharge\n";
  //output hours and charge for customer 1 with spacing aligned to the right
  cout<<"1"<<setw(12)<<right<<fixed<<setprecision(1)<<customers[0]<<setprecision(2)<<setw(9)<<right<<calculateCharges(customers, 0)<<endl;
  //output hours and charge for customer 2 with spacing aligned to the right
  cout<<"2"<<setw(12)<<right<<fixed<<setprecision(1)<<customers[1]<<setprecision(2)<<setw(9)<<right<<calculateCharges(customers, 1)<<endl;
  //output hours and charge for customer 3 with spacing aligned to the right
  cout<<"3"<<setw(12)<<right<<fixed<<setprecision(1)<<customers[2]<<setprecision(2)<<setw(9)<<right<<calculateCharges(customers, 2)<<endl;   
  //output total hours and charge for all three customers with spacing aligned to the right
  cout<<"\n\nTOTAL"<<setw(8)<<right<<fixed<<setprecision(1)<<customers[0]+customers[1]+customers[2]<<fixed<<setprecision(2)<<setw(9)<<right<<calculateCharges(customers,0)+calculateCharges(customers,1)+calculateCharges(customers,2)<<endl;
 
   return 0;
}//end of main
 
//function charge calculation
double calculateCharges(double hours[], int LocationInArray)
{
  	double result[3];
		//if less then 3 hours return $2.00
		if(hours[LocationInArray]<3)
		{
			result[LocationInArray]=2.00;
			return result[LocationInArray];
		}
		//if more then 3 hours do the following
		else
		{
			//if less then 24 hours but more then 3 hours choose this statement added with initial 3 hour charge
			if (hours[LocationInArray]<24)
			{
				//calculation of total charge for less then 24 hours and return charge
				result[LocationInArray]=2.00+((hours[LocationInArray]-3)*0.5);
				return result[LocationInArray];
			}
			//if total time is equal to 24 hours return $10.00
			else
			{
				result[LocationInArray]=10.00;
				return result[LocationInArray];
			}
		}
  
}//end of function 
Ah I see thanks for the information my professor didn't explain that part of the arrays.
Topic archived. No new replies allowed.