//4.13: Gas Mileage
//Develop a C++ program that uses a WHILE statement to input the miles driven and gallons used for each trip.
//include statements
#include <iostream>
#include <iomanip>
using namespace std;
//initialize variable for miles driven to 0.
unsigned int milesDriven{0};
//initialize variable for total miles driven to 0.
unsigned int milesTotal{0};
//initialize variable for gallons used to 0.
unsigned int gallonsUsed{0};
//initialize variable for total gallons used to 0.
unsigned int gallonsTotal{0};
unsigned int counter{0};
int main(){
//while input value is greater than -1:
do {
//prompt user to input number of miles driven.
cout << "Enter miles driven (-1 to quit): ";
cin >> milesDriven; //obtain user input
if (milesDriven == -1) {
cout << "MPG calculation complete." << endl;
} else {
//prompt user to input gallons used.
cout << "Enter gallons used: ";
cin >> gallonsUsed;
//add to milestotal and gallonstotal
milesTotal = milesTotal + milesDriven;
gallonsTotal = gallonsTotal + gallonsUsed;
//turn mpg into a double and calculate
double mpgTrip{static_cast<double>(milesDriven) / gallonsUsed};
//display mpg
cout << "MPG this trip: " << mpgTrip << endl;
//turn total mpg into a double
double mpgTotal{static_cast<double>(milesTotal) / gallonsTotal};
//display total mpg
cout << "Total MPG: " << mpgTotal << endl;
counter = counter + 1;
}
} while (milesDriven!= -1);
}
//This should allow the user to input miles and gallons, receive an average per trip,
//and total average mpg. When the user presses -1 as miles driven, the program will cancel. And it did!
//4.17 : Find the Largest
//write a c++ program that uses a while statement to determine the largest of 10 numbers input by the user.
#include <iostream>
#include <string>
using namespace std;
//initialize variables:
unsigned int counter{1}; //to count to 10, initialize to 1
unsigned int number{}; //to input the current number into the program
unsigned int largest{};
int main() {
//while counter is less than or equal to 10:
std::cout << "Please enter 10 numbers." << std::endl;
while (counter <= 10) {
//prompt user to input an integer
std::cout << "Number " << counter << ": " << endl;
std::cin >> number; //obtain user input
if (number > largest) {
largest = number;
}
counter++;
}
std::cout << "The largest number is: " << largest << std::endl;
}
//This program should prompt the user 10 times, and output the largest number inputted at the end of the program.