Click to See Complete Forum and Search --> : writing functions


gurlygurlz
February 5th, 2003, 08:30 PM
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;
}

mwilliamson
February 5th, 2003, 09:09 PM
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

gurlygurlz
February 5th, 2003, 09:17 PM
it says illegal use of floating point

Kheun
February 5th, 2003, 09:28 PM
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; :)

mwilliamson
February 5th, 2003, 09:42 PM
yeah, what am I thinking... i've been using vb too much ;)