Originally Posted by
David Reghay
Here's the tidied up code. I tried to use all your suggestions. I don't know how to turn up the warning level on my compiler, if anyone can help with that. I'm using visual studio express. Please let me know if this is still a sloppy implementation.
Not sloppy, but I think you misunderstood laserlight's suggestion. It was to make dollars a parameter to the function. A function should do one thing and do it well. If it's job is to convert, it should only convert, not take user input also. Another benefit of doing this is that your function can better be reused. Imagine you want to read dollar values in from a file in some sort of automated script (e.g. runs overnight, with no user). You don't want to write a whole separate convert function to do that, you want to reuse the convert function you already have. You can't do that if the function requires user input though. Also, convert is a bit generic for a name, it could convert anything to anything. Try something like convert_dollars_to_pounds. Here's the way I would write it:
Code:
#include <stdio.h>
float convert_dollars_to_pounds(float dollars);
int main(void)
{
float dollars;
float pounds;
printf("Enter a dollar amount: $");
scanf("%f", &dollars);
pounds = convert_dollars_to_pounds(dollars);
printf("That's equal to %.2f pounds!\n", pounds);
return 0;
}
float convert_dollars_to_pounds(float dollars)
{
return dollars*2;
}