Okay, question.
What if I inputted this:
Enter a temperature: 32
Enter 'C' for Celsius or 'F' for Fahrenheit: c
Choose a conversion ('C' for Celsius or 'F' for Fahrenheit): c
The temperature is: 0.0 C.
I want the program to read [The temperature is: 32 C.] when I go from C to C but my program is outputting 0.0 C. Same thing when I go from F to F.
CODE:
/* Directives */
#include <stdio.h>
int main(void)
{
float temp, CtoF, FtoC;
char choic;
char choice;
char answer[10];
char answers[10];
printf("Enter a temperature: ");
scanf("%f", &temp);
printf("Enter 'C' for Celsius or 'F' for Fahrenheit: ");
scanf("%s", answer);
choic=answer[0];
switch(choic)
{
case 'F':
case 'f':
{
break;
}
case 'C':
case 'c':
{
break;
}
default:
{printf("Unknown Syntax!\n");
return(0);
break;
}
}
printf("Choose a conversion ('C' for Celsius or 'F' for Fahrenheit): ");
scanf("%s", answers);
choice=answers[0];
switch(choice)
{
case 'F':
case 'f':
{CtoF = temp*9/5+32;
printf("The temperature is: %.1f F.\n", CtoF);
break;
}
case 'C':
case 'c':
{FtoC = (temp - 32) * (5.0/9.0);
printf("The temperature is: %.1f C.\n", FtoC);
break;
}
default:
{printf("Unknown Syntax!\n");
return(0);
break;
}
}
/* End Program */
return(0);
}
7 answers
I would help you with this, but Its too tiring. (coding)
float temp, CtoF, FtoC;
char choic;
char choice;
char answer[10];
char answers[10];
printf("Enter a temperature: ");
scanf("%f", &temp);
printf("Enter 'C' for Celsius or 'F' for Fahrenheit: ");
scanf("%s", answer);
choic=answer[0];
^ above, fix the coding, it isn't correct. "answer[0]" I believe.
Actually, your approach is a bit convoluted. It can be done more simply in other ways - like, simply assume that the user isn't converting F to F, and take the second choice for granted! Like, if he's converting from F, don't ask what he's converting to, just assume it's C.
However, following on with the approach you have taken, you need a new if/else (or switch statement) within your conversion logic, to decide whether to apply a conversion or not.
So your switches now are:
C to F
F to C
F to F
C to F
and you have four assignments depending on which was chosen.
The patch below follows from your work so far, but I say again that standing back from the problem and thinking about it differently will produce a shorter program, and one easier to understand.
/* Directives */
#include <stdio.h>
int main(void)
{
float temp, CtoF, FtoC;
char choic;
char choice;
char answer[10];
char answers[10];
printf("Enter a temperature: ");
scanf("%f", &temp);
printf("Enter 'C' for Celsius or 'F' for Fahrenheit: ");
scanf("%s", answer);
choic=answer[0];
switch(choic)
{
case 'F':
case 'f':
{
break;
}
case 'C':
case 'c':
{
break;
}
default:
{printf("Unknown Syntax!\n");
return(0);
break;
}
}
printf("Choose a conversion ('C' for Celsius or 'F' for Fahrenheit): ");
scanf("%s", answers);
choice=answers[0];
switch(choice)
{
case 'F':
case 'f':
{
switch(choic)
{
case 'F':
case 'f':
{
CtoF = temp; /* converting F to F */
break;
}
case 'C':
case 'c':
{
CtoF = temp*9/5+32; /* converting C to F */
break;
}
}
printf("The temperature is: %.1f F.\n", CtoF);
break;
}
case 'C':
case 'c':
{
switch(choic)
{
case 'F':
case 'f':
{
FtoC = (temp - 32) * (5.0/9.0); /* converting F to C */
break;
}
case 'C':
case 'c':
{
FtoC = temp; /* converting C to C */
break;
}
}
printf("The temperature is: %.1f C.\n", FtoC);
break;
}
default:
{printf("Unknown Syntax!\n");
return(0);
break;
}
}
}
Back to your question. As Jim commented, it is normal not to consider the trivial conversions in programming, but since it is a requirement, you will have to live with it.
The way to do this is to store the original unit (choic) and make a decision when you have obtained the destination unit (choice). The pseudocode is as follows:
input choic (C or F) (initial unit)
input choice (C or F) (destination unit)
switch(choice)
{
case 'C':
if(choic=='C')print input value (C)
else print converted value (F)
break;
case 'F':
if(choic=='F')print input value (F)
else print converted value (C)
break;
}
I assume you have learned the if...else construct.
Can you work with that?
Now is the time to write a good documentation for the program.
Programmers are often accused of not writing documentation, or writing it badly or inaccurtely. You can change this!