I don't understand the error message, maybe someone else is smarter

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.
 
A

Alexander Block

Joshua said:
/* 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.

Joshua,

Can you post the error message? It would make it easier helping you.

Best,
Alex
 
J

Joshua Moore

Joshua Moore schrieb:


/* 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.

Joshua,

Can you post the error message? It would make it easier helping you.

Best,
Alex

here it comes:
(sorry for the weird formatting).

[moorej@OITLinux ~]$ g++ -Wall -o buysome buysome.cpp
/tmp/ccI1DZlz.o(.text+0x121): In function `main':
: undefined reference to `MenuFruit()'
/tmp/ccI1DZlz.o(.text+0x1f7): In function `main':
: undefined reference to `BuyProduct(std::basic_string<char,
std::allocator<char> > said:
, float&, bool&)'
/tmp/ccI1DZlz.o(.text+0x34b): In function `main':
: undefined reference to `BuyProduct(std::basic_string<char,
std::allocator<char> > said:
, float&, bool&)'
/tmp/ccI1DZlz.o(.text+0x49f): In function `main':
: undefined reference to `BuyProduct(std::basic_string<char,
std::allocator<char> > said:
, float&, bool&)'
collect2: ld returned 1 exit status
 
A

anon

Joshua said:
/* 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.


1) you declared and used
void MenuFruit()
but defined
void MenuFruit(float fruitweight, float totalweight)

2) you declared and used
void BuyProduct(string productname,
float PPRODUCT,
float PRODUCTMAX,
float& qproduct,
string categoryname,
float& categoryweight,
bool& tlreached);
but defined
void BuyProduct(string productname,
float PPRODUCT,
float CATEGORYMAX,
float& qproduct,
float& totalweight,
string categoryname,
float& categoryweight,
bool& tlreached)
 
A

Alexander Block

Joshua said:
/* 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.

1) you declared and used
void MenuFruit()
but defined
void MenuFruit(float fruitweight, float totalweight)

2) you declared and used
void BuyProduct(string productname,
float PPRODUCT,
float PRODUCTMAX,
float& qproduct,
string categoryname,
float& categoryweight,
bool& tlreached);
but defined
void BuyProduct(string productname,
float PPRODUCT,
float CATEGORYMAX,
float& qproduct,
float& totalweight,
string categoryname,
float& categoryweight,
bool& tlreached)


Another thing I see is that you're giving a "const float" value to a
function that receives a non const float reference. I'm not sure why
you have no compile errors for that, but I think it's illegal to do
so.
 
J

Joshua Moore

Another thing I see is that you're giving a "const float" value to a
function that receives a non const float reference. I'm not sure why
you have no compile errors for that, but I think it's illegal to do
so.

I figured the function would not care what exactly it gets (const or
not) as long as it gets the required type.
 
J

Joshua Moore

I figured the function would not care what exactly it gets (const or
not) as long as it gets the required type.

Thanks for your help, Alexander and anon. Now that the syntax errors
are fixed, I can continue working on "saying what I mean" instead of
saying what I think means what I mean ;)
 
H

Howard

----- Original Message -----
From: "Joshua Moore" <[email protected]>
Newsgroups: comp.lang.c++
Sent: Monday, April 16, 2007 1:47 AM
Subject: Re: I don't understand the error message, maybe someone else is
smarter

Thanks for your help, Alexander and anon. Now that the syntax errors
are fixed, I can continue working on "saying what I mean" instead of
saying what I think means what I mean ;)

Just an FYI: those were not syntax errors, actually. Sure, they were caused
by typing the wrong statements, but the errors were generated by the
linker/loader, not the compiler. (Notice the .o extension mentioned in the
error; that means "object" file, an intermediate file generated by that
particular compiler.) The errors are "undefined reference" errors, which
means that you declared something (in this case, some functions), but never
defined them (i.e., you never wrote code which implemented them, at least
not with the correct signature). In this case, fixing the function
declarations and/or definitions fixes the problem, but other times you might
get this error are when you simply forget to write the implementation code
for a function, or when you aren't including the implementation code in your
project/makefile.

-Howard

(P.S., sorry for replying directly to you first; I'm still getting used to
this new newsreader of mine.)
 
O

Old Wolf

Another thing I see is that you're giving a "const float" value to a
function that receives a non const float reference. I'm not sure why
you have no compile errors for that, but I think it's illegal to do
so.

I don't see it; the 'float &' parameters seem to receive arguments
of automatic non-const floats.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top