Declaration of float kilos , hours and average shadows a parameter

Good day , I am having the error above Declaration of float kilos shadows a parameter , also for hours and average .I thinking its something to do with the naming but how can i fix this at the same time not "change " the meaning of the code

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

using namespace std;




void printDescription ( )
{
 cout << "**************************************************************" << endl;
 cout << "This program takes two numbers (kilometers and hours)" <<endl;
 cout << "and outputs the average speed travelled" << endl;
 cout << "**************************************************************" << endl;
}

  void computeKilosPerHour (float kilos, float hours, float average)


   {

    float kilos, hours, average;
    average = 0;
    cout << "Welcome to the Travel Calculator" << endl;
    printDescription();
    cout << "Please input the kilometers travelled" << endl;
    cin >> kilos;
    cout <<"Please input the hours travelled" << endl;
    cin >> hours;
    computeKilosPerHour(kilos, hours,average);

   }
      {


         int main()

         average = kilos/hours;
         cout << "Your average speed was" << average << "km per hour" << endl;
         cin >> average;
         cout << "We hope you enjoyed this program" << endl ;
return;
}
closed account (o3hC5Di1)
Hi there,

Your function definition is as such:

void computeKilosPerHour (float kilos, float hours, float average)

Then you go on to declare variable with the exact same names as the parameters passed into the function:

float kilos, hours, average;

Although I see what you're trying to do, the second line is not needed and actually confuses the compiler. The compiler knows what "kilos, hours and average" are because it saw them in the function definition, so it sets aside memory for them. Then you go one and tell the compiler "Please reserve space for hours, kilos and average". So the compiler gets confused.

Just leave line 22 out and you should be fine.

Hope that helps.

All the best,
NwN
Thank you so much , really helped .
Topic archived. No new replies allowed.