# C Programming

posted by .

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?

• C Programming -

Logically, you do not need to choose the conversion, because you are not going to convert from C to C, nor from F to F.
If it is a requirement, you can modify accordingly.

...
do
{
printf("Enter 'C' for Celsius or 'F' for Fahrenheit:");
answer=getc(stdin); // get char. from keyboard
{ // C-> F conversion
...
}else
{ // F->C conversion
...
}

• C Programming -

I'm guessing the //get char. from keyboard
is a comment telling what the code you just wrote means?

• C Programming -

Oh yeah, and I need to use a switch statement in this program. The instructor's orders.

• C Programming -

Yes, this was meant to be the beginning of a comment (until the end of the line).
Most C-compilers accept this covention which came from C++.
Are you doing OK with your C-programming project?

• C Programming -

In general, an if statement is used for a two-branch condition, and a switch used for three branches or more. There is no penalty for one way or another.
A switch statement would be written as:

char U; // declare the variable for units
...
...... assign a value to U ('C' or 'F')
...
switch(U)
{
case 'C': // code for Celsius
case 'c':
...calculations for Celsius
break;
case 'F': // code for Fahrenheit
case 'f':
...calculations for Fahrenheith
break;
case default: // code for other cases
printf("error in temperature unit\n");
break;
}

The break statement is necessary otherwise at the end of execution of the case, it will 'run into' the next case.
This is also why it is possible write the code once for both upper and lower cases.

• C Programming -

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:
/* 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);

}

• C Programming -

The code logic looks reasonable. There are a few things to address, though.

1. In the original C-language, the declaration of variables cannot be placed anywhere in the program. In C++, this is an accepted practice to keep the namespace clean. So the declarations of choic and choice should follow the "float" line at the beginning. If your compiler allows you to do otherwise, check if your teacher allows it too.

2. It is superfluous to enter the temperature unit (C or F) twice. When C is entered, it cannot convert to anything else than Fahrenheit.
Now that you have to enter both as a requirement, you will have to verify that the two sets of answers are consistent, namely from C to F and from F to C, and nothing else. If the user enters C to C or F to F, it would be normal to give the user a second chance to enter it correctly. That means more code.

3. The first switch statement does not do anything, probably you have simply transcribed my suggested code and removed the lines where you are supposed to add code (...). This is why you got the unexpected response.

4. the getchar() will not work well with many compilers when preceded by a response with an "enter". It will take the last "enter" as the character and proceeds to do its own things and give surprising results: usually skipping input commands.
One way to avoid this problem is to use an array as the answer, and extract the first character as the response. For example,
...
...

Remember, ... means you have to supply appropriate code.

5. Finally, some compilers, including Dev-C++ will close the console window once the program terminates, with the result that you will not see the result displayed.
If you are running under Windows XP, you can add the statement
system("pause");
before the end of the program to cause the system to pause waiting for a keypress before the window disappears.

• C Programming -

Okay, so since my first switch statement is useless, how would I go about getting the program to only recognize F, f, C, and c only and if anything else than that is inputted for it to display "Invalid Syntax"?

• C Programming -

You have done the correct coding. The invalid syntax is because of the extraneous carriage-return carried from the previous data. You can avoid this by replacing
choic=getchar()
with
but you will need to declare answer as a character array at the beginning of the program using
The 10 is arbitrary, since you are expecting one character for input anyway.
Once you get this fixed, it will accept correctly f,F,c,C as planned.
Since your program requirements demand that the user enters two times, the from-unit and the to-unit, you will keep the first switch statement to store the from-unit. In the second switch statement you will check the to-unit to make sure it is the other one (i.e. from C to F, or from F to C). This is called data validation.
If you get some of the changes out of the way, you can then concentrate on the main part of the calculations, which I believe some math is amiss.

• C Programming -

Thanks! Now the first switch is working. But how do you end the program if the user inputs something else than C,c,F,f? It displays "Invalid Syntax" but it still moves onto the next line. I want it to just stop at "Invalid Syntax" if the user doesn't input C, c, F, f.

Also, now the second switch is giving me the automatic Invalid Syntax error without me having a chance to enter in something.

• C Programming -

Ok, I replaced the second switch with the code you provided earlier and now I got the second switch to operate correctly.

I'm just wondering how to end the program if the user does not enter C,c, F, f.

• C Programming -

In practice, I would make a for-loop that runs 3-5 times to invite the user to re-enter the correct data. After the specified number of attempts, the program will terminate with a message.
Have you done for-loops yet?

• C Programming -

Nevermind! I found out how to end the program if it's not C,c,f,F after experimenting. return(0);

=]

• C Programming -

We have just talked a little bit about loops structures. Haven't gotten into any coding with it though.

• C Programming -

Return 0; is OK.
Does it quit without warning, and the window disappears? If so, add system("pause"); before return 0.
Also, display an explanatory note before quitting to avoid frustrated users.

• C Programming -

It doesn't close out the window. It just goes back to the ~>

Now I just gotta figure out why my math is wrong since it's not spitting out the correct temperatures after the conversion.

• C Programming -

I think you got the right formulas, but probably put them in the wrong place.

• C Programming -

Oops. One of the formulas should have the multiplication factor 5/9 and the other one should be 9/5, right?

• C Programming -

Hmm, my F to C conversion works fine but my C to F is not giving me the right output.

My code for C to F is (temp * 5/9) + 32. I think it's the way you divide the 5/9. Starting out with 32.4, when I do it a certain way on my calculator, I get 50, which the program is spitting out but the correct answer is 90.3. When I do it like 32.4*9/5+32 on my calculator I get 90.3.

• C Programming -

Oh! There's my problem. It's 9/5, not 5/9.

• C Programming -

Good night! Will be back in the morning.

• C Programming -

The correct formulas are:
F = (C*9/5)+32
C = (F-32)*5/9
The graduations on the C scale are bigger, that is why from F to C it is 9/5.

• C Programming -

Yeah, at first I had the formulas in the wrong place, then I put 5/9 in one of the formulas instead of 9/5.

Thanks for all your help man. I don't know what I would do without you. THANK YOU!!!

## Similar Questions

1. ### Algebra

Celsius temperature readings can be converted to Fahrenheit readings using formula F=9/5c+32 What is the fahrenheit temp. that corresponds to each of the following Celsius temperatures -10, 0, 15, 1000?
2. ### Algebra

The folrmula F=9/5c+32 can be used to make temperature conversions with the Celsius ans Fahrenheit temperature scales. F-Fahrenheit temperature C=Celsius temperature Convert 100 degress Fahrenheit to the Celsius temperature scale. …
3. ### math

the formula C=5/9(F-32) is used to convert Fahrenheit temperature,F, to Celsius temperature,C. What temperature in Fahrenheit is equivalent to a temperature of 100 Celsius. I know that in Fahrenheit its 212 degrees, but i used it by …
4. ### C Programming

Write a program that prompts the user to enter a temperature, followed by a letter representing Celsius (C) or Fahrenheit (F). Then prompt the user for the conversion type and print the results to the screen with one decimal point. …
5. ### C Programming

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 …
6. ### math

Consider the following method of estimating Fahrenheit temperatures given Celsius temperatures (which was stated earlier in this section): To estimate the Fahrenheit temperature for a given Celsius temperature, double the Celsius temperature …
7. ### computer science

in one the chapter 3 programming challenges you were asked to write a program that converts a celsius temperature to fahrenheit. Modify that program so it uses a loop to display a table of celsius from 0 to 20 and their Fahrenheit …
8. ### Fund of programming 1

Complete the following C++ program. The program should compute and output the Celsius value corresponding to the given Fahrenheit value. //************************************************ // Celsius program // This program outputs …
9. ### Algebra

In order to covert temperature from Celsius to Fahrenheit,one can use the following equation f= 1.8c+32,where C is the temperature in Celsius and F is the temprature in Fahrenheit.what is the temperature in Fahrenheit when C is 15 …
10. ### INTRO PROGRAMMING

Celsius to Fahrenheit Table Design a program that displays a table of the Celsius temperatures 0 through 20 and their Fahrenheit equivalents. The formula for converting a temperature from Celsius to Fahrenheit is F = 95 C + 3 2 where …

More Similar Questions