Hi,
I need to write a n00b program that calculates the best deal customers can get when buying pizza. The premise is, although an extra large pizza may appear a better buy, you need to calculate the cost per sq inch of both round and rectangular pizzas to really know.

What I need to do:
-use function overloading
-write functions that pass by value and by reference
-use the SAME named function twice(for a total of 4 functions), call them getData and computeUnitCost

The first function gets all user input for round pizza, and must store the values it gets from the user where they can be accessed by other parts of the program(no global variables)length, width, price.

The 2nd function gets all user input for square pizza and the rest of the info like the first function.


The 3rd and 4th functions are computeUnitCost and do the computations. One takes the diameter and price as parameters, and returns the cost of 1 sq in of pizza. The other take the length and price and returns the cost. The function should be overloaded.

I need some help with how to call the functions and how to set up the overloading plz? :)
Here's what I have so far:

#include <iostream>

using namespace std;

char pizzaType;
double getData(int, int, int);

int main()
{
        bool okay = true;
        do {
        cout << "Please enter in the type of pizza: " << endl;
        cout << "r - a round pizza" << endl;
        cout << "s - a square pizza" << endl;
        cin >> pizzaType;
        
        cin.ignore(80, '\n');
        
        okay = (pizzaType == 'r' || pizzaType == 's');
        
            cout << "\nYou entered an invalid pizza type!" << endl;
        }while(!okay);
        if (pizzaType == 'r')
            
        double totalPrice = getData(length, width, price);
        cout << "The price per square inch for this pizza is " << totalPrice << endl;
    return 0;    
}

double getData(int length, int width, int price)
    {
        
        cout << "Enter in the length of the pizza: " << endl;
        cin >> length;
        cout << "Enter in the width of the pizza: " << endl;
        cin >> width;
        cout << "Enter in the price of the pizza: " << endl;
        cin >> price;
        
    }
double getData(int diamter, int price)
    {
        cout << "Enter in the diameter of the pizza: " << endl;
        cin >> diameter;
        cout << "Enter in the the price of the pizza: " << endl;
        cin >> price;
    }

double computeUnitCost(diameter, price)
    {
        roundCost = diameter / price;
    return roundCost;
    cout << "The price per square inch for this pizza is: " << roundCost << endl;
}
double computeUnitCost(length, width, price)
    {
        squareCost = ((lenth * width))/ price;
    return squareCost;
    cout << "The price per square inch for this pizza is: " << squareCost << endl;
    }

when declaring a function you must specify the parameter types as well as name. For example

double computeUnitCost(int length,int width,int price)
{
   // the code goes here
}

It is also necessary to declare the functions before they are used, so you need to put the function prototypes at the top of the program. Just simply copy the function declaraction shown above and paste it near the top of the program then terminate that line with a semicolon.

Thanks for the help. HOw do I call the different functions in main to make it work?

Also, I am not sure where to 'reference' the function. I know I need to use the apersand '&' but not sure where to stick it.

the parameters of getData() need to be passed by reference so that getData() can change their values. So you make the function like this

double getData(int & length, int & width, int & price)
{

}

Thanks again, I think I just need to see how to call it in the main function, because I'm really not sure. Also, do I have the rest set up correctly?

>>I think I just need to see how to call it in the main function
looks ok to me, but I didn't attempt to compile your program.

double getData(int diamter, int price)

Above function needs similar conversion to use references.

I get this error:

error: no matching function for call to `getData()'
pizza.cpp:6: note: candidates are: double getData(int, int, int)
pizza.cpp:7: note:                 double getData(int, int)
pizza.cpp:28: warning: unused variable 'roundPrice'
pizza.cpp:29: error: `roundPrice' undeclared (first use this function)
pizza.cpp:29: error: (Each undeclared identifier is reported only once for each function it appears in.)
pizza.cpp: In function `double computeUnitCost(int, int, int)':
pizza.cpp:60: error: `lenth' undeclared (first use this function)
pizza.cpp:63:3: warning: no newline at end of file

pizza.o - 6 error(s), 2 warning(s)

pizza.cpp:60: error: `lenth' undeclared (first use this function)

I think you mean 'length' instead of 'lenth'

error: no matching function for call to `getData()'

You are trying to call the function getData() somewhere without passing any parameters.

I can't say anything about the rest, because you didn't post your latest code (yet?)

Edited 6 Years Ago by Nick Evan: n/a

Okay still getting errors, and I don't know if what I have is actually going to do what I need it to do, but here goes:

sing namespace std;

char pizzaType;
void getData(int length, int width, int price);
void getData(int diamter, int price);
double computeUnitCost(int diameter, int price);
double computeUnitCost(int length, int width, int price);

int main()
{
        bool okay = true;
        do {
        cout << "Please enter in the type of pizza: " << endl;
        cout << "r - a round pizza" << endl;
        cout << "s - a square pizza" << endl;
        cin >> pizzaType;
        
        cin.ignore(80, '\n');
        
        okay = (pizzaType == 'r' || pizzaType == 's');
        
            cout << "\nYou entered an invalid pizza type!" << endl;
        }while(!okay);
        if (pizzaType == 'r')
            
        getData(int&, int&);
        cout << "The price per square inch for this pizza is " << roundPrice << endl;
    //return 0;
        if (pizzaType == 's')
            
         squarePrice = getData();
        cout << "The price per square inch for this pizza is " << squarePrice << endl;
}

void getData(int& length, int& width, int& price)
    {
        
        cout << "Enter in the length of the pizza: " << endl;
        cin >> length;
        cout << "Enter in the width of the pizza: " << endl;
        cin >> width;
        cout << "Enter in the price of the pizza: " << endl;
        cin >> price;
        
    }
void getData(int& diameter, int& price)
    {
        cout << "Enter in the diameter of the pizza: " << endl;
        cin >> diameter;
        cout << "Enter in the the price of the pizza: " << endl;
        cin >> price;
    }

double computeUnitCost(int diameter, int price)
    {
        int roundCost = diameter / price;
    return roundCost;
    cout << "The price per square inch for this pizza is: " << roundCost << endl;
}
double computeUnitCost(int length, int width, int price)
    {
        int squareCost = ((length * width))/ price;
    return squareCost;
    cout << "The price per square inch for this pizza is: " << squareCost << endl;
    }

I'm almost positive the bottom functions aren't right, i just put them together.

How do I pass parameters? That's where I am hung up :(

Okay still getting errors, and I don't know if what I have is actually going to do what I need it to do, but here goes:

using namespace std; // u wasnt there
 
char pizzaType; // dont make this global, put it in main
void getData(int& length, int& width, int& price); // changed to pass by reference
void getData(int& diameter, int& price);
double computeUnitCost( int diameter, int price);
double computeUnitCost( int length, int width, int price);
 
int main()
{
        bool okay = true;
        do {
        cout << "Please enter in the type of pizza: " << endl;
        cout << "r - a round pizza" << endl;
        cout << "s - a square pizza" << endl;
        cin >> pizzaType;
 
        cin.ignore(80, '\n');
 
        okay = (pizzaType == 'r' || pizzaType == 's');
 
            cout << "\nYou entered an invalid pizza type!" << endl;
        }while(!okay);
        if (pizzaType == 'r')
 
       // getData(int&, int&); The most incorrect way of calling functions
        cout << "The price per square inch for this pizza is " << roundPrice << endl;
    //return 0;
        if (pizzaType == 's')
 
         squarePrice = getData();
 
// roundprice and squareprice are not declared
        cout << "The price per square inch for this pizza is " << squarePrice << endl;
 
// price computation functions never called.
}
 
void getData(int& length, int& width, int& price)
    {
 
        cout << "Enter in the length of the pizza: " << endl;
        cin >> length;
        cout << "Enter in the width of the pizza: " << endl;
        cin >> width;
        cout << "Enter in the price of the pizza: " << endl;
        cin >> price;
 
    }
void getData(int& diameter, int& price)
    {
        cout << "Enter in the diameter of the pizza: " << endl;
        cin >> diameter;
        cout << "Enter in the the price of the pizza: " << endl;
        cin >> price;
    }
 
double computeUnitCost(int diameter, int price)
    {
        int roundCost = diameter / price;
    return roundCost;
    cout << "The price per square inch for this pizza is: " << roundCost << endl;
}
double computeUnitCost(int length, int width, int price)
    {
        int squareCost = ((length * width))/ price;
    return squareCost;
    cout << "The price per square inch for this pizza is: " << squareCost << endl;
    }

I'm almost positive the bottom functions aren't right, i just put them together.

How do I pass parameters? That's where I am hung up :(

Many mistakes in your program to begin with not to mention the spelling mistakes. I have marked the problem areas in red so you sould be able to make out the mistakes.

Make these changes and repost.

PS: Dont you have a good reference book to refer to ? IF not say in the next post.

I have Big C++ but the pages listed for referenceing are not very helpful. Nor, is my C++ for Dummies :)

#include <iostream>

using namespace std;

void getData(int& length, int& width, int& price);
void getData(int& diameter, int& price);
double computeUnitCost(int diameter, int price);
double computeUnitCost(int length, int width, int price);

int main()
{
        char pizzaType;
        bool okay = true;
        do {
        cout << "Please enter in the type of pizza: " << endl;
        cout << "r - a round pizza" << endl;
        cout << "s - a square pizza" << endl;
        cin >> pizzaType;
        
        cin.ignore(80, '\n');
        
        okay = (pizzaType == 'r' || pizzaType == 's');
        
            cout << "\nYou entered an invalid pizza type!" << endl;
        }while(!okay);
        if (pizzaType == 'r')
        int roundPrice;
        roundPrice = getData();
        
        cout << "The price per square inch for this pizza is " << roundPrice << endl;
        if (pizzaType == 's')
        int squarePrice;    
        squarePrice = getData();
        cout << "The price per square inch for this pizza is " << squarePrice << endl;
        
        computeUnitCost();
}

void getData(int& length, int& width, int& price)
    {
        
        cout << "Enter in the length of the pizza: " << endl;
        cin >> length;
        cout << "Enter in the width of the pizza: " << endl;
        cin >> width;
        cout << "Enter in the price of the pizza: " << endl;
        cin >> price;
    }

void getData(int& diameter, int& price)
    {
        cout << "Enter in the diameter of the pizza in: " << endl;
        cin >> diameter;
        cout << "Enter in the the price of the pizza in: " << endl;
        cin >> price;
    }

double computeUnitCost(int diameter, int price)
    {
        int roundCost = diameter / price;
    cout << "The price per square inch for this pizza is: " << roundCost << endl;
    return roundCost; // not sure if i need to return this or if its the right way
}

double computeUnitCost(int length, int width, int price)
    {
        int squareCost = ((length * width))/ price;
    cout << "The price per square inch for this pizza is: " << squareCost << endl;
        return squareCost; // not sure if this is the right way to return it

    }

Okay I'll check that out. In the mean time, I've got my code closer to done, but not quite. What else needs changing?

ps. If I have this:
int roundPrice;
roundPrice = getData();

Why do i still get an undeclared error?

That is because your &quot;getData()&quot; returns void but you are expecting it to return a value by putting a L value at the LHS of the function. You are trying to assign data to the variable by means of a function which actually doesnt return anything. That is because your &quot;getData()&quot; returns void but you are expecting it to return a value by putting a L value at the LHS of the function. You are trying to assign data to the variable by means of a function which actually doesnt return anything.

This article has been dead for over six months. Start a new discussion instead.