Help with do while loops!

Please help!! In a little over my head!

I am continuing an assignment that our class has been working on at home for about a week and have run into a snag. We are learning about loops and this week's installment calls for a more complex program and I am having trouble creating it. Here our the directions and what I have thus far...


A rental car company typically has multiple makes and models in its fleet, and they may
want to repeat the mpg computation for each make and model. Also, in order to prevent
the user from adding erroneous values, your program will need to error check the user’s
inputs to the program.

In this phase you will add two features to your program to accomplish these tasks:

1. Add an outer do-while loop that allows the user to repeat the calculations until
they are done. The user must type y or n (lowercase). If they type anything else,
assume they want to repeat the calculation.

2. Assume that no company owns more than 10 fleet vehicles, so error check the
user’s input to ensure they only enter [1, 10] as the number of vehicles they own.

3. Notice on the sample output that if the user types 1, the (es) is removed when the
average mpg is printed. Use an if statement to ensure this happens with your
program.

Match the following prompts and error messages for your program below.

Please enter the make and model of the car: Tesla S
Please enter the number of Tesla S(es) owned (1-10): -3
*** Error *** Enter a number between 1 and 10 (inclusively).
Please enter the number of Tesla S(es) owned (1-10): 15
*** Error *** Enter a number between 1 and 10 (inclusively).
Please enter the number of Tesla S(es) owned (1-10): 3
Please enter the mpg for each car: 48.2 51.7 62.0
Average MPG for 3 Tesla S(es) = 53.97
Do you want to repeat this computation (y/n)? y
Please enter the make and model of the car: Infiniti G37
Please enter the number of Infiniti G37(es) owned (1-10): 4
Please enter the mpg for each car: 28.0 20.0 16.0 12.0
Average MPG for 4 Infiniti G37(es) = 19.00
Do you want to repeat this computation (y/n)? k
Please enter the make and model of the car: Junker 10
Please enter the number of Junker 10(es) owned (1-10): 1
Please enter the mpg for each car: 5
Average MPG for 1 Junker 10 = 5.00
Do you want to repeat this computation (y/n)? n

***You must use a do-while loop to repeat the computation***


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
#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.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
	
	cout << "Average MPG for 6 Tesla S(es)";
	cin >> mpg;
	
	return 0;
You've written most of it. Just put your algorithm in between a do/while (DOW)

1
2
3
4
5
6
char choice;

do {
//Your algorithm goes here..
cout << "Repeat or Quit?"; cin >> choice;
}while(choice == 'y');
So you are saying that most of the content of the program is already there?

I am sorry I'm just having trouble envisioning the algorithm I need to add in here... I had to go to our class's help session and thought I walked out of there understanding all this but it is harder to do on your own.

.
Last edited on
Would this suffice for an IF statement, as requested in #3 of my directions?

1
2
3
4
5
6
if (n ==1) {
cout<< "Average MPG for model_of_car =/n";
}
else if (n > 2) {
cout<< "Average MPG for model_of_car (es)=/n";
}


Last edited on
and would this IF statement(or something similar) complete my task on #2 of the directions? If so I'm really starting to get this!

1
2
3
4
5
6
if(n<1) {
cout<< "***Error*** Enter a number between 1-10\n";
}
else if(n>10){
cout<<"***Error*** Enter a number between 1-10\n";
}
Shouldn't model_of_car be a string?
1
2
3
4
5
6
if(n<1) {
cout<< "***Error*** Enter a number between 1-10\n";
}
else if(n>10){
cout<<"***Error*** Enter a number between 1-10\n";
}
can be written as
1
2
3
4
if(n < 1 || n < 10)
{
    cout<<"***Error*** Enter a number between 1-10\n";
}
Does anyone understand the do while? I am not sure of how to put it into the rest of the code.... also not exactly sure how to build the algorithm inside it to make sure it fits with the instructions. I have been able to figure out #2 and #3 on the problem, however not #1.

As shamieh said earlier, the set up for the do while within this problem is:

1
2
3
4
5
6
char choice;

do {
//Your algorithm goes here..
cout << "Repeat or Quit?"; cin >> choice;
}while(choice == 'y');


Can anyone give me further insight on this and/or its placement within this program?
Help, as always, is much appreciated!
Thanks to giblit, gkraft, and shamieh.
Topic archived. No new replies allowed.