-1

So I have a pretty simple code to calculate the speed of sound using an input form the user, when I run the program I get an answer but its not correct and I get the error

'Line 14: assignment makes integer from pointer without cast'.

I don't know what that means and have tried adjusting my pointer and function to try and fix this problem. Any help would be appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int SpeedofSound(int t,int Answer);
int t,Answer;
int *pAnswer;

int main(void)
{
    printf("Please enter a Temp (Fahrenheit) to calculate the speed of sound.\n");
    scanf(" %d", &t);

    Answer = SpeedofSound;
    printf("At Temp %d, the Speed of sound is %d feet/second.", t, Answer);
    return 0;
}

int SpeedofSound(int t,int Answer)
{
    *pAnswer = 1086 * sqrt(((5 * t) + 297)/247);
    Answer = *pAnswer;
    return (Answer);
}
user3386109
  • 34,287
  • 7
  • 49
  • 68
  • 1
    You obviously know how to call functions (hint: both `printf` and `scanf` are functions), so why don't you *call* your function? You *do* however need to learn more about pointers. And pointer are not needed here. I suggest you [find a good beginners book to read](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – Some programmer dude Oct 27 '16 at 17:30
  • Bear in mind that the function argument `int t` has no relation to the global variable `int t`, and ditto for `Answer`. The function will use the one with the closest scope - the function argument. The globals `int t, Answer;` actually belong in `main`. The function does not need the second argument `Answer` because the computation result is returned as the function value. – Weather Vane Oct 27 '16 at 17:35
  • `*pAnswer = 1086 * sqrt(((5 * t) + 297)/247);` is your next problem. `pAnswer` doesn't point to valid memory, you need to allocate it (I'm ignoring the overall design issues as this is obviously practice, but using a global pointer and mutating it from within that function is not a great way to accomplish what you want here.). – Ed S. Oct 27 '16 at 17:37
  • Yet another, is that you might need to do floating point calculations instead of integer calculations. – Weather Vane Oct 27 '16 at 17:39
  • *None* of your global variables are necessary (though you might want an `int Answer` declared in `main()`) . You don't need to declare global copies of variables used within your functions. – Dmitri Oct 27 '16 at 17:59
  • You can just do `return 1086 * sqrt(((5 * t) + 297)/247);` – SIGSTACKFAULT Oct 27 '16 at 18:10

2 Answers2

0

Here's your problem:

Answer = SpeedofSound;

This is not a function call, but an assignment. You assign the address of the function to an integer variable named Answer. Do this instead:

Answer = SpeedofSound(t);

and rewrite the function to take one argument. Drop the pAnswer stuff. Good luck

Bjorn A.
  • 1,148
  • 9
  • 12
0

So here's what the compiler is complaining about:

Answer = SpeedofSound;

You're not actually calling the SpeedofSound function - since you left off the () function call operator, the compiler iterprets SpeedofSound as a pointer to the function. So in the line above, you're trying to assign a pointer value to an int, which isn't allowed without a cast.

You have a lot going on in your code that isn't necessary at all: You can rewrite it as

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int SpeedofSound(int t);

int main(void)
{
    int t;
    int Answer;

    printf("Please enter a Temp (Fahrenheit) to calculate the speed of sound.\n");
    scanf(" %d", &t);

    Answer = SpeedofSound( t ); 
    printf("At Temp %d, the Speed of sound is %d feet/second.", t, Answer);
    return 0;
}

int SpeedofSound(int t)
{
    return 1086 * sqrt(((5 * t) + 297)/247);
}

You don't need any pointers or global variables. All you need is t and Answer.

Note that integer arithmetic gives integer results; i.e., 1/2 == 0. You might want to use doubles for your inputs and calculations:

double SpeedofSound( double t )
{
  return 1086.0 * sqrt(((5.0 * t) + 297.0)/247.0);
}

int main( void )
{
  double t;
  double Answer;
  ...
}

You'll also need to replace %d with %f in your printf and scanf calls.

John Bode
  • 119,563
  • 19
  • 122
  • 198