Mixed Nuts (v. 2)
Program Listing
 This is an improved version of Mixed Nuts.
 This version includes error checking to see if the input is sensible.
MixedNuts.cpp
/****************************************************************************
* *
* Program: MixedNuts *
* *
* Purpose: This program will calculate the percent of peanuts and *
* the percent of cashews to mix to attain a specified *
* price for the mixture. *
* *
****************************************************************************/
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {
// Define program constants
const float PEANUT_PRICE = 5.00; // Price per lb of peanuts
const float CASHEW_PRICE = 10.00; // Price per lb of cashews
// Get the desired price of the mix
cout << "Enter the desired price of the mix: " << flush;
float MixPrice; // Price of the mixed nuts
cin >> MixPrice;
// Compute the amounts of peanuts and cashews
float PeanutPart = (MixPrice  CASHEW_PRICE)/(PEANUT_PRICE  CASHEW_PRICE);
// Fraction of mix that is peanuts
float CashewPart = 1  PeanutPart; // Fraction that is cashews
float PeanutPercent = round(100*PeanutPart); // Express as a percent
float CashewPercent = round(100*CashewPart); // Express as a percent
// Print the results
cout << "The mix should contain " << PeanutPercent << "% peanuts and ";
cout << CashewPercent << "% cashews" << endl;
return 0;
}

Sample Run #1
Enter the desired price of the mix: 8.50
The mix should contain 30% peanuts and 70% cashews

Sample Run #2
Enter the desired price of the mix: 12.00
That price is not within the proper range.

Sample Run #3
The peanut and cashew prices are equal, so it doesn't matter how much of each you mix. The mix price will be $10 per pound.

Return to Lectures page
email me at
robbk@hsc.edu
This page was last modified on Tue Dec 7 16:04:31 1999
.
URL: http://people.hsc.edu/facultystaff/robbk/Coms261/Examples/MixedNuts2.html
Return to Coms 261 home page