CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2002
    Posts
    30

    writing functions

    I have to write a program that finds the unit price for a pizza. Input consists of price and size of pizza. I'm a beginner on writing functions.

    Use functions to:
    1. get input from user
    2. calculate price per square inch
    3. print results

    How do i square radius? i tried square(radius) but it won't work.

    Thanks


    // This program finds the price per square inch of a pizza when size and price is input by the user

    #include <iostream.h>
    #include <math.h>

    void area ();
    // prototype for area function
    void input (float &size, float &price);
    // prototype for input function

    void print_results (float price_per_sq_inch);
    // prototype for print_results function

    float price, size, wait;
    // stores price and size of pizza
    float price_per_sq_inch;
    // used to calculate price per square inch

    main() // main function

    {

    input (size, price); // calls input function
    area (); // calls area function
    print_results (price_per_sq_inch); // calls print_results function

    cin >> wait;
    return 0;
    }


    void input (float &size, float &price)
    // input function
    {
    cout << "What is the size of the pizza (diameter)? ";
    // asks user to input size of pizza
    cin >> size;
    // stores size

    cout << "What is the price of the pizza? ";
    // asks user for price of pizza
    cin >> price;
    // stores price
    }


    void area () // area function
    {
    float radius;

    // radius is declared as float

    const float PI = 3.14159;
    // declares PI as a floating constant

    radius = size/2;


    price_per_sq_inch = (PI * square(radius)/price); // **THIS LINE**


    // calculates price per square inch
    }

    void print_results (float price_per_sq_inch)
    // print_results function

    {
    // outputs results
    cout << "The price per square inch for a " << size << " inch pizza is $" << price_per_sq_inch;
    }
    Last edited by gurlygurlz; February 5th, 2003 at 09:52 PM.

  2. #2
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    Why doesn't it work?

    You can use the ^ operator for exponents. ie.

    2 ^ 5 = 32

    int x = 2;
    x ^= 5; // or y = x ^ 5;
    // x == 32
    Last edited by mwilliamson; February 5th, 2003 at 10:22 PM.

  3. #3
    Join Date
    Oct 2002
    Posts
    30
    it says illegal use of floating point

  4. #4
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Isn't that '^' operator is used for bit-wise XOR operation?

    For calculating square, we can use

    double x=5.0;
    double square = pow(x, 2.0);

    Alternatively,

    square = x * x;

  5. #5
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    yeah, what am I thinking... i've been using vb too much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured