How do I call a function outside of main or use a parameter to pass a reference?

If my function is:

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;
    }

how would I call it in main()?

Would it be getData(diameter, price) ?
or some variable = getData(diameter, price) ? I can't figure it out.
or getData(int&, int&) ?
Yes, it was declared at the first part of the program. I get an error saying it expected a primary expression before int?

How do I call a function outside of main or use a parameter to pass a reference?

If my function is:

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;
    }

how would I call it in main()?

Would it be getData(diameter, price) ?
or some variable = getData(diameter, price) ? I can't figure it out.
or getData(int&, int&) ?
Yes, it was declared at the first part of the program. I get an error saying it expected a primary expression before int?

int a,b;
getData(a,b);

For some reason then, this:

int diameter, price;    
getData( diameter, price);

gives an error that
Compiling...
pizza.cpp
pizza.cpp: In function `int main()':
pizza.cpp:27: warning: unused variable 'diameter'
pizza.cpp:27: warning: unused variable 'price'
pizza.cpp:28: error: `diameter' undeclared (first use this function)
pizza.cpp:28: error: (Each undeclared identifier is reported only once for each function it appears in.)
pizza.cpp:28: error: `price' undeclared (first use this function)

Also, I need to reference it somehow, how do i do that?

For some reason then, this:

int diameter, price;    
getData( diameter, price);

gives an error that
Compiling...
pizza.cpp
pizza.cpp: In function `int main()':
pizza.cpp:27: warning: unused variable 'diameter'
pizza.cpp:27: warning: unused variable 'price'
pizza.cpp:28: error: `diameter' undeclared (first use this function)
pizza.cpp:28: error: (Each undeclared identifier is reported only once for each function it appears in.)
pizza.cpp:28: error: `price' undeclared (first use this function)

Also, I need to reference it somehow, how do i do that?

you cant use the same same name if you wonna do so
change the parameters name inside the function...

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

Dont talk in riddles, post the entire code and we will help you out.

It's getting near the deadline and I can't figure it out: I am to use two different functions using the same name, and then pass the variables by value and by reference and NOT use pointers.

Here's what needs to be done:

Your program should include

  1. A function named getData( )that gets all of the user input for a round pizza. For a round pizza it will ask for the diameter of the pizza and its price. Each piece of data entered by the user must be validated to ensure that it is a non-zero, positive value. If a value is zero or negative, tell the user that the input is invalid and go back and ask for that data to be input again. Somehow, this function must store the values it gets from the user where they can be accessed by other parts of the program. You cannot use global variables! The return type of the function is void.
  2. Another function named getData( )that gets all of the user input for a square pizza. For a square pizza it will ask for the length and width of the pizza and its price. Each piece of data entered by the user must be validated to ensure that it is a non-zero, positive value. If a value is zero or negative, tell the user that the input is invalid and go back and ask for that data to be input again. Somehow, this function must store the values it gets from the user where they can be accessed by other parts of the program. You cannot use global data. The return type of the function is void.
  3. A function named computeUnitCost( ) that takes the diameter of a round pizza and its price as parameters, and returns the cost of 1 square inch of that pizza.
  4. Another function named computeUnitCost( ) that takes the length and width of a rectangular pizza and its price, and returns the cost of 1 square inch of that pizza. Note that the function computeUnitCost( ) is overloaded.

What I have:

#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);
        double price = 0.0;
        
        if (pizzaType == 'r')
        {
        int diameter = 0;    
        getData(diameter, price);
        cout << "The price per square inch for this pizza is " << computeUnitCost(diameter, price) << endl;
        }
        if (pizzaType == 's')
        {
        int length = 0;
        int width = 0;
        
        getData(length, width, price);
        cout << "The price per square inch for this pizza is " << computeUnitCost(length, width, price) << endl;
        }
        computeUnitCost();
        
        system("PAUSE");
}

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

    }

Maybe some changes incorporated would do the job...
Try to understand the changes made and learn something from the differences in what you had written prev and this code...

#include <iostream>
#include <cstring>
using namespace std;
const double PI = 3.14 ;
void getData(double& length, double& width, double& price);
void getData(double& diameter, double& price);
double computeUnitCost(double diameter, double price);
double computeUnitCost(double length, double width, double price);
int main()
{
        char pizzaType = '\0';
        bool okay = true;
        double price = 0.0 ;
        do {
        cout << "R => round pizza\tS => square pizza" << endl << endl1;
        cout << "Please enter in the type of pizza: " ;
        cin >> pizzaType;
        pizzaType = tolower( pizzaType ) ;
        cin.clear() ;
        cin.ignore(numeric_limits<streamsize>::max() , '\n');
        okay = (pizzaType == 'r' || pizzaType == 's');
        if( !okay )
            cout << "\nYou entered an invalid pizza type!" << endl;
        }
        while(!okay);
        if (pizzaType == 'r')
        {
        double diameter = 0 ;
        getData(diameter, price);
        cout << "The price per square inch for this pizza is " << computeUnitCost(diameter, price) << endl;
        }
        else
        {
        double length = 0, width = 0 ;
        getData(length, width, price);
        cout << "The price per square inch for this pizza is " << computeUnitCost(length, width, price) << endl;
        }
        cin.get( ) ;
        return 0 ;
}
void getData(double& length, double& width, double& price)
{
    cout << "Enter in the length of the pizza: " ;
    cin >> length;
    cout << "Enter in the width of the pizza: " ;
    cin >> width;
    cout << "Enter in the price of the pizza: " ;
    cin >> price;
}
void getData(double& diameter, double& price)
{
    cout << "Enter in the diameter of the pizza in: " ;
    cin >> diameter;
    cout << "Enter in the the price of the pizza in: " ;
    cin >> price;
}
double computeUnitCost(double diameter, double price)
{
    return ( (diameter / 2) * (diameter / 2) * PI)   / price ;
}
double computeUnitCost(double length, double width, double price)
{
    return (length * width) / price ;
}

Wanted to say thanks! I did follow the logic all the way through and it was frustrating to see how close I was :). Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.