mardi 14 juin 2016

C Program That asks If the User wants to Convert from C to F then does the conversion

I have an assignment to create a program that asks the user what temperature they want to convert and then convert it, but my if statements are not working.

Here is my code:

#include <stdio.h>
#include <string.h>

int main(void) {
//Declare Variables
float ce; //Celcius
float fa; //Fahrenheit
char in; //Input form user
float go; //If a loop should keep running

//Ask the user what they would like to convert
printf("Would you like to convert from Celsius 'C' or Fahrenheit 'F'?\n");

//Get the input from the user
scanf("%d", &in);

//Start the Loop
go = 1;
while (go == 1) {
    if (in == 'F')  { //If the user wants to convert from Fahrenheit
        //Stop the loop
        go = 0;

        //Ask the user the temperature they would like to convert
        printf("What temperature F would you like to convert to C?\n");

        //Get the input from the user
        scanf("%d", &fa);

        //Crunch the Numbers
        ce = ((fa - 32)/1.8);

        //Print the answer
        printf("The temperature in Celcius is %lf\n", ce);

    } else if (in == 'C') { //If the user wants to convert from Celcius
        //Stop the loop
        go = 0;

        //Ask the user the temperature they would like to convert
        printf("What temperature C would you like to convert to F?\n");

        //Get the input from the user
        scanf("%d", &ce);

        //Crunch the Numbers
        fa = (32 + (ce * 1.8));

        //Print the answer
        printf("The temperature in Fahrenheit is %lf\n", fa);

    } else {
        //make sure the loop will continue running
        go = 1;

        //Print an error message
        printf("That is an invalid input, please enter either a 'C' of 'F'\n");

        //Take the input again
        scanf("%d", &in);
    }
}

//End the program
return 0;

}

No matter what i enter, It keeps coming back with "That is an invalid input..."

How can i get my if statements to work? I have also tried strcmp only to get a "Comparison between pointer and integer" Error. This should be a fairly simple and basic program and yet I can't get it to work.

Aucun commentaire:

Enregistrer un commentaire