function within struct and calling values by reference/pointer

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.

code snippets:


struct allData {
char selection;
double centigrade;
double fahrenheit;
double temperature;
double ConvertToC (const double& temperature);
double ConvertToF (const double& temperature);
} allData;

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) {

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

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;

}
Last edited on
Hi,

1
2
3
4
5
6
7
struct allData {
double centigrade; 
double fahrenheit; 
double temperature; 
double ConvertToC (const double& temperature);
double ConvertToF (const double& temperature);
} allData;//pretty sure this is wrong 


you have assigned the same var name as to struct name.

also

cin >> allData.selection;

could you tell me where is allData.selection defined? Because I cant seem to find it...
PS: welcome to cplusplus.com :)
Apologies, in creating code snippets for my post (to post more concisely) I removed my definintion of allData.selection from the post but not my program. However, it is contained within the struct definition with all of the other variables (see below). Additionally, I tested your theory on naming the struct variable in visual studio and found that the name of the struct variable is fine.

struct should read:

struct allData{
char selection;
double centigrade;
double fahrenheit;
double temperature;
double ConvertToC (const double& temperature);
double ConvertToF (const double& temperature);
} allData;
Topic archived. No new replies allowed.