Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

whats wrong with this program

johanjohanjohanjohan Member Posts: 10
.
.
I copied this program out of a book. the problem revolves around the word:


unitprice


Its here on this part of the program that the problem comes up

unitprice_small = unitprice(diameter_small, price_small);
unitprice_large = unitprice(diameter_large, price_large);

The compiler is saying that the word..unitprice is not declared.....then when i do declare it, it says its not allowed to be declared.......

I cant figure this out, its crazy

thanks



#include
using namespace std:
int main()
{
int diameter_small, diameter_large;
double price_small, unitprice_small;
double price_large, unitprice_large;


cout << "Welcome to the pizza consumer union.
";
cout << "Enter diameter of a small pizza ( in inches): ";
cin >> diameter_small;
cout << "Enter the price of a small pizza: ";
cin >> price_small;
cout << "Enter diameter of a large pizza: (in inches)";
cin >> diameter_large;
cout <<"Enter the price of a large pizza: ";
cin>> price_large;



unitprice_small = unitprice(diameter_small, price_small);
unitprice_large = unitprice(diameter_large, price_large);

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout<<"small pizza:
";
cout<<"diameter = " <<diameter_small<<"inches
"
<<"Price = $ " <<price_small
<<" per square inch =$" << unitprice_small<<endl
<<"large pizza:
";
cout<< "Diameter = " << diameter_large<< "inches
";
cout<<"Price = " << price_large;
cout<< "per square inch = " << unitprice_large;
if (unitprice_large < unitprice_small)
cout << "The the large one is a better buy
";
else
cout <<"The small one is better buy
";
system("pause");
return 0;

}

double unitprice(int diameter, double price)
{
const double PI = 3.14159;
double radius, area;

radius = diameter/static_cast<double>(2);
area = PI * radius * radius;
return (price/area);
}

Comments

  • zibadianzibadian Member Posts: 6,349
    : .
    : .
    : I copied this program out of a book. the problem revolves around the
    : word:
    :
    :
    : unitprice
    :
    :
    : Its here on this part of the program that the problem comes up
    :
    : unitprice_small = unitprice(diameter_small, price_small);
    : unitprice_large = unitprice(diameter_large, price_large);
    :
    : The compiler is saying that the word..unitprice is not
    : declared.....then when i do declare it, it says its not allowed to
    : be declared.......
    :
    : I cant figure this out, its crazy
    :
    : thanks
    :
    :
    :
    : #include
    : using namespace std:
    : int main()
    : {
    : int diameter_small, diameter_large;
    : double price_small, unitprice_small;
    : double price_large, unitprice_large;
    :
    :
    : cout << "Welcome to the pizza consumer union.
    ";
    : cout << "Enter diameter of a small pizza ( in inches): ";
    : cin >> diameter_small;
    : cout << "Enter the price of a small pizza: ";
    : cin >> price_small;
    : cout << "Enter diameter of a large pizza: (in inches)";
    : cin >> diameter_large;
    : cout <<"Enter the price of a large pizza: ";
    : cin>> price_large;
    :
    :
    :
    : unitprice_small = unitprice(diameter_small, price_small);
    : unitprice_large = unitprice(diameter_large, price_large);
    :
    : cout.setf(ios::fixed);
    : cout.setf(ios::showpoint);
    : cout.precision(2);
    : cout<<"small pizza:
    ";
    : cout<<"diameter = " <<diameter_small<<"inches
    "
    : <<"Price = $ " <<price_small
    : <<" per square inch =$" << unitprice_small<<endl
    : <<"large pizza:
    ";
    : cout<< "Diameter = " << diameter_large<< "inches
    ";
    : cout<<"Price = " << price_large;
    : cout<< "per square inch = " << unitprice_large;
    : if (unitprice_large < unitprice_small)
    : cout << "The the large one is a better buy
    ";
    : else
    : cout <<"The small one is better buy
    ";
    : system("pause");
    : return 0;
    :
    : }
    :
    : double unitprice(int diameter, double price)
    : {
    : const double PI = 3.14159;
    : double radius, area;
    :
    : radius = diameter/static_cast<double>(2);
    : area = PI * radius * radius;
    : return (price/area);
    : }
    :
    Why ask this at a Basic language board, while the language this program is written in is C?
  • MT2002MT2002 Member Posts: 1,444

    You need to [b]foward declare[/b] the function (or define it above main()).

    To foward declare, just declare it before it's first use (i.e., above main()):
    [code]
    //Put above your main() routine
    double unitprice(int diameter, double price);
    [/code]

    Remember that in C++, [b]everything must be defined before they are used[/b]. Your original code uses this routine without it ever being defined.

    Either move your function above main(), or foward declare it.

    ---------

    I do have to also ask why put this in this forum though o.0?

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.mt2002.sitesled.com]Website :: OS Development Series[rightbr][/link][/size]
Sign In or Register to comment.