I am trying to combine Switch into Void Function to convert Celsius to Fahrenheit and vice versa. I am not sure what is correct way to combine Switch and Void Function together.
My code is at below. Any advice is valuable for me.
Thank you.
#include<stdio.h>
void fahtocel(void);
void celtofah(void);
int main(void)
{
int tem;
printf("Please select your choice\n");
printf("Enter 1 if you need to convert Fahrenheit to Celsius\n");
printf("Enter 2 if you need to convert Celsius to Fahrenheit\n");
scanf("%d", &tem);
switch(tem) {
case 1:
fahtocel();
break;
case 2:
celtofah();
break;
}
return 0;
}
void fahtocel()
{
float fahr;
printf("Please input Fahrenheit: ");
scanf("%2.f", &fahr);
printf("%3f Fahrenheit is equal %3.0f Celsius \n",fahr,(5.0/9.0)*(fahr - 32)*5/9;
}
void celtofah()
{
float celsius;
printf("Please input celsius: ");
scanf("%2.f", &celsius);
printf("%2.f Celsius is equal %2.f Fahrenheit\n",celsius, (9*celsius/5)+32);
}
You must be getting compiler issues. Put a parentheses at the end of your printf, and change the %2.f to just %f in your scanfs.
This code works...
#include<stdio.h>
void fahtocel(void);
void celtofah(void);
int main(void)
{
int tem;
printf("Please select your choice\n");
printf("Enter 1 if you need to convert Fahrenheit to Celsius\n");
printf("Enter 2 if you need to convert Celsius to Fahrenheit\n");
scanf("%d", &tem);
switch (tem) {
case 1:
fahtocel();
break;
case 2:
celtofah();
break;
}
return 0;
}
void fahtocel()
{
float fahr;
printf("Please input Fahrenheit: ");
scanf("%f", &fahr);
printf("%3f Fahrenheit is equal %3.0f Celsius \n", fahr, (5.0 / 9.0) * (fahr - 32) * 5 / 9);
}
void celtofah()
{
float celsius;
printf("Please input celsius: ");
scanf("%f", &celsius);
printf("%2.f Celsius is equal %2.f Fahrenheit\n", celsius, (9 * celsius / 5) + 32);
}
Related
I'm trying to write a program that converts Fahrenheit to Celsius and vice versa based on user menu selections and inputs, and I want the results to display a table based on the range and resolution inputted by user, but I'm stuck on writing the for loop and the while loop I was instructed to use for the two display functions. I'm new to programming with a language like c and I've been working on this for 3 days, so any tips/suggestions would be helpful.
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#pragma warning(disable:4996)
float calc_FtoC(float Fahrenheit);
void display_FtoC(int start, int stop, int step);
float calc_CtoF(float Celsius);
void display_CtoF(int start, int stop, int step);
void main(void)
{
char select;
int start, stop, step;
do
{
printf("Enter A to Convert Fahrenheit to Celsius.\n");
printf("Enter B to Convert Celsius to Fahrenheit.\n");
printf("Enter Q to Quit.\n");
printf("You entered -> ");
scanf("%c", &select);
while (getchar() != '\n');
select = toupper(select);
printf("Enter the highest temp to convert: \n");
scanf("%d", &start);
while (getchar() != '\n');
printf("Enter lowest temp to convert: \n");
scanf("%d", &stop);
while (getchar() != '\n');
printf("Enter increments: \n");
scanf("%d", &step);
while (getchar() != '\n');
switch (select)
{
case 'A':
display_FtoC(start, stop, step);
break;
case 'B':
display_CtoF(start, stop, step);
break;
case 'Q':
break;
default:
printf("You did not enter a correct selection!\n");
break;
}
} while (select != 'Q');
}
float calc_FtoC(float Fahrenheit)
{
float calc_f2c;
calc_f2c = 0.556 * Fahrenheit - 17.78;
return calc_f2c;
}
void display_FtoC(int start, int stop, int step)
{
// This is where the for loop for Fahrenheit to Celsius conversion is supposed to be
}
float calc_CtoF(float Celsius)
{
float calc_c2f;
calc_c2f = 1.8 * Celsius + 32;
return calc_c2f;
}
void display_CtoF(int start, int stop, int step)
{
// This is where the while loop is supposed to be for Celsius to Fahrenheit conversion
}
I'm a beginner to C language and I'm writing through a practice question where I use functions to convert between Fahrenheit and Celsius. I have written the program using 2 functions: one to convert from Fahrenheit to Celsius and another vice verca:
#include <stdio.h>
int celsius2fahrenheit(int celsius, int fahrenheit){
fahrenheit = (9/5) * celsius + 32;
printf("%d", fahrenheit);
}
int fahrenheit2celsius(int fahrenheit, int celsius){
celsius = (fahrenheit - 32) * (5/9);
printf("%d", celsius);
}
int main(void){
int celsius, fahrenheit;
celsius = fahrenheit = 0;
printf("-----------------Menu-----------------\n");
printf("Option 1. Convert Celsius to Fahrenheit\n");
printf("Option 2. Convert Fahrenheit to Celsius\n");
printf("\n");
printf("Option: ");
int Option;
scanf("%d", Option);
if (Option == 1){
printf("Enter the temperature (Celsius) : ");
scanf("%d", celsius);
printf("\n");
celsius2fahrenheit(celsius, fahrenheit);
}
else if (Option == 2){
printf("Enter the temperature (Fahrenheit) : ");
scanf("%d", fahrenheit);
printf("\n");
fahrenheit2celsius(fahrenheit, celsius);
}
else {
printf("Invalid Option");
}
return 0;
}
When the program was compiled, the error messages occur:
"Segmentation fault" after executing the option to select the operation. I assume there was something wrong with the variable declaration but I'm not too sure.
You are missing '&' in your scanf functions (line 23, 27 and 33)
Also you should take the values as floats to get more accurate results, because right now answers comes as integer..
#include <stdio.h>
int main()
{
float fh,cl;
int choice;
printf("\n1: Convert temperature from Fahrenheit to Celsius.");
printf("\n2: Convert temperature from Celsius to Fahrenheit.");
printf("\nEnter your choice (1, 2): ");
scanf("%d",&choice);
if(choice ==1){
printf("\nEnter temperature in Fahrenheit: ");
scanf("%f",&fh);
cl= (fh - 32) / 1.8;
printf("Temperature in Celsius: %.2f",cl);
}
else if(choice==2){
printf("\nEnter temperature in Celsius: ");
scanf("%f",&cl);
fh= (cl*1.8)+32;
printf("Temperature in Fahrenheit: %.2f",fh);
}
else{
printf("\nInvalid Choice !!!");
}
return 0;
}
scanf needs the specifier and the memory address of the variable.
scanf("%d", &Option);
Note that Option and &Option are two different things. & means the memory address where the variable is located.
Change each one in the code (line 27 and 33).
I wrote up this quickly and every time I execute the program I always end up with 32 for fahrenheit and 0.00 for celsius and am not sure what the issue is here.
#include <stdio.h>
int main(void)
{
double celsius=0, fahrenheit=0;
printf("Enter a temperature in degrees Celsius: ");
scanf("%f", &celsius);
fahrenheit =(5.0/9.0)*celsius + 32;
printf("That is %.2f Fahrenheit \n", fahrenheit);
printf("Enter a temperature in degrees fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * (5.0/9.0);
printf("That is %.2f Celsius \n", celsius);
return(0);
}
Your format specifier for double is wrong, it should be "%lf". But that's not enough, you should also check that scanf() successfuly read read the value, like this
#include <stdio.h>
int report_error(const char *const message)
{
// TODO: add message formatting capabilities to this function
fprintf(stderr, "error: %s\n", message);
return EXIT_FAILURE;
}
int main(void)
{
double celsius = 0;
double fahrenheit = 0;
printf("Enter a temperature in degrees Celsius: ");
if (scanf("%lf", &celsius) != 1)
return report_error("Invalid Input");
fahrenheit = (5.0 / 9.0) * celsius + 32;
// ^ Define this as a constant?
printf("That is %.2f Fahrenheit \n", fahrenheit);
printf("Enter a temperature in degrees fahrenheit: ");
if (scanf("%lf", &fahrenheit) != 1)
return report_error("Invalid Input");
celsius = (fahrenheit - 32) * (5.0 / 9.0);
// ^ see if it was a constant???
printf("That is %.2f Celsius \n", celsius);
return 0;
}
Also, it appears that your algebra went wrong. You should check the celsius expression apparently.
I am trying to make a program within code::blocks that will allow me to select multiple unit conversions to do. However, whenever I build and compile and reach the point in the program where it scans in my input for the variable "choice", code::blocks displays a window immediately afterwards that says that my .exe has stopped working and I can't figure out why this is the case. I am using the GNU GCC Compiler. Any help would be enormously appreciated.
#include <stdio.h>
int main()
{
float fahrenheit;
float celsius;
float pound;
float kilogram;
float inch;
float centimeter;
float mph;
float kph;
int foc;
int pok;
int ioc;
int mok;
int choice;
printf("\n1. Temperature Conversion\n");
printf("\n2. Weight Conversion\n");
printf("\n3. Length Conversion\n");
printf("\n4. Speed Conversion\n");
printf("\n5. Exit Program\n");
printf("\n");
printf("\nEnter the number of the program you would like to run :\n");
printf("\n");
scanf("%d", choice);
//Temperature Conversion
if(choice == 1) {
printf("\n1. Convert from Celsius to Fahrenheit\n");
printf("\n2. Convert from Fahrenheit to Celsius\n");
printf("\nEnter the number that corresponds with the conversion you would like to do:\n");
printf("\n");
scanf("%d", &foc);
if(foc == 1) {
//option 1
printf("\nEnter your temperature in Celsius : ");
scanf("%f", &celsius);
fahrenheit = 32 + (celsius * 1.8);
printf("\nYour temperature in Fahrenheit : %f ", fahrenheit);
}
else {
//option 2
printf("\nEnter your temperature in Fahrenheit : ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 0.55555555;
printf("\nYour temperature in Celsius : %f ", celsius);
}
}
//Weight Conversion
else if(choice == 2) {
printf("\n1. Convert from Pound to Kilogram ");
printf("\n2. Convert from Kilogram to Pound ");
printf("\nEnter the number that corresponds with the conversion you would like to do: ");
printf("\n ");
scanf("%d", &pok);
if(pok == 1) {
//option 1
printf("\nEnter your weight in pounds : ");
scanf("%f", £);
kilogram = (2.20462 * pound);
printf("\nYour weight in kilograms : %f ", kilogram);
}
else {
//option 2
printf("\nEnter your weight in kilograms : ");
scanf("%f", &kilogram);
pound = (kilogram/2.20462);
printf("\nYour weight in pounds : %f ", celsius);
}
}
//Length Conversion
else if(choice == 3) {
printf("\n1. Convert from inches to centimeters ");
printf("\n2. Convert from centimeters to inches ");
printf("\nEnter the number that corresponds with the conversion you would like to do: ");
printf("\n ");
scanf("%d", &ioc);
if(ioc == 1) {
//option 1
printf("\nEnter your length in inches : ");
scanf("%f", &inch);
centimeter = (inch/2.54);
printf("\nYour length in centimeters : %f ", centimeter);
}
else {
//option 2
printf("\nEnter your length in centimeters : ");
scanf("%f", ¢imeter);
inch = (centimeter*2.54);
printf("\nYour length in inches : %f ", inch);
}
}
//Speed Conversion
else if(choice == 4) {
printf("\n1. Convert from mph to kph ");
printf("\n2. Convert from kph to mph ");
printf("\nEnter the number that corresponds with the conversion you would like to do: ");
printf("\n ");
scanf("%d", &mok);
if(mok == 1) {
//option 1
printf("\nEnter your speed in mph : ");
scanf("%f", &mph);
kph = (mph/1.60934);
printf("\nYour speed in kilometers: %f ", kph);
}
else {
//option 2
printf("\nEnter your speed in kph : ");
scanf("%f", &kph);
mph = (1.60934*kph);
printf("\nYour length in inches : %f ", mph);
}
}
else if(choice == 5) {
printf("\nProgram has ended. Thanks for your time!");
}
else {
printf("\nThat is not a valid program number. ");
}
}
You have to pass the pointer to the choice variable (i.e. &choice) to your scanf() call, just as you've done for your other scanf() uses.
I.e. instead of
scanf("%d", choice);
use
scanf("%d", &choice);
scanf() reads a value from the user and the address of the variable must be represented in the syntax
if not represented the program takes the garbage value so
I was wondering how I could prompt the end-user of my program to type in a value they want to be converted from Fahrenheit into Celsius in C.
Basically, since I'm a total n00b and I'm writing amazing "programs" such as this one:
//Simple program to convert Fahrenheit to Celsius
int main (int argc, char *argv[])
{
double celsius, fahrenheit, result;
celsius = result;
fahrenheit = 27;
result = (fahrenheit - 32) / 1.8;
printf("27 degress Fahrenheit is %g degrees Celsius!", result);
return 0;
}
I would like to add some actual "functionality" to it if you know what I mean. Instead of just making this a test program where really it just shows off some simple arithmetic expression evaluating, I would like to actually make it somewhat mildly useful.
Anyway, I was wondering if I could use the function listed in the scanf(3) Man page to aid me in the recognition of user-inputted data, and then somehow store it into the Fahrenheit variable.
Now, it would really be cool if the program, upon running, could prompt the end-user with a question asking whether he or she would like to convert from Celsius to Fahrenheit or from Fahrenheit to Celsius, but let's just take it one step at a time, and I'll wait until I read the chapter in my book about "Making Decisions"! :)
UPDATE:
Removes useless variable result as pointed out by kiamlaluno:
//Simple program to convert Fahrenheit to Celsius
int main (int argc, char *argv[])
{
double fahrenheit, celsius;
fahrenheit = 27;
celsius = (fahrenheit - 32) / 1.8;
printf("27 degress Fahrenheit is %g degrees Celsius!", celsius);
return 0;
}
UPDATE UPDATE:
I've been trying to incorporate everyone's helpful suggestions posted here, but I'm running into more problems with my code:
//Simple program to convert Fahrenheit to Celsius and Celsius to Fahrenheit
int main (int argc, char *argv[])
{
int celsius, fahrenheit, celsiusResult, fahrenheitResult;
celsiusResult = (fahrenheit - 32)*(5/9);
fahrenheitResult = (celsius*(9/5)) + 32;
int prompt;
printf("Please press 1 to convert Fahrenheit to Celsius, or 0 to convert Celsius to Fahrenheit please:\n ");
scanf("%i", &prompt);
if(prompt == 1) {
printf("Please enter a temperature in Fahrenheit to be converted into Celsius!:\n");
scanf("%i", &fahrenheit);
printf("%i degress Fahrenheit is %i degrees Celsius!", fahrenheit, celsiusResult);
}
else {
printf("Please enter a temperature in Celsius to be converted into Fahrenheit:\n");
scanf("%i", &celsius);
printf("%i degreses Celsius is %i degrees Fahrenheit", celsius, fahrenheitResult);
}
return 0;
}
Everything's working great, except for the calculations themselves, which come out completely wrong..
For a second I thought this may have been because I changed the numbers themselves to integers types, but I made them doubles again and it was still kind of screwy.
Any thoughts?
To use scanf() to read a double, you'd need to use the correct format string. It would be %lf for "long float" (where %f alone would read into a float). You could then read directly to a double. Same goes for printing it out.
int main (int argc, char *argv[])
{
double fahrenheit, celsius;
printf("farenheit? "); /* write a prompt */
scanf("%lf", &fahrenheit); /* read a double into the fahrenheit variable */
celsius = (fahrenheit - 32) / 1.8;
printf("%lf degress Fahrenheit is %lf degrees Celsius!\n", fahrenheit, celsius);
return 0;
}
Note that this doesn't handle non-numeric inputs at all. You'd need to use other input techniques.
[edit]
You are certainly taking jumps in your code which seems like you're making a lot of progress. :)
To address your most current update, there are a couple of issues. Your code doesn't actually calculate anything, you applied the formula too soon. You should wait until fahrenheit or celsius have meaningful values (i.e., after the user has input the value to be converted). It would be a good idea to move these formulas into functions to perform the conversion. You should also stick with using double and not integers. You will not get the precision you want using integers.
double convert_fahrenheit_to_celsius(double fahrenheit)
{
return (fahrenheit - 32)*(5/9);
}
double convert_celsius_to_fahrenheit(double celsius)
{
return (celsius*(9/5)) + 32;
}
int main (int argc, char *argv[])
{
double celsius, fahrenheit, celsiusResult, fahrenheitResult;
int prompt;
printf("Please press 1 to convert Fahrenheit to Celsius, or 0 to convert Celsius to Fahrenheit please:\n ");
scanf("%i", &prompt);
if(prompt == 1) {
printf("Please enter a temperature in Fahrenheit to be converted into Celsius!:\n");
scanf("%lf", &fahrenheit);
/* now convert user-input fahrenheit to celsius */
celsiusResult = convert_fahrenheit_to_celsius(fahrenheit);
printf("%lf degress Fahrenheit is %lf degrees Celsius!", fahrenheit, celsiusResult);
}
else {
printf("Please enter a temperature in Celsius to be converted into Fahrenheit:\n");
scanf("%lf", &celsius);
/* now convert user-input celsius to fahrenheit */
fahrenheitResult = convert_celsius_to_fahrenheit(celsius);
printf("%lf degreses Celsius is %lf degrees Fahrenheit", celsius, fahrenheitResult);
}
return 0;
}
Ok if you want to prompt the user for input just use scanf with the tag %lf:
//Simple program to convert Fahrenheit to Celsius
int main (int argc, char *argv[])
{
double fahrenheit, result;
printf("Please enter a number for farenheit: ");
scanf("%lf", &fahrenheit);
result = (fahrenheit - 32) / 1.8;
printf("27 degress Fahrenheit is %g degrees Celsius!", result);
return 0;
}
As for prompting the user if he/she wants celcius or farenheit you need something called and if statement and else statement.
int main (int argc, char *argv[])
{
double celsius, fahrenheit, result;
int ForC;
printf("1 for Celsius or 0 for Fahrenheit plz: ");
scanf("%d", &ForC);
if(ForC == 1) {
scanf("%lf", &fahrenheit);
result = (fahrenheit - 32) / 1.8;
printf("fahrenheit to celsius:%lf", result);
}
else {
scanf("%lf", &celsius);
result = (celsius*9.0)/5.0 + 32;
printf("celsius to farenheit:%lf", result);
}
return 0;
}
Handling the possibly erroneous user input could be done by checking the return value of scanf(). It must be equal to the number of values expected (1 in this case). In other case, the input should be repeated.
For your UPDATE UPDATE : Try doing the calculation after you get the value from the user. If not, the resulting value of celsius, fahrenheit, celsiusResult, fahrenheitResult is unknown.