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?
9 2737
#include <iostream>
using namespace std;
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;
}
int main () {
int diameter;
int price;
getData( diameter, price);
cout << "diameter of the pizza is: " diameter << " price is: " << price << endl;
return 0;
}
Hmm for some reason I put that, and I get:
pizza.cpp
pizza.cpp: In function `int main()':
pizza.cpp:27: warning: unused variable 'diameter'
pizza.cpp:29: error: `diameter' undeclared (first use this function)
pizza.cpp:29: error: (Each undeclared identifier is reported on
And I have: -
int diameter;
-
int price;
-
getData(diameter, price);
-
What's wrong?
Its hard to find where the problem is, can you show me your file?
On my system I get error in this line - cout << "diameter of the pizza is: " diameter << " price is: " << price << endl;
so I have used this - cout << "diameter of the pizza is: " <<diameter << " price is: " << price << endl;
Price is part of an equation, so I don't think that will work. Here's the whole file: -
#include <iostream>
-
-
using namespace std;
-
-
char pizzaType;
-
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()
-
{
-
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 diameter;
-
int price;
-
void getData();
-
cout << "The price per square inch for this pizza is " << getData(diameter, price) << endl;
-
//return 0;
-
if (pizzaType == 's')
-
-
int squarePrice = getData(int& length, int& width, int& price);
-
cout << "The price per square inch for this pizza is " << getData << 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;
-
double computeUnitCost(int&, int&);
-
}
-
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
-
-
}
remember to pass by reference if you want a return value be put into the referenced object. use const reference if you want a value that is never changed in your function scope.
you have to change:
void getData(int length, int width, int price);
void getData(int diameter, int price);
void getData(int& length, int& width, int& price);
void getData(int& diameter, int& price);
Your function prototypes have to match your function declarations. In the code you posted you have neglected to include the reference operator "&" in your parameter lists.
woops. I had them there originally, then took them out for some reason. I think i messing around with using string instead of void.
Here's what I need to accomplish:
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: J.R. |
last post by:
Hi folks,
The python can only support passing value in function call (right?), I'm
wondering how to effectively pass a large parameter, such as a large list or
dictionary?
It could achieved...
|
by: Mr A |
last post by:
Hi!
I've been thinking about passing parameteras using references instead
of pointers in order to emphasize that the parameter must be an
object.
Exemple:
void func(Objec& object); //object...
|
by: Zlatko Matić |
last post by:
Hi everybody!
Recently I was struggling with client/server issues in MS Access/PostgreSQL
combination.
Although Access is intuitive and easy to use desktop database solution, many
problems...
|
by: NorrYtt |
last post by:
I am having a problem with passing a function as a parameter to my DLL
so it can call it back with updates. The DLL is written in LabWindows
CVI with some #ifdef 'C'-style wrappings. The...
|
by: David++ |
last post by:
Hi folks,
I would be interested to hear peoples views on whether or not 'pass by
reference' is allowed when using a Web Service method.
The thing that troubles me about pass-by-reference into...
|
by: Abhi |
last post by:
I wrote a function foo(int arr) and its prototype
is declared as foo(int arr); I modify the values of the array in the
function and the values are getting modified in the main array which
is...
|
by: giloosh |
last post by:
can i pass a hash as a function parameter. ive seen it been used
before but i can't figure out how to do it.
i would like to call a function like this for example
...
|
by: raylopez99 |
last post by:
Keywords: scope resolution, passing classes between parent and child
forms, parameter constructor method, normal constructor, default
constructor, forward reference, sharing classes between forms....
|
by: Davy |
last post by:
Hi all,
Sometimes I need to pass same parameter in recursive function. From my
point of view, the style is redundant, and I don't what to use some
global style like self.A, self.B, Is there any...
|
by: soni2926 |
last post by:
hi,
is it possible to pass a function into another function as a
parameter? Say i have these:
function SaveMe(text)
{...}
function SaveMeNow(text)
{...}
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |