# Thread: Using switch statements and some other questions

1. ## Using switch statements and some other questions

This is what the program needs to do:

Enter a temperature: 32.4
Enter 'C' for Celsius or 'F' for Fahrenheit: C
Choose a conversion ('C' for Celsius or 'F' for Fahrenheit): F
The temperature is: 90.3 F.

How would I store the input when it asks for C or F, then use it to activate the correct equation to convert the inputted temp to C or F?

2. Code:
```char choice;
if (choice == 'C'){
}```

3. What if I wanted to use switch statements for this program?

& where would char choice; go?

4. Code:
```char choice;

choice=getchar();

switch(choice)
{
case 'C':
{//equation for Celcius
break;
}

case 'F':
{
//equation for Farenhite
break;
}

default:
{
//code for default
break;
}

}```

A switch statement took a variable or a constant and compare the value with the case,the value then invoke the corresponding case.If no cases matches the default will invoke.

5. Thanks aaa111. That's what I need. Something to get me going.

What if I wanted to make the program recognize lowercase 'c' and 'f' as uppercase 'C' and 'F'? Would I just add another case 'f' and another case 'c'?

6. Originally Posted by gnozahs
Thanks aaa111. That's what I need. Something to get me going.

What if I wanted to make the program recognize lowercase 'c' and 'f' as uppercase 'C' and 'F'? What kind of statement or function would I need to use?
well you can use two case in a row for same thing,like this:

Code:
```char choice;

choice=getchar();

switch(choice)
{
case 'C':
case 'c':
{//equation for Celcius
break;
}

case 'F':
case 'f':
{
//equation for Farenhite
break;
}

default:
{
//code for default
break;
}

}```

7. Wait, is the code you wrote above for storing the inputted number as C or F or actually converting it from C to F or F to C? I'm confused.

This is what the program should look like:
Enter a temperature: 32.4
Enter 'C' for Celsius or 'F' for Fahrenheit: C
Choose a conversion ('C' for Celsius or 'F' for Fahrenheit): F
The temperature is: 90.3 F.

8. you can also use strcmp

9. I haven't learned strcmp yet so I have no idea how to use it.

How would I store the inputted number as a C or F temperature?
Use scanf under the Enter 'C' for Celsius or 'F' for Fahrenheit: C?

10. Using any of the input functions, like scanf (for instance).

11. Okay, so here is my code so far.

I'm not sure how to connect the inputted C or F Enter 'C' for Celsius or 'F' for Fahrenheit: C to the inputted temperature Enter a temperature: 32.4 and then take all that and make it use the correct temperature conversion.
Code:
```/* This program inputs a temperature with a letter representing Celsius */
/* or Fahrenheit then the program converts it to either Celsius or      */
/* Fahrenheit.                                                          */

/* Directives */

#include <stdio.h>

int main(void)
{

float temp, fahrenheit;

printf("Enter a temperature: ");
scanf("%f", &temp);
printf("Enter 'C' for Celsius or 'F' for Fahrenheit: ");

switch (code)
{
case F:
case f:
scanf("%f", &fahrenheit);

/* End Program */
return(0);

}```

12. Originally Posted by gnozahs
Okay, so here is my code so far.

I'm not sure how to connect the inputted C or F Enter 'C' for Celsius or 'F' for Fahrenheit: C to the inputted temperature Enter a temperature: 32.4
You shouldn't. They are not connected, so there's no point in trying to connect them. Whether they say C or F, you still need to input a temperature. The only thing that will change is what to do with it.

13. Originally Posted by tabstop
You shouldn't. They are not connected, so there's no point in trying to connect them. Whether they say C or F, you still need to input a temperature. The only thing that will change is what to do with it.
Oh! I just noticed that right now.

Alright, so how would I say Unknown Syntax! if the user inputted something other than a C or F in the part where the program asks Enter 'C' for Celsius or 'F' for Fahrenheit? Would I have to do a switch statement here too so that I can let the program know that a lowercase c is the same thing as an uppercase C?

14. Originally Posted by gnozahs
Oh! I just noticed that right now.

Alright, so how would I say Unknown Syntax! if the user inputted something other than a C or F in the part where the program asks Enter 'C' for Celsius or 'F' for Fahrenheit? Would I have to do a switch statement here too so that I can let the program know that a lowercase c is the same thing as an uppercase C?
That's part of your switch statement.

15. Alright. I think I'm almost there. I'm just having a slight problem when I'm running the program.

This is how it should look like (for reference).

Enter a temperature: 32.4
Enter 'C' for Celsius or 'F' for Fahrenheit: C
Choose a conversion ('C' for Celsius or 'F' for Fahrenheit): F
The temperature is: 90.3 F.

When I run it, after I enter 32.4, the next line automatically reads Enter 'C' for Celsius or 'F' for Fahrenheit: Unknown Syntax! without me having a chance to enter C or F in. And then it just takes me to the next line. I do something wrong in my switch statement?

Here's my code:
Code:
```/* Directives */

#include <stdio.h>

int main(void)
{

float temp, CtoF, FtoC;

printf("Enter a temperature: ");
scanf("%f", &temp);
printf("Enter 'C' for Celsius or 'F' for Fahrenheit: ");

char choic;
choic=getchar();

switch(choic)
{
case 'F':
case 'f':
{break;
}

case 'C':
case 'c':
{break;
}

default:
{printf("Unknown Syntax!\n");
break;
}
}

printf("Choose a conversion ('C' for Celsius or 'F' for Fahrenheit): ");

char choice;
choice=getchar();

switch(choice)
{
case 'F':
case 'f':
{CtoF = (temp - 32) * (5.0/9.0);
printf("The temperature is: %.1f F.\n", CtoF);
break;
}
case 'C':
case 'c':
{FtoC = temp * (5.0/9.0) + 32;
printf("The temperature is: %.1f C.\n", FtoC);
break;
}

default:
{printf("Uknown Syntax!\n");
break;
}
}

/* End Program */
return(0);

}```