Looping question?

Feb 25, 2017 at 6:24pm
So, I'm trying to figure out how to get everything within the cout lines to repeat if the user puts 'Y' in the "Would you like to enter information for another car?". How would I go about that?

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;

const double GALLONS_PER_LITER = .264179;

int main()
{

	int liters = 0;
	double distance = 0;
	double mpg = distance /(liters * GALLONS_PER_LITER);
	cout << "Enter the number of liters consumed by the car: " << endl;
	cin >> liters;
	
	cout << "Enter the number of miles traveled: " << endl;
	cin >> distance;
	

	cout << "This car delivers " << (distance/liters) << "miles per gallon." << endl;
	
	cout << "Would you like to enter information for another car?  Enter 'Y' to enter again or any other key to quit: " << endl;
	
	if (cin = 'Y')
	
	else
	exit(0);
}
Feb 25, 2017 at 6:31pm
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
#include <iostream>
#include <ctype.h>
using namespace std;

const double GALLONS_PER_LITER = .264179;

int main()
{

	int liters = 0;
	double distance = 0;
	double mpg = distance / (liters * GALLONS_PER_LITER);
	char run = 'Y';

	while (run == 'Y')
	{
		cout << "Enter the number of liters consumed by the car: " << endl;
		cin >> liters;

		cout << "Enter the number of miles traveled: " << endl;
		cin >> distance;


		cout << "This car delivers " << (distance / liters) << "miles per gallon." << endl;

		cout << "Would you like to enter information for another car?  Enter 'Y' to enter again or any other key to quit: " << endl;
		cin >> run;
		toupper(run);

	}


	return 0;
}
Last edited on Feb 25, 2017 at 6:32pm
Feb 25, 2017 at 8:39pm
@ joe864864,

When I compiled the program here the compiler gave me a warning about line 28 "statement has no effect". what you need here is run = toupper(run); or change the while condition to while (toupper(run) == 'Y'). Either one will work.

Also llSPEEDll the variable "mpg" is never used.

Hope that helps,

Andy

P.S. you are calculating the value of "mpg" in the wrong place. The formula would work out as
mpg = 0 / (0 * .264179) which is "0 / 0". If you would print "mpg" at line 14 it would say nan (not a number).
Last edited on Feb 25, 2017 at 9:20pm
Topic archived. No new replies allowed.