Johanis
Johanis

Reputation: 3

Trouble going from Celsius to Fahrenheit

I just started learning C today so this might seem like a simple question but I'm having trouble running my program. I'm trying to implement a program that converts from Celsius to Fahrenheit. The formula for converting from Celsius to Fahrenheit is T(F)=T(C)*9/5+32. The degrees In Celsius is given as 30.

int main()
{
    int DegreesCelsius;
    printf("30 Degrees Celsius:");
    scanf("%d",&30 );
    printf("The temperature in F is %d\n", 30 * 9/5 + 32);

    return 0;
}

My work is shown above. The problem is on the line containing scanf where I'm told "1value required as unary & operand"

Am I using the ampersand incorrectly? Thanks

Upvotes: 0

Views: 83

Answers (1)

Chris Loonam
Chris Loonam

Reputation: 5745

You need to scan the entered value into DegreesCelsius, and then use that variable to perform the conversion.

int DegreesCelsius;
printf("Degrees Celsius: ");
scanf("%d", &DegreesCelsius);
printf("The temperature in F is %d\n", DegreesCelsius * 9/5 + 32);

&30 is invalid syntax, as 30 is an integer literal and cannot be addressed by &; this is why you have DegreesCelsius, to hold the value the user enters.

Upvotes: 3

Related Questions