Loops! Help :p

Hello!
I am having a difficult time building this program, it is our first time using loops and our professor didnt spend a lot of time going over it. We spent the class transferring a for loop to a while, which did help me see what the function looks like, but being able to incorporate it is a different story! Please someone look at what I have here and try and help me build this!(Not a HW Grade, dont worry)

" OVERVIEW
Scientists will often write programs to perform calculations on some experimental observations,
such as the amount of rainfall or the miles per gallon (mpg) achieved by various cars. In this lab you
are going to practice using loops to calculate the average mpg achieved by a fleet of rental cars.
DEVELOPMENT
You will need to handle 3 pieces of information: Make of the car, Model of the car, Number of cars
owned. Then, you will use a “for” loop to input a variable number of decimals representing miles
per gallon for this problem. Then finally, output the average MPG as a decimal to two (2) decimal
points of precision.
Use the following sample to develop your prompts and inputs, with the user input highlighted in
blue. Your prompts and outputs must match these exactly.
Please enter the make and model of the car: Tesla S
Please enter the number of Tesla S(es) owned: 6
Please enter the mpg for each car: 81.2 93.1 72.9 92.4 89.7 88.6
Average MPG for 6 Tesla S(es) = 86.32
***You must use a for loop to solve this problem***"

* I have some work already completed for this word problem, however it is so minimal I feel it not even necessary to add. Here is what I have started of my loop(that is more than likely wrong)

1
2
3
4
5
6
 double mpg;
	int n;
	cin >> n;
	for (int i=0; i < n; i++) {
		cin >> mpg;
	}
Average = sum of values / number of values.

You already have a variable with number of values. You need to create variable to hold sum of values and add to it each value after you read it.
Then you will just divide sum by your n variable, and this will be the average.
Ok I think I understand... I'm sorry would you mind just showing me an example of a line(if asking for a larger portion is too much of an inconvenience) of that new variable?
Also is my previous work on pace to be correct?
1
2
3
4
5
6
double sum = 0.0; //We need to initialize it to zero.

//...

double average = sum / n;
//Use average whichever way you like. 
Also is my previous work on pace to be correct?
Yes, it would work fine. Just add output statements for it to look like in example.
Okay so I thought I had done everything right but apparently not...
Can someone diagnose this and see what I am doing wrong?

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

int	main()
{	
	int model_of_car, n;
	double sum = 0.0; mpg
	
	cout << "Please enter the make and model of the car:";
	cin >> model_of_car;
	cout << "Please enter the number of Tesla(es) owned:";
	cin >> n;
	cout << "Please enter the mpg for each car:";
	cin >> mpg;
	
	
	{double mpg;
	int n;
	cin >> n;
	for (int i=0; i < n; i++) {
		cin >> mpg;
		double average = sum/n;
	}}
	return 0;
}


And should I put the "Average MPG for 6 Tesla S(es)" dead last?
Last edited on
stdin copy
Standard input is empty

compilation info
prog.cpp: In function 'int main()':
prog.cpp:7:20: error: 'mpg' was not declared in this scope
double sum = 0.0; mpg

stdout
Standard output is empty
Why you are entering n twice? Why there are stray mpg reading outside of the loop?

Why are there two variables called mpg? Why are there extra set of braces at lines 17 and 23?

Walk through your code saying out lous what each line do. You should find some problems with program flow.
You were right! Honestly, I did not re read my work and had made some stupid errors. I believe I have everything in order now! I think lol!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using	namespace	std;

int	main()
{	
	int model_of_car, n;
	double sum = 0.0, mpg;
	
	cout << "Please enter the make and model of the car:";
	cin >> model_of_car;
	cout << "Please enter the number of Tesla(es) owned:";
	cin >> n;
	cout << "Please enter the mpg for each car:";
	cin >> mpg;
	
	for (int i=0; i < n; i++) {
		cin >> mpg;
		double average = sum/n;
	}
	cout << "Average MPG for 6 Tesla S(es)";
	cin >> mpg;
	
	return 0;
}


It still isnt running exactly right, all my output statements come consectively and will not let me enter a value. Does anyone know why this would happen?
Last edited on
does anyone see what I have wrong?
Why are you entering mpg outside of the loop? Why are average calculation inside of the loop? Where do you calculate sum of all mpg?
If I understand what you are trying to do this should be the code below. First for model of car unless no chars are going to be entered I would change to string and you aren't actually adding the mpg together to average them.
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
#include <iostream>
#include <string>
using	namespace	std;

int	main()
{	
	string model_of_car;
	int n;
	double sum = 0.0, mpg_1, average;
	
	cout << "Please enter the make and model of the car:";
	cin >> model_of_car;
	cout << "Please enter the number of Tesla(es) owned:";
	cin >> n;
	cout << "Please enter the mpg for each car:";
	
	for (int i=0; i < n; i++) {
		cin >> mpg_1;
		int mpg =mpg_1;
		sum +=mpg;
	}
	cout << "Average MPG for " << n << " Tesla S(es)";
	average = sum/n;
	cout << average;
	
	return 0;
}
Topic archived. No new replies allowed.