multiplying two numbers in an array to get another array

Hi! I'm trying to create a program where I have a user enter in the number of trips, then for each of the trips enters in the trip number, miles, and the cost to the organization per mile. Then, I want to display a receipt with all of that information plus the expense (miles*amt per mile). How would I multiply the two arrays to get the expense amount?

This is what I have so far for the array but I don't know if its right/how to get the expense..

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
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	//declare variables
	char another = 'Y';
	double trips = 0.0;
do
{

//declare array
	double numtrip[x][y][z]= {0};

//declare variables
double miles = 0.0;
double milesAmt = 0.0;

//fill array with data

	cout << "Enter in the number of trips: " ;
	cin>> trips;
	for (int x=0; x <=trips; x+=1)
	{
		for (int numtrip=0; numtrip <=x; numtrip+=1)
		{cout <<"Enter in the trip number of trip " << x << ": ";
		cin >> numtrip[sub];
		for (int miles=0; miles <=x; miles+=1)
		{cout <<"Enter in the miles of trip " << x << ": ";
		cin >> miles[sub];
		for (int mileAmt=0; mileAmt <=x; mileAmt+=1)
		{cout << "Enter in the mileage amount." << endl;
	cout << "<Enter .50 for 50 cents>: ";
	cin >> mileAmt[sub];
		}//end for
		}//end for
		}//end for

//calculations 
Is there any particular reason why you are using a 3 dimensional array? If not, there are much better ways. What exactly does the 3 dimensional array hold in its 3 dimensions?
To be honest, I don't really know.

I was trying to get the user to enter in how many trips they have..

then for each trip, they would individually enter the trip number, the miles and the cost per mile.. and at the end it would display back each individual trip plus the total from all the trips
Well how about one of these two approaches.

You could create a class for Trip and hold the trip number, the miles, and the cost per mile in that class. Then just have a 1 dimensional vector of the Trip type. You could even do all of the calculations in the class with member functions.
1
2
3
4
5
6
7
8
class Trip {
    int trip_number;
    float num_miles, cost_per_mile;
};

int main() {
    std::vector<Trip> trips;
}


OR...
You could have two vectors of that information. One for the miles and one for the cost per mile. The trip number would be represented as the index that the information is in the vector (you could represent the trip number the same way in the class example).

1
2
3
int main() {
    std::vector<float> num_miles, cost_per_mile;
}


I recommend the class example, because its more practical and less error prone.
Topic archived. No new replies allowed.