0

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; 

}
2
  • 3
    &allData::ConvertToC is the address of the method, your are not calling it, you need to do allData.ConvertToC(allData.temperature). Commented Jun 23, 2016 at 13:53
  • This is also the correct answer, I believe! Thank you! Commented Jun 23, 2016 at 17:49

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Note: it's bad practice to call endl repeatedly like that, because it unecessarily flushes the stream. You should instead just add \ns to the stream, and flush it at the end if you really need to.
Indeed it is, my own fault for copying and pasting without checking the rest of the line! I'll edit now.
Thanks everyone- I will use \n instead of endl. However, I have made the following edits to no avail. I converted '&allData::ConvertToC' to 'allData::ConvertToC(allData.temperature)'. I now get a compiler error: "cannot call member function ‘double allData::ConvertToC(double)’ without object case 'c': { allData::ConvertToC(allData.temperature)"
I've edited my answer again, it should be "allData.ConvertToC" and not "allData::ConvertToC", sorry!
Thank you! I believe this has worked. I will need to test it further, later, though.
0
// 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;
}

1 Comment

This also worked correctly. There was redundancy in my previous function definitions, which you pointed out as well. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.