unit price problem

Discussion in 'C++' started by johanjohan, Aug 19, 2007.

  1. johanjohan

    johanjohan New Member

    Joined:
    Aug 19, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    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
     
    Last edited by a moderator: Aug 20, 2007
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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).
     
  3. johanjohan

    johanjohan New Member

    Joined:
    Aug 19, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    that ugly mess was a mistake, some how it multi-copied the program with out me knwoing it.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I have removed the double code as well inserted the code block for you.
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  6. johanjohan

    johanjohan New Member

    Joined:
    Aug 19, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
    Last edited by a moderator: Aug 20, 2007
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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.
     
  8. johanjohan

    johanjohan New Member

    Joined:
    Aug 19, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  9. listendinesh

    listendinesh New Member

    Joined:
    Aug 3, 2007
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    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);
    }
     
    Last edited by a moderator: Aug 20, 2007
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    listendinesh, Please use posts.code block when you have codes snippets in the
     
  11. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    This is a function definition:
    Code:
    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);
    }
    
    It will serve as a declaration if it precedes the call. This is also a definition, but it is not a function.
    Code:
    double unitprice;
    
    So, do you want "unitprice" to be a double or a function? WE don't know. YOU are supposed to know and write the code appropriately.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice