how to find total

Hello, I want to find a total of the fees but no matter how I try it always gives errors, can you help me.
#include <iostream>
using namespace std;
float calfee(float time);
int main()

{

int number = 10;
float fees[number];
float times[number];
int i;
for(i=0; i<number; i++)
{
cout<<"Enter the time of parking of "<<i+1<<" customer: ";
cin>>times[i];
fees[i]=calfee(times[i]);
}

for(i=0; i<number; i++)
cout <<"Customer's "<<i+1<<" fee for "<<times[i]<<" = "<<fees[i]<<endl;

return 0;
}
float calfee(float time)
{
float amount = 25 + (5 * ((int)time - 3));
if(amount > 60)
return 60;
else if(amount<25)
return 25;
else return amount;
}
What errors are you getting?
Just at a glance, and without you mentioning what error you're actually receiving (would be helpful to post the error in the future), you're saying you want to find the total for the fees yet you have no code here that attempts to sum up the fees. Other than that, your arrays are initialized with a non-const size, which while it is supported by some compilers, it's not a very portable way to write that bit. If you add
const int number = 10;
Then it becomes portable.
In your calfee() function, you're initializing a float with an integer value.
You could:
a) Either just use all float values here:
float amount = 25.0f + 5.0f*time-3.0;
b) Or just make amount an integer variable since you're already casting time to int;
Approach b seems to be the way you're wanting to approach this since in your conditional checks, you're comparing your float variable "amount" to an integer value.

I would rework your approach like so:
Assuming what you really want is a float return value
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
#include <iostream>
#include <array>

float calfee(float time);

int main()
{
    const int number{ 10 };
    int i{ 0 };
    std::array<float, number> fees;
    std::array<float, number> times;

    for (i; i < number; ++i) {
        std::cout << "Enter the parking time of customer " << i + 1 << ": ";
        // You can use [] but .at() includes a bounds check here that [] doesn't provide
        std::cin >> times.at(i);
        fees.at(i) = calfee(times.at(i));
    }

    for (i=0; i < number; ++i) {
        std::cout << "Customer " << i + 1 << "'s fee for " << times.at(i) << " = " << fees.at(i) << "\n";
    }
}

float calfee(float time) {
    auto amount = 25.0f + (5.0f * (time- 3.0f));

    if(amount > 60.0f){
        return 60.0f;
    } else if ( amount < 25.0f){
        return 25.0f;
    }else{
        return amount;
    }
}


The above compiles and gives me correct values in a really brief test case.
There are many other ways to achieve what you're aiming for as well and my quick rework is just one of those ways, but as I stated in the beginning, you don't have any code here that attempts to sum any of the values together - so I'm a little lost on what your actual question should be.

One last word of warning though, I'm not sure if it's being taught to you or not, but don't use
using namespace std; gloabally as you have a high chance of collisions, especially once you start working on larger projects that use third-party libraries and even internal code that may use namespaces that have functions of the same signature and naming as those found in std's namespace. When that happens, you'll find that your compiler is unable to disambiguate what function call you're really intending to make.
It's better to just use the namespace or even use a namespace alias (incredibly helpful for namespaces like those in the chrono library).

If I misunderstood your question, I apologize in advance.

EDIT: Realized what you were doing in your conditional check and edited answer to reflect this (My bad on that one lol)
Last edited on
This can be simplified - you don't need fees array. This also shows the total of all the fees:

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
#include <iostream>

float calfee(float time);

int main() {
	constexpr size_t number {10};
	float times[number] {};

	for (size_t i {}; i < number; ++i) {
		std::cout << "Enter the parking time of customer " << i + 1 << ": ";
		std::cin >> times[i];
	}

	float total {};

	for (size_t i {}; i < number; ++i) {
		const auto fee {calfee(times[i])};

		total += fee;
		std::cout << "Customer " << i + 1 << "'s fee for " << times[i] << " = " << fee << '\n';
	}

	std::cout << "\nThe total of the fees is " << total << '\n';
}

float calfee(float time) {
	const auto amount {25.0f + (5.0f * (time - 3.0f))};

	if (amount > 60.0f)
		return 60.0f;

	if (amount < 25.0f)
		return 25.0f;

	return amount;
}

Topic archived. No new replies allowed.