Hey!
I wrote this code for the question: Write a function called findReading. It should be a Reading structure reference variable as its parameter. The function should ask the user to enter values for each member of the structure. My code:
struct TempScale
{
double fahrenheit;
double centigrade;
};
struct Reading
{
int windSpeed;
double humidity;
tempScale temperature;
};
void findReading(struct Reading)
{
cout %26lt;%26lt; "Enter the following values:\n";
cout %26lt;%26lt; "Wind speed: ";
cin %26gt;%26gt; Reading.windSpeed;
cout %26lt;%26lt; "Humidity: ";
cin %26gt;%26gt; Reading.humidity;
cout %26lt;%26lt; "Fahrenheit temperature: ";
cin %26gt;%26gt; TempScale.temperature.fahrenheit;
cout %26lt;%26lt; "Centigrade temperature: ";
cin %26gt;%26gt; TempScale.temperature.centigrade;
}
}
Can u help me fix it or change it? Thanks
Beginner C++ small question?
ur parameter should be
void findReading(Reading input_readings)
input_readings is just a variable name, which is how you are going to refer to the Reading object in your function. Structs are types. So u can use them as types, just like
void some_function(int x), you need void findReadings(Reading name_of_variable);
so if you use the name input_readings, your input it going to look like this
the %26amp; passes it the memory address of the variable so you can directly work on it
void findReading( Reading%26amp; input_reading)
{
cout %26lt;%26lt; "Enter the following values:\n";
cout %26lt;%26lt; "Wind speed: ";
cin %26gt;%26gt; input_reading.windSpeed;
ect. change all of them
also, TempScale is an object inside of readings, so it would look like
cin %26gt;%26gt; input_reading.temperature.Fahrenheit
cout %26lt;%26lt; "Centigrade temperature: ";
cin %26gt;%26gt; input_reading.temperature.centigra...
}
and u have an extra } at the end
hope that helps ya.
Reply:Your problem is the declaration of the function findReading. You write:
void findReading(struct Reading)
{
What you need is:
void findReading(struct Reading %26amp; value)
{
...
cin %26gt;%26gt; value.windSpeed;
...
cin %26gt;%26gt; value.humidity;
// etc
Now, there is a big difference when you use the '%26amp;' in the function signature.
void findReading(struct Reading value)
then you call:
fincReading(reading);
In this case, the function receives a copy of the caller's parameter. Any changes are local to the function are not reflected to the caller.
void findReading(struct Reading %26amp; value)
then you call:
fincReading(reading);
Here, the function instead of receiving a copy, gets a reference. A reference is an object that says "I'm really over there." (it's a hidden pointer). Any changes you make to the reference are made to the original. When you call the function, the parameter you pass can be modified by the function.
Reply:struct TempScale
{
double fahrenheit;
double centigrade;
};
struct Reading
{
int windSpeed;
double humidity;
tempScale temperature;
};
struct Reading is a structure, you need to declare a variable of type struct Reading
In C++ you can define a function with just the type to pass in but this parameter is ignored.
Avoids the unused parameter warning.
void f(int) {}
Call this function as follows: f(0);
The function 'f' doesn't use the value passed in.
To use the value, just change the declaration to
void f(int f)
{if (f) return;} %26lt;%26lt;- Here i use the value.
void findReading(struct Reading %26amp;r)
{
cout %26lt;%26lt; "Enter the following values:\n";
cout %26lt;%26lt; "Wind speed: ";
cin %26gt;%26gt; r.windSpeed;
cout %26lt;%26lt; "Humidity: ";
cin %26gt;%26gt; r.humidity;
tempScale t;
cout %26lt;%26lt; "Fahrenheit temperature: ";
cin %26gt;%26gt; t.fahrenheit;
// You can calculate degree Celsius
t.centigrade = (t.fahrenheit - 32.0) * 5.0 / 9.0;
r.temperature = t;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment