Link to home
Start Free TrialLog in
Avatar of rebelcowgirl_73
rebelcowgirl_73

asked on

Help with a basic C program to convert temperatures

I am trying to do a conversion program for temperatures, and will attach all files I have below, however I am having some difficulty.

Whenever I try to compile the convert.c file in UNIX i get the following message:

convert.c:32:2: warning: no newline at end of file

This is the first time I have ever really worked with C.  Here is all my codes:
fahr_to_cent.h
 
#ifndef FAHR_TO_CENT_H
#define FAHR_TO_CENT_H
 
#define F_TO_C 0.55555556
 
double fahr_to_cent(double);
 
#endif
.......................................................................................................
fahr_to_cent.c
 
#include "fahr_to_cent.h"
 
double fahr_to_cent(double f)
{
     return F_TO_C * (f - 32);
}
....................................................................................................................
cent_to_fahr.h
 
#ifndef CENT_TO_FAHR_H
#define CENT_TO_FAHR_H
 
#define C_TO_F 1.8
 
double cent_to_fahr(double);
 
#endif
..................................................................................................................................
cent_to_fahr.c
 
#include "cent_to_fahr.h"
 
double cent_to_fahr(double c)
{
     return C_TO_F * (c + 32);
}
.....................................................................................................................................
convert.c
 
#include <stdio.h>
#include "fahr_to_cent.c"
#include "cent_to_fahr.c"
 
main()
{
     int choice;
 
     double fahr;
     double cent;
     
     printf("Welcome to the Temperature Conversion Program\n\n");
     printf("1 - Fahrenheit to Celsius\n");
     printf("2 - Celsius to Fahrenheit\n");
     printf("_________________________\n");
     printf("Your choice is? : ");
     scanf("%d", &choice);
     
     if(choice == 1)
     {
	printf("Please enter then temperature in Fahrenheit: ");
	scanf("%1f", &fahr);
	scanf("fahr_to_cent(fahr)");
     }
     else
     {
	printf("Please enter the temperature in Celsius: ");
	scanf("%1f", &cent);
	scanf("cent_to_fahr(cent)");
     }
     return 0;
}

Open in new window

SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rebelcowgirl_73
rebelcowgirl_73

ASKER

Ok now wen I run it, after the execution of the user inputting their temperature nothing happens.  The conversion is not being displayed!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i changed it to printf and now it prints out
cent_to_fahr(cent) and vice versa rather then the actual conversion!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
printf(cent_to_fahr(cent));
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is the changes I made to the convert.c file it compiles and runs however, it is giving to many spaces and I am unfamiliar wih formatting the scanf command.
#include <stdio.h>
#include "fahr_to_cent.c"
#include "cent_to_fahr.c"
 
main()
{
     int choice;
 
     double fahr;
     double cent;
     
     printf("Welcome to the Temperature Conversion Program\n\n");
     printf("1 - Fahrenheit to Celsius\n");
     printf("2 - Celsius to Fahrenheit\n");
     printf("_________________________\n");
     printf("Your choice is? : ");
     scanf("%d", &choice);
     
     if(choice == 1)
     {
	printf("Please enter then temperature in Fahrenheit: ");
	scanf("%lf", &fahr); 
        printf("%lf degrees Fahrenheit is %lf degrees Celcius. \n", fahr, fahr_to_cent(fahr));
     }
     else
     {
	printf("Please enter the temperature in Celsius: ");
	scanf("%lf", &cent);
	printf("%lf degrees Celcius is %lf degrees Fahrenheit. \n", cent , cent_to_fahr(cent));
     }
     return 0;
}

Open in new window

for example if I only wanted it to print until it reached a zero could I do that:

as in 32.45 printing as 32.45 instead of 32.450000 and on the same line116.010000 would come out 116.01
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
right but if I do .%.2lf then it prints two decimal places to the left but if there were none then I still get 0's and if there were more then it cuts of the end.

so for example
45 prints 45.00
45.1 prints 45.10
and 45.234 prints 45.23

I would like to print 45 as 45, 45,1 as 45,1 and 45.234 as 45.234
if possible
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

>>>>>
for example
45 prints 45.00
45.1 prints 45.10
and 45.234 prints 45.23

I would like to print 45 as 45, 45,1 as 45,1 and 45.234 as 45.234
if possible
>>>>>>

Then u need to take anothr double variable to store the value and need to convert it to a string.Then using pointer you need to verify what value to keep and then convert the resulting string to double...