Reputation: 11
All- I've researched this quite a bit. My program compiles without error, but the values from the functions within the struct are not passing to the program. Can you help me to figure out why they are not? I included the snippets of code that show the components in question. Mainly, my code like: "&allData::ConvertToC" is not returning any value from the function within the struct "allData". It only will return a value of 1, no matter the input of "allData.temperature". I know the all components of the program, other than those mentioned are working.
code snippets:
//defining the struct
struct allData {
char selection;
double centigrade;
double fahrenheit;
double temperature;
double ConvertToC (const double& temperature);
double ConvertToF (const double& temperature);
} allData;
//adding data to the struct for the functions within the struct to use
cout << "Enter C for converting your temperature to Celsius, or enter F for converting your temperature to Fahrenheit, and press ENTER." << endl << endl;
cin >> allData.selection;
cout << "Enter your starting temperature to two decimal places, and press ENTER." << endl << endl;
cin >> allData.temperature;
switch (allData.selection) {
//my attempt to reference the functions within the struct and the data in the struct, but it is not working and always returns a value of 1.
case 'c': { &allData::ConvertToC;
cout << "Your temperature converted to Celsius is: " << &allData::ConvertToC
<< endl << endl;
break;
}
case 'C': { &allData::ConvertToC;
cout << "Your temperature converted to Celsius is: " << &allData::ConvertToC
<< endl << endl;
}
}
//Function definitions that are located in the struct. Do I define the functions in the normal way, like this, if they are located in the struct?
double allData::ConvertToF (const double& temperature) {
double fahrenheit = 0;
fahrenheit = temperature * 9 / 5 + 32;
return fahrenheit;
}
double allData::ConvertToC (const double& temperature) {
double centigrade = 0;
centigrade = (temperature - 32) * 5 /9;
return centigrade;
}
Upvotes: 0
Views: 144
Reputation: 46
// Name of struct made distinct from its instance, for clarity.
struct AllData {
char selection;
double centigrade;
double fahrenheit;
double temperature;
double ConvertToC ();
double ConvertToF ();
} allData;
...
allData.selection = 'C';
allData.temperature = 74.5;
switch (allData.selection)
{
case 'c':
case 'C':
cout << "Your temperature converted to Celsius is: " << allData.ConvertToC() << endl << endl;
break;
}
...
double AllData::ConvertToF ()
{
//double fahrenheit = 0; Why not store the result in the struct?
fahrenheit = temperature * 9 / 5 + 32;
return fahrenheit;
}
double AllData::ConvertToC ()
{
//double centigrade = 0;
centigrade = (temperature - 32) * 5 / 9;
return centigrade;
}
Upvotes: 0
Reputation: 1301
You're not executing the function call, you're just passing a function pointer to the cout stream.
I think what you really want is something like:
cout << "Your temperature converted to Celsius is: " << allData.ConvertToC(allData.temperature) << endl;
In addition you don't need to pass by reference in the "ConvertToC" method as you're not really saving anything (a "double" is 8 bytes wide, and references/pointers are 4 bytes on 32 bit systems, or 8 bytes on 64 bit systems).
Upvotes: 1