[error] 'variable' previously declared here

This is just a simple distance per tank calculator given fuel economy for variables. Exactly what does this error mean in relation to my code? ----[error] 'double totalMPG' previously declared here------

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
  //This program calculates the distance a car can travel on one tank of gas 
#include <iostream>
using namespace std;

int main()

{

double  cityMPG = 21.5, 	//Miles per gallon in city
	highwayMPG = 26.8,     //Miles per gallon on highway
	totalMPG,	          //To hold total city and highway MPG combined
	avgMPG,               //To hold average of city and highway mileage
	tankCapacity = 20,   //Capacity of gas tank 	       
	distance, 	        //To hold distance car can travel


//Calculate the total combined city and highway mpg
totalMPG = cityMPG + highwayMPG;

//Calculate average mileage
avgMPG = totalMPG / 2;

//Calculate distance car can travel
distance = avgMPG * tankCapacity;

//Display distance car can travel
cout << "The car can travel a distance of " << distance << " miles" << endl;

return 0;

}
closed account (3hM2Nwbp)
Looks like you want a semicolon instead of a comma at line 14.
You need to replace the comma on line 14 with a semicolon.

Your compiler thinks that totalMPG is going to be another variable because of that comma.
Got it, thanks!
Topic archived. No new replies allowed.