-2

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.

G. Santos
  • 3
  • 3
  • 1
    It is somewhat unlikely that a decimal integer is `'F'` or `'C'`. On ascii, the numeric values of these characters are `70` and `67`, respectively. – EOF Jun 14 '16 at 15:42
  • 3
    `scanf("%d", &in);`, how? `in` is a `char`... – Sourav Ghosh Jun 14 '16 at 15:44
  • 1
    `scanf("%d", &in);` is looking for a number. `scanf("%c", &in);` scans for a letter. See: [scanf](http://en.cppreference.com/w/cpp/io/c/fscanf). – 001 Jun 14 '16 at 15:44

3 Answers3

1

Problem :

In your code in is declared as char but,you are using wrong format specifier in scanf() i.e, %d while scanning in..

scanf("%d",&in)


Solution:

to avoid scan using %c as format specifier in scanf()

scanf(" %c",&in)

Note: I've given space infront of %c to consume white spaces that might come from newline characters (\n) entered at the end of previous scanf() statements


In scanf() all the arguments you pass are pointers, and it is mandatory that you pass the exact format specifier that matches the type of the variable present at that address.

To know more about what happens when wrong format specifier is used in scanf() or printf() see this : click

Community
  • 1
  • 1
Cherubim
  • 5,287
  • 3
  • 20
  • 37
1

Use Switch() Statement its better in this case.

#include <stdio.h>
main()
{
float c,f,k;
int choice;
printf("Which Scale do you want to convert\n 1.Celsius\n 2.Fahrenheit\n 3.Kelvin");
printf("\n Enter Your Choice: ");
scanf("%d",&choice);
switch(choice)
{
    case 1:  printf("Enter Celsius Temperature: ");
             scanf("%f",&c);
             f=(9*c+32)/5;
             k=c+273;
             printf("\nThe Temperature is %.2f in Fahrenheit and %.2f in Kelvin\n",f,k);
             break;
    case 2:  printf("Enter Fahrenheit Temperature: ");
             scanf("%f",&f);
             c=((5*(f-32))/9);
             k=((5*(f-32))/9)+273;
             printf("\nThe Temperature is %.2f in Celsius and %.2f in Kelvin\n",c,k);
             break;
    case 3:  printf("Enter Kelvin Temperature: ");
             scanf("%f",&k);
             c=k-273;
             f=((9*(k-273))/5)+32;
             printf("\nThe Temperature is %.2f in Celsius and %.2f in Fahrenheit\n",c,f);
             break;
    default: printf("\nError. Please Restart The Program\n");
}

}
0

check the respective changes that what you need to change.

#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

printf("Would you like to convert into Celsius 'C' or Fahrenheit 'F'?\n");

//scanf("%d", &in);
scanf("%c", &in);

go = 1;
while (go == 1) {
    if ((in == 'F') || (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");
        printf("What temperature F would you like to convert to C?\n");

        //Get the input from the user
        scanf("%f", &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')|| (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("%f", &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(" %c", &in); // because of \n characters entered at the end of previous scanf() statement
    }
}

//End the program
return 0;

}
13krn
  • 541
  • 4
  • 9