• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 628
  • Last Modified:

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

0
rebelcowgirl_73
Asked:
rebelcowgirl_73
  • 7
  • 5
  • 5
  • +1
15 Solutions
 
ozoCommented:
try adding a newline at the end of the file
(how did you create the file?)
But since it seems to be a warning and not an error, did otherwise function properly?
0
 
isaackhaziCommented:
The Error is caused by not hitting enter after the last line of code.
0
 
isaackhaziCommented:
Go to the end of the file and hit enter and recompile. Its a warning so you can continue anyways....
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
rebelcowgirl_73Author Commented:
Ok now wen I run it, after the execution of the user inputting their temperature nothing happens.  The conversion is not being displayed!
0
 
Subrat (C++ windows/Linux)Software EngineerCommented:
I don't know about unix, but same type of message can be get if not including stdafx.h in Visual Studio.
So u might have to include another precompiled header.

Other wise start a project without choosing include precompile header.
0
 
ozoCommented:
In order to display a conversion, there needs to be code to do that.
I see none in the above code snippet
0
 
isaackhaziCommented:
Thats because you dont have a printf statement
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

0
 
isaackhaziCommented:
This is it. just add a printf
ahr_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);
        printf("fahr_to_cent(fahr)");
     }
     else
     {
        printf("Please enter the temperature in Celsius: ");
        scanf("%1f", &cent);
        printf("cent_to_fahr(cent)");
     }
     return 0;

Open in new window

0
 
isaackhaziCommented:
Could do this also
Its not comeplete but i hop u get the idea
ahr_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)
{
     printf(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);
        fahr_to_cent(fahr);
     }
     else
     {
        printf("Please enter the temperature in Celsius: ");
        scanf("%1f", &cent);
        cent_to_fahr(cent);
     }

Open in new window

0
 
rebelcowgirl_73Author Commented:
i changed it to printf and now it prints out
cent_to_fahr(cent) and vice versa rather then the actual conversion!
0
 
Subrat (C++ windows/Linux)Software EngineerCommented:
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);

not 1f , it should be lf

//        scanf("fahr_to_cent(fahr)");

here you need printf(%lf", fahr_to_cent(fahr));
     }
     else
     {
        printf("Please enter the temperature in Celsius: ");
        scanf("%1f", &cent);// use lf insted of 1f
        scanf("cent_to_fahr(cent)");//use printf() as above
     }
     return 0;
}
0
 
isaackhaziCommented:
Yes please remove the " " around the function call.
0
 
isaackhaziCommented:
printf(cent_to_fahr(cent));
0
 
ozoCommented:
if you want to print the value of a double with printf,
you can use the %f or %g format, and pass it the number you want to display
0
 
Subrat (C++ windows/Linux)Software EngineerCommented:
Complete code but not compiled...

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"// use .h instead of .c
#include "cent_to_fahr.c" // Use .h instead of .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", fahr_to_cent(fahr));
     }
     else
     {
      printf("Please enter the temperature in Celsius: ");
      scanf("%lf", &cent);// this is small 'L' not if ------ lf
      printf("%lf", cent_to_fahr(cent));
     }
     return 0;
}
0
 
ozoCommented:
7.19.6.1 The fprintf function
Synopsis
#include <stdio.h>
int fprintf(FILE * restrict stream,
const char * restrict format, ...);
Description
The fprintf function writes output to the stream pointed to by stream, under control
of the string pointed to by format that specifies how subsequent arguments are
converted for output. If there are insufficient arguments for the format, the behavior is
undefined. If the format is exhausted while arguments remain, the excess arguments are
evaluated (as always) but are otherwise ignored. The fprintf function returns when
the end of the format string is encountered.
...
The conversion specifiers and their meanings are:
d,i The int argument is converted to signed decimal in the style []dddd. The
precision specifies the minimum number of digits to appear; if the value
being converted can be represented in fewer digits, it is expanded with
leading zeros. The default precision is 1. The result of converting a zero
value with a precision of zero is no characters.
o,u,x,X The unsigned int argument is converted to unsigned octal (o), unsigned
decimal (u), or unsigned hexadecimal notation (x or X) in the style dddd; the
letters abcdef are used for x conversion and the letters ABCDEF for X
conversion. The precision specifies the minimum number of digits to appear;
if the value being converted can be represented in fewer digits, it is expanded
with leading zeros. The default precision is 1. The result of converting a
zero value with a precision of zero is no characters.

f,F A double argument representing a floating-point number is converted to
decimal notation in the style []ddd.ddd, where the number of digits after
the decimal-point character is equal to the precision specification. If the
precision is missing, it is taken as 6; if the precision is zero and the # flag is
not specified, no decimal-point character appears. If a decimal-point
character appears, at least one digit appears before it. The value is rounded to
the appropriate number of digits.
A double argument representing an infinity is converted in one of the styles
[-]inf or [-]infinity  which style is implementation-defined. A
double argument representing a NaN is converted in one of the styles
[-]nan or [-]nan(n-char-sequence)  which style, and the meaning of
any n-char-sequence, is implementation-defined. The F conversion specifier
produces INF, INFINITY, or NAN instead of inf, infinity, or nan,
respectively.237)
e,E A double argument representing a floating-point number is converted in the
style []d.ddd e±dd, where there is one digit (which is nonzero if the
argument is nonzero) before the decimal-point character and the number of
digits after it is equal to the precision; if the precision is missing, it is taken as
6; if the precision is zero and the # flag is not specified, no decimal-point
character appears. The value is rounded to the appropriate number of digits.
The E conversion specifier produces a number with E instead of e
introducing the exponent. The exponent always contains at least two digits,
and only as many more digits as necessary to represent the exponent. If the
value is zero, the exponent is zero.
A double argument representing an infinity or NaN is converted in the style
of an f or F conversion specifier.
g,G A double argument representing a floating-point number is converted in
style f or e (or in style F or E in the case of a G conversion specifier),
depending on the value converted and the precision. Let P equal the
precision if nonzero, 6 if the precision is omitted, or 1 if the precision is zero.
Then, if a conversion with style E would have an exponent of X:
 if P > X e 4, the conversion is with style f (or F) and precision
P  (X + 1).
 otherwise, the conversion is with style e (or E) and precision P  1.
Finally, unless the # flag is used, any trailing zeros are removed from the
fractional portion of the result and the decimal-point character is removed if
there is no fractional portion remaining.
A double argument representing an infinity or NaN is converted in the style
of an f or F conversion specifier.
a,A A double argument representing a floating-point number is converted in the
style []0xh.hhhh p±d, where there is one hexadecimal digit (which is
nonzero if the argument is a normalized floating-point number and is
otherwise unspecified) before the decimal-point character238) and the number
of hexadecimal digits after it is equal to the precision; if the precision is
missing and FLT_RADIX is a power of 2, then the precision is sufficient for
an exact representation of the value; if the precision is missing and
FLT_RADIX is not a power of 2, then the precision is sufficient to
distinguish239) values of type double, except that trailing zeros may be
omitted; if the precision is zero and the # flag is not specified, no decimalpoint
character appears. The letters abcdef are used for a conversion and
the letters ABCDEF for A conversion. The A conversion specifier produces a
number with X and P instead of x and p. The exponent always contains at
least one digit, and only as many more digits as necessary to represent the
decimal exponent of 2. If the value is zero, the exponent is zero.
A double argument representing an infinity or NaN is converted in the style
of an f or F conversion specifier.
c If no l length modifier is present, the int argument is converted to an
unsigned char, and the resulting character is written.
If an l length modifier is present, the wint_t argument is converted as if by
an ls conversion specification with no precision and an argument that points
to the initial element of a two-element array of wchar_t, the first element
containing the wint_t argument to the lc conversion specification and the
second a null wide character.
s If no l length modifier is present, the argument shall be a pointer to the initial
element of an array of character type.240) Characters from the array are
written up to (but not including) the terminating null character. If the
precision is specified, no more than that many bytes are written. If the
precision is not specified or is greater than the size of the array, the array shall
contain a null character.
If an l length modifier is present, the argument shall be a pointer to the initial
element of an array of wchar_t type. Wide characters from the array are
converted to multibyte characters (each as if by a call to the wcrtomb
function, with the conversion state described by an mbstate_t object
initialized to zero before the first wide character is converted) up to and
including a terminating null wide character. The resulting multibyte
characters are written up to (but not including) the terminating null character
(byte). If no precision is specified, the array shall contain a null wide
character. If a precision is specified, no more than that many bytes are
written (including shift sequences, if any), and the array shall contain a null
wide character if, to equal the multibyte character sequence length given by
the precision, the function would need to access a wide character one past the
end of the array. In no case is a partial multibyte character written.241)
p The argument shall be a pointer to void. The value of the pointer is
converted to a sequence of printing characters, in an implementation-defined
manner.
n The argument shall be a pointer to signed integer into which is written the
number of characters written to the output stream so far by this call to
fprintf. No argument is converted, but one is consumed. If the conversion
specification includes any flags, a field width, or a precision, the behavior is
undefined.
% A % character is written. No argument is converted. The complete
conversion specification shall be %%.
0
 
rebelcowgirl_73Author Commented:
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

0
 
rebelcowgirl_73Author Commented:
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
0
 
Subrat (C++ windows/Linux)Software EngineerCommented:
can use printf("%.2lf",32.4500001);
0
 
rebelcowgirl_73Author Commented:
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
0
 
ozoCommented:
    gG      The double argument is converted in style f or e (or F or E for G
             conversions).  The precision specifies the number of significant
             digits.  If the precision is missing, 6 digits are given; if the
             precision is zero, it is treated as 1.  Style e is used if the
             exponent from its conversion is less than -4 or greater than or
             equal to the precision.  Trailing zeros are removed from the
             fractional part of the result; a decimal point appears only if it
             is followed by at least one digit.
0
 
Subrat (C++ windows/Linux)Software EngineerCommented:

>>>>>
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...


0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 7
  • 5
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now