Hello everyone;

I did a program using arrays -for the first time-

I've got errors, I fixed some and there are others left that I couldn't straighten

any help is highly appreciated

here is the code :

# include <iostream>
# include <iomanip>
using namespace std;

int read_car (int cars[], int n);
double read_time (double time[], int n);
double charge (time[]);
void print ();

double sum_time, sum_charge;

int main()
{
	int time;
	int cars[n];
	int n;

	cout << "Welcome to the Car Parking fee calculater." <<endl;
	cout << "Please enter the numbers of the cars." <<endl;
	cin >> n;

	read_car (cars,n);
	read_time(time,n);
	charge(time,n);

	print ();
	
	return 0;

	
}





//1

int read_car (int cars[], int n)
{
	int i;

	for (i=1; i<=n; i++)
		cin >> cars[i];

	return cars;
}

//2
double read_time (double time[], int n)
{
	int i;
	sum=0;

	for (i=1; i<=n; i++)
		cin >> time[i];
	time[i] = ceil(time);
	
	sum_time= sum + time;

	return time;
}
//3
double charge (double time[],int n)
	{ 
		double charge;
		double sum=0;

		for ( int i=1; i<=n; i++)
		{

			if (time <= 3.00)
				charge= 2.00;

			if (time == 24.0)
				charge = 10.0;
	
				else 
					charge = (time - 3)*0.5 + 2.00;

			sum_charge= sum+charge;

			return charge;
		}
}

//4
void print (n)
{
	cout << left;
	cout << setw(6) <<"Car"
		 << setw(6) <<"Hours"
		 << setw(6) <<"Charge" 
		 <<endl;
	cout <<"__________________"<<endl;

	for (i=1; i<=n; i++)
	{

		cout << setw(6) << i
			 << setw(6) << time[i]
			 << setw(6) << charge (time,n)
			 <<endl;
	}
	
	cout <<"__________________"<<endl;

	cout << setw(6) <<"Total"
		 << setw(6) << sum_time
		 << setw(6) << sum_charge
		 <<endl;

}

To be honest, I would rewrite some of the code to use pointers as parameters versus an array parameter. It's been awhile since I've worked directly with C++ so I'm not entirely sure.

I am a bit concerned about this chunk of code though--

# include <iostream>
# include <iomanip>
using namespace std;

int read_car (int cars[], int n);
double read_time (double time[], int n);
double charge (time[]);
void print ();

double sum_time, sum_charge;

int main()
{
	int time;
	int cars[n];
	int n;

The last 3 lines of the information I grabbed from your snippet I realize that n isn't declared until after int cars[n], yet you use int cars[n] as if n has been declared and is a constant.

If you want to dynamically adjust an array size consider using a pointer--

int main(){

	int time;
	int n;

	cout << "Welcome to the Car Parking fee calculater." <<endl;
	cout << "Please enter the numbers of the cars." <<endl;
	cin >> n;

        int* cars = new int[n];

        // .. sometime later in code
        delete n;
        return 0;
}

This is only half the battle. You use time in your functions as if its an array (or pointer) but the declaration

int time;

suggests that time is not an array/pointer type and only holds one value.

There are probably other problems but I'm not near a compiler, nor do I want to overstep my bounds. Try making the fixes I suggested first and see if you can solve your problem(s) from there.

Edit: I also notice that the function you declare to charge (time*) differs from the function you defined below main as charge(time*, int). You should change your declaration to match your definition.

Edited 7 Years Ago by Intrade: n/a

Thanks for replying .. I'll try it but can I use this declaration :

int time[];
	int cars[];

instead of putting "i" or "n" inside ?

Thanks for replying .. I'll try it but can I use this declaration :

int time[];
	int cars[];

instead of putting "i" or "n" inside ?

I'm fairly certain that you have to give the array a size parameter before your code can compile.

int time[3];
int cars[3];

but as you can see, you cannot assign time and cars a new set of input once they are declared with their size. They keep that size and the only way for them to lose it is for those variables to go out of scope.

Again, if you're doing something with variability in array sizes, use pointers or use the standard library vector.

The problem is that I want the user to enter the size,
but I keep confuzing the variables

I need it to get the first value of time and the second etc to the nth
and then start calculating the fee "charge" for each
(but then at some point I have to get the sum of time and charge )

finally I need it to output everything in a table

do you think I'm making it harder while there might be easier ways?

Is this like a rental car program? That's what I can see from this but correct me if I'm wrong.

Again, if you want to have variability in your array size use a vector or a pointer.

Also I noticed something very odd in your code--

double charge (double time[],int n)
	{ 
		double charge;
		double sum=0;

		for ( int i=1; i<=n; i++)
		{

			if (time <= 3.00)
				charge= 2.00;

			if (time == 24.0)
				charge = 10.0;
	
				else 
					charge = (time - 3)*0.5 + 2.00;

			sum_charge= sum+charge;

			return charge;
		}
}

1) Your for loop starts with the iteration counter at 1 instead of 0. I can see how you would make this kind of mistake if you've previously programming in Lua. Keep in mind that arrays start at index 0.

2) Your array goes up to, and includes n which means that if you have an array of size 3 with indices 0, 1, 2 with data {9, 13, -1} where 9 is located at index 0, 13 is located at index 1 and -1 is located at index 2, yet you want to access index 3 you will get an error from your application for attempting to access memory that doesn't belong to it. Change your iteration of i <= n to i < n.

3) You have a return statement in the iteration of your for loop, meaning after one iteration the process of looping stops and you return charge.

4) Your parameter time is an array type, but you use time as if it is a single-variable type. Consider changing "time" with "time", but only after you've made the corrections I suggested in points 1) and 2).

Edited 7 Years Ago by Intrade: n/a

This article has been dead for over six months. Start a new discussion instead.