Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   unit price problem (http://www.go4expert.com/forums/unit-price-problem-t5936/)

johanjohan 20Aug2007 02:47

unit price problem
 
The word "unitprice" in this part of the program

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

is giving me problems

The compiler says : "unitprice has not been declared".....then when i declare it its says : "unitprice can not be used as a function"

This thing is crazy , I dont get it.

I copied this program out of a c++ book from college

Thanks

Code:

#include <iostream>
using namespace std;
int main()
{
int diameter_small, diameter_large, unitprice;
double price_small, unitprice_small;
double price_large, unitprice_large;


cout << "Welcome to the pizza consumer union.\n";
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:\n";
cout<<"diameter =  " <<diameter_small<<"inches\n"
    <<"Price = $ " <<price_small
    <<" per square inch =$" <<  unitprice_small<<endl
    <<"large pizza:\n";
cout<< "Diameter =  " << diameter_large<< "inches\n";
cout<<"Price = " << price_large;
cout<< "per square inch = " << unitprice_large;
    if (unitprice_large < unitprice_small)
        cout << "The the large one is a better buy\n";
        else
        cout <<"The small one is  better buy\n";
        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);
}

Edited for code tags

DaWei 20Aug2007 05:30

Re: unit price problem
 
Please be polite enough to read the "Before you make a query" thread (upper right hand corner of the page).

I refuse to read all that ugly mess, but I read the top part. A function must be declared OR defined (your choice) before it is called. If it is not, it will cause an error in C++. C will assume that it takes one int and returns an int (which would obviously cause errors in your code).

johanjohan 20Aug2007 07:34

Re: unit price problem
 
that ugly mess was a mistake, some how it multi-copied the program with out me knwoing it.

shabbir 20Aug2007 07:55

Re: unit price problem
 
Quote:

Originally Posted by johanjohan
that ugly mess was a mistake, some how it multi-copied the program with out me knwoing it.

I have removed the double code as well inserted the code block for you.

DaWei 20Aug2007 07:56

Re: unit price problem
 
It still needed code tags to preserve the formatting. I've done that for you, but I am not your mama. Please go ahead and read the indicated thread.

The fix for your problem is in my original response.

johanjohan 20Aug2007 08:01

Re: unit price problem
 
I don't think you understand what Im saying


When i compile the program the way it is I get an error message that says "unitprice has not been declared".

when I declare it such as:

int diameter_small, diameter_large, unitprice;
double price_small, unitprice_small;
double price_large, unitprice_large;
double unitprice;

or

int diameter_small, diameter_large, unitprice;
double price_small, unitprice_small;
double price_large, unitprice_large, unitprice;

or

double unitprice;

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

Which means that Im declaring it.

Then i compile it , it says "unitprice can not be used as a function"


So if I cant declare it and cant use it as a function after i declre it, then how am i suppose to use it in the program?

Thanks

Code:

#include <iostream>
using namespace std;
int main()
{
int diameter_small, diameter_large, unitprice;
double price_small, unitprice_small;
double price_large, unitprice_large;


cout << "Welcome to the pizza consumer union.\n";
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:\n";
cout<<"diameter = " <<diameter_small<<"inches\n"
<<"Price = $ " <<price_small
<<" per square inch =$" << unitprice_small<<endl
<<"large pizza:\n";
cout<< "Diameter = " << diameter_large<< "inches\n";
cout<<"Price = " << price_large;
cout<< "per square inch = " << unitprice_large;
if (unitprice_large < unitprice_small)
cout << "The the large one is a better buy\n";
else
cout <<"The small one is better buy\n";
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);
}

how do you edit these posts, after i created the first post I couldnt get back to edit it.

shabbir 20Aug2007 08:05

Re: unit price problem
 
You probably is not getting. You should have the code blocks if you have code snippets in the posts so that its readable to all.

About post editing you need some 10 or 20 ( I can't remember right now ) to edit your own posts and that is kept for stopping spam and self promotion.

johanjohan 20Aug2007 12:43

Re: unit price problem
 
the code is written in green and black

the part:

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

is written in green


near the top they have


double unitprice(int diameter, double price) <--- thats the decloration

I didnt see it at all till now because its written i green and there are three lines of notes in greeen also below it so i didnt even see it.

I forgot about how to declare functions. Thats why i kept looking at the int and double declorations after int main(). Then i seen the function decloration among the notes ( all in green)and it all came back to me. Oh yea, oh yea, thats how Im supppose to do it, I forgot. i hadn't opened my book up in about a year and forgot about declorations of functions.

Well , Im back on track now.

sorry about that

thanks

listendinesh 20Aug2007 12:47

Re: unit price problem
 
things will get well just declare that function before main.
Like this...
Code:

#include <iostream>
using namespace std;

double unitprice(int diameter, double price);

int main()
{
int diameter_small, diameter_large;
double price_small, unitprice_small;
double price_large, unitprice_large;


cout << "Welcome to the pizza consumer union.\n";
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:\n";
cout<<"diameter = " <<diameter_small<<"inches\n"
<<"Price = $ " <<price_small
<<" per square inch =$" << unitprice_small<<endl
<<"large pizza:\n";
cout<< "Diameter = " << diameter_large<< "inches\n";
cout<<"Price = " << price_large;
cout<< "per square inch = " << unitprice_large;
if (unitprice_large < unitprice_small)
cout << "The the large one is a better buy\n";
else
cout <<"The small one is better buy\n";
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);
}


shabbir 20Aug2007 13:53

Re: unit price problem
 
listendinesh, Please use posts.code block when you have codes snippets in the


All times are GMT +5.5. The time now is 16:53.