J
Joshua Moore
/* Hi, I was hoping someone could help me with this problem. I did my
work and worked my way through the usual compiler messages, but I have
run against some problem I can't identify. The compiler error message
is unintelligable -- to me anyway. Anyway: Here is the code, maybe
someone can tell me what is wrong with it. */
//buysome.cpp
//Joshua Moore
//the part responsible for the buying part of the farm pos simulation
program
#include <iostream>
#include <iomanip>
using namespace std;
const float PAPPLES = 1.99;
const float PPEARS = 2.59;
const float PGRAPES = 1.49;
const float MAXFRUIT = 30;
const float MAXTOTAL = 100;
const char EXIT = 'q'; //This is the escape character
void BuyProduct(string productname,
float PPRODUCT,
float PRODUCTMAX,
float& qproduct,
string categoryname,
float& categoryweight,
bool& tlreached);
void MenuFruit();
float WeightLeft(float categoryweight, float totalweight, float
CATEGORYMAX);
int CatOrTotalLeftSmaller(float categoryweight, float totalweight,
float CATEGORYMAX);
//Let the fun begin:
int main()
{
float qapples, qpears, qgrapes;
float papples, ppears, pgrapes;
char choice;
float categoryw;
bool tlreached = false;
do
{
MenuFruit();
cin >> choice;
switch (choice)
{
case 'q':
break;
case 'a':
BuyProduct("Apples", PAPPLES, MAXFRUIT, qapples, "Fruit",
categoryw, tlreached);
break;
case 'p':
BuyProduct("Pears", PPEARS, MAXFRUIT, qpears, "Fruit",
categoryw, tlreached);
break;
case 'g':
BuyProduct("Grapes", PGRAPES, MAXFRUIT, qgrapes, "Fruit",
categoryw, tlreached);
break;
default:
cout << "Please enter a valid choice." << endl;
} //exit switch
}while((choice!=EXIT) && (tlreached = false));
//the rest is just here to display everything
cout << qapples << " lbs of Apples purchased for: $" <<
setprecision(2) << papples << "." << endl;
cout << " " << qpears << " lbs of Pears purchased for: $" <<
setprecision(2) << ppears << "." << endl;
cout << qgrapes << " lbs of Grapes purchased for: $" <<
setprecision(2) << pgrapes << "." << endl;
return 0;
} //exit main
void MenuFruit(float fruitweight, float totalweight)
{
cout << "You have room for " << WeightLeft(fruitweight, totalweight,
MAXFRUIT);
cout << " pounds of Fruit." << endl;
cout << " [a] Apples, " << setprecision(2) << PAPPLES << " per
pound" << endl;
cout << " [p] Pears, " << setprecision(2) << PPEARS << " per
pound" << endl;
cout << " [g] Grapes, " << setprecision(2) << PGRAPES << " per
pound" << endl;
cout << "What would you like to buy?" << endl;
cout << "Enter 'q' to go back to the main menu" << endl;
}
//This function returns how much more product can be purchased with
respect to
//both the limits on each category of product and the limits on the
total purchase
//Returns 0 if something is seriously fubr
float WeightLeft(float categoryweight, float totalweight, float
CATEGORYMAX)
{
float categoryleft = CATEGORYMAX-categoryweight;
float totalleft = MAXTOTAL - totalweight;
if (categoryleft <= totalleft)
return categoryleft;
else if (totalleft < categoryleft)
{
if (totalleft <= CATEGORYMAX)
return totalleft;
else if (CATEGORYMAX <= totalleft)
return CATEGORYMAX;
}
return 0;
} //exit Weightleft
//returns 1 if the amount left for the category is smaller than the
amount left for the total
//returns 2 if the amount left for the total is smaller than the
amount left for the category
//returns 3 if the amounts are the same
//returns 0 if something is seriously fubr
int CatOrTotalLeftSmaller(float categoryweight, float totalweight,
float CATEGORYMAX)
{
float categoryleft = CATEGORYMAX - categoryweight;
float totalleft = MAXTOTAL - totalweight;
if (categoryleft < totalleft) return 1;
if (categoryleft > totalleft) return 2;
if (categoryleft = totalleft) return 3;
return 0;
} //exit CatOrTotalLeftSmaller
void BuyProduct(string productname,
float PPRODUCT,
float CATEGORYMAX,
float& qproduct,
float& totalweight,
string categoryname,
float& categoryweight,
bool& tlreached)
{ //start BuyProduct
float left;
float tempproduct;
cout << productname << " cost $" << setprecision(2) << PPRODUCT <<
"per pound." << endl;
cout << "How many pounds of " << productname << " would you like to
purchase?" << endl;
cin >> tempproduct;
left = WeightLeft(categoryweight, totalweight, MAXFRUIT);
if (tempproduct < left) //
qproduct = tempproduct;
categoryweight = categoryweight + qproduct;
totalweight = totalweight + qproduct;
if (tempproduct >= left)
{
switch (CatOrTotalLeftSmaller(categoryweight, totalweight,
MAXFRUIT))
{
case 1:
cout << "You have reached the maximum weight for " <<
categoryname << "." << endl;
cout << "Fortunately, there was still enough left for " <<
left;
cout << " pounds of " << productname << "," << endl;
cout << "so that amount has been added to your purchase
instead." << endl;
qproduct = left;
categoryweight = categoryweight + left;
totalweight = totalweight + left;
break;
case 2:
case 3:
cout << "You have reached the maximum weight for your total
purchase." << endl;
cout << "Fortunately, there was still enough left for " <<
left;
cout << " pounds of " << productname << "," << endl;
cout << "so that amount has been added to your purchase
instead." << endl;
cout << "Next, we will direct you to the Shipping Method
Selection." << endl;
qproduct = left;
categoryweight = categoryweight + left;
totalweight = totalweight + left;
tlreached = true;
break;
} //exit switch
} //exit if
} //exit BuyProduct.
work and worked my way through the usual compiler messages, but I have
run against some problem I can't identify. The compiler error message
is unintelligable -- to me anyway. Anyway: Here is the code, maybe
someone can tell me what is wrong with it. */
//buysome.cpp
//Joshua Moore
//the part responsible for the buying part of the farm pos simulation
program
#include <iostream>
#include <iomanip>
using namespace std;
const float PAPPLES = 1.99;
const float PPEARS = 2.59;
const float PGRAPES = 1.49;
const float MAXFRUIT = 30;
const float MAXTOTAL = 100;
const char EXIT = 'q'; //This is the escape character
void BuyProduct(string productname,
float PPRODUCT,
float PRODUCTMAX,
float& qproduct,
string categoryname,
float& categoryweight,
bool& tlreached);
void MenuFruit();
float WeightLeft(float categoryweight, float totalweight, float
CATEGORYMAX);
int CatOrTotalLeftSmaller(float categoryweight, float totalweight,
float CATEGORYMAX);
//Let the fun begin:
int main()
{
float qapples, qpears, qgrapes;
float papples, ppears, pgrapes;
char choice;
float categoryw;
bool tlreached = false;
do
{
MenuFruit();
cin >> choice;
switch (choice)
{
case 'q':
break;
case 'a':
BuyProduct("Apples", PAPPLES, MAXFRUIT, qapples, "Fruit",
categoryw, tlreached);
break;
case 'p':
BuyProduct("Pears", PPEARS, MAXFRUIT, qpears, "Fruit",
categoryw, tlreached);
break;
case 'g':
BuyProduct("Grapes", PGRAPES, MAXFRUIT, qgrapes, "Fruit",
categoryw, tlreached);
break;
default:
cout << "Please enter a valid choice." << endl;
} //exit switch
}while((choice!=EXIT) && (tlreached = false));
//the rest is just here to display everything
cout << qapples << " lbs of Apples purchased for: $" <<
setprecision(2) << papples << "." << endl;
cout << " " << qpears << " lbs of Pears purchased for: $" <<
setprecision(2) << ppears << "." << endl;
cout << qgrapes << " lbs of Grapes purchased for: $" <<
setprecision(2) << pgrapes << "." << endl;
return 0;
} //exit main
void MenuFruit(float fruitweight, float totalweight)
{
cout << "You have room for " << WeightLeft(fruitweight, totalweight,
MAXFRUIT);
cout << " pounds of Fruit." << endl;
cout << " [a] Apples, " << setprecision(2) << PAPPLES << " per
pound" << endl;
cout << " [p] Pears, " << setprecision(2) << PPEARS << " per
pound" << endl;
cout << " [g] Grapes, " << setprecision(2) << PGRAPES << " per
pound" << endl;
cout << "What would you like to buy?" << endl;
cout << "Enter 'q' to go back to the main menu" << endl;
}
//This function returns how much more product can be purchased with
respect to
//both the limits on each category of product and the limits on the
total purchase
//Returns 0 if something is seriously fubr
float WeightLeft(float categoryweight, float totalweight, float
CATEGORYMAX)
{
float categoryleft = CATEGORYMAX-categoryweight;
float totalleft = MAXTOTAL - totalweight;
if (categoryleft <= totalleft)
return categoryleft;
else if (totalleft < categoryleft)
{
if (totalleft <= CATEGORYMAX)
return totalleft;
else if (CATEGORYMAX <= totalleft)
return CATEGORYMAX;
}
return 0;
} //exit Weightleft
//returns 1 if the amount left for the category is smaller than the
amount left for the total
//returns 2 if the amount left for the total is smaller than the
amount left for the category
//returns 3 if the amounts are the same
//returns 0 if something is seriously fubr
int CatOrTotalLeftSmaller(float categoryweight, float totalweight,
float CATEGORYMAX)
{
float categoryleft = CATEGORYMAX - categoryweight;
float totalleft = MAXTOTAL - totalweight;
if (categoryleft < totalleft) return 1;
if (categoryleft > totalleft) return 2;
if (categoryleft = totalleft) return 3;
return 0;
} //exit CatOrTotalLeftSmaller
void BuyProduct(string productname,
float PPRODUCT,
float CATEGORYMAX,
float& qproduct,
float& totalweight,
string categoryname,
float& categoryweight,
bool& tlreached)
{ //start BuyProduct
float left;
float tempproduct;
cout << productname << " cost $" << setprecision(2) << PPRODUCT <<
"per pound." << endl;
cout << "How many pounds of " << productname << " would you like to
purchase?" << endl;
cin >> tempproduct;
left = WeightLeft(categoryweight, totalweight, MAXFRUIT);
if (tempproduct < left) //
qproduct = tempproduct;
categoryweight = categoryweight + qproduct;
totalweight = totalweight + qproduct;
if (tempproduct >= left)
{
switch (CatOrTotalLeftSmaller(categoryweight, totalweight,
MAXFRUIT))
{
case 1:
cout << "You have reached the maximum weight for " <<
categoryname << "." << endl;
cout << "Fortunately, there was still enough left for " <<
left;
cout << " pounds of " << productname << "," << endl;
cout << "so that amount has been added to your purchase
instead." << endl;
qproduct = left;
categoryweight = categoryweight + left;
totalweight = totalweight + left;
break;
case 2:
case 3:
cout << "You have reached the maximum weight for your total
purchase." << endl;
cout << "Fortunately, there was still enough left for " <<
left;
cout << " pounds of " << productname << "," << endl;
cout << "so that amount has been added to your purchase
instead." << endl;
cout << "Next, we will direct you to the Shipping Method
Selection." << endl;
qproduct = left;
categoryweight = categoryweight + left;
totalweight = totalweight + left;
tlreached = true;
break;
} //exit switch
} //exit if
} //exit BuyProduct.