Answer to Question #59290 in C++ for Fady.s

Question #59290
Keeping track of and converting between US fluid quantities can be quite confusing and challenging, given all those tablespoons, ounces, quarts, etc. The relationships between all of these fluid units are:

1 fluid ounce = 2 tablespoons
1 gill = 4 fluid ounces (fl oz)
1 cup = 2 gills
1 pint = 2 cups
1 quart = 2 pints
1 gallon = 4 quarts
1 barrel = 42 gallons

Write a program that asks the user to input a number of fluid ounces. Allocate the fluid ounces into the various units, first starting with barrels, then gallons, and so on, finishing with tablespoons.

Sample 1:
How many fluid ounces do you have? 77
77 fluid ounces can be divided into:
0 barrel(s)
0 gallon(s)
2 quart(s)
0 pint(s)
1 cup(s)
1 gill(s)
2 tablespoons

Sample 2:
How many fluid ounces do you have? 6555
6555 fluid ounces can be divided into:
1 barrel(s)
9 gallon(s)
0 quart(s)
1 pint(s)
1 cup(s)
0 gill(s)
6 tablespoons
1
Expert's answer
2016-04-21T11:57:05-0400

#include <iostream>
#include <cstring>
#include <iomanip>

using namespace std;



int main()

{

const int fluid_ounce_to_tablespoons = 2;
const int gill_to_fluid_ounces = 4;
const int cup_to_gills = 2;
const int pint_to_cups = 2;
const int quart_to_pints = 2;
const int gallon_to_quarts = 4;
const int barrel_to_gallons = 42;
int tablespoon, ounce, gill, cup, pint, quart, gallon, barrel;

int barrel_to_ounces = barrel_to_gallons* gallon_to_quarts*quart_to_pints*
pint_to_cups*cup_to_gills*gill_to_fluid_ounces;
int gallon_to_ounces = gallon_to_quarts*quart_to_pints*
pint_to_cups*cup_to_gills*gill_to_fluid_ounces;
int quart_to_ounces = quart_to_pints*pint_to_cups*
cup_to_gills*gill_to_fluid_ounces;
int pint_to_ounces = pint_to_cups*cup_to_gills*gill_to_fluid_ounces;
int cup_to_ounces = cup_to_gills*gill_to_fluid_ounces;

//input ounces
cout << "\nHow many fluid ounces do you have? ";
cin >> ounce;

cout << endl << ounce << " fluid ounces can be divided into:\n";

barrel = ounce / barrel_to_ounces;
ounce -= barrel * barrel_to_ounces;
cout << barrel << " barrel(s) \n";

gallon = ounce / gallon_to_ounces;
ounce -= gallon * gallon_to_ounces;
cout << gallon << " gallon(s) \n";

quart = ounce / quart_to_ounces;
ounce -= quart * quart_to_ounces;
cout << quart << " quart(s) \n";

pint = ounce / pint_to_ounces;
ounce -= pint * pint_to_ounces;
cout << pint << " pint(s) \n";

cup = ounce / cup_to_ounces;
ounce -= cup * cup_to_ounces;
cout << cup << " cup(s) \n";

gill = ounce / gill_to_fluid_ounces;
ounce -= gill * gill_to_fluid_ounces;
cout << gill << " gill(s) \n";

tablespoon = ounce*fluid_ounce_to_tablespoons;
cout << tablespoon << " tablespoons \n";

cin.get();
cin.get();
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS