### Thread: C Code Problem

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
5
Rep Power
0

#### C Code Problem

Code:
```#include<stdio.h>
#include<ctype.h>
void work(void);
double fah_2_cels(double fahr );
double cels_2_fahr(double  cels);
int main(void){
char more;
puts("\n enter the temperature followed by f for fahr and c for cels");
do{work();
puts("\n do you want to continue(Y/N)?");
scanf("%c" ,&more);
more =toupper(more);
}while(more!='N');
return 0;
}
void work(void)
{
double temp;
//char ch[6];
double num,fahr,cels;
printf("enter the temperature");
scanf("%lg %c",&num,&num);
if (num='70')
{temp=fahr_2_cels(num);
printf("temperature in cel is %g",temp);}
else
temp=cels_2_fahr(num);
printf("temperature in fahr is %g",temp);
}
double fah_2_cels(double fahr){return ((5.0/9.0)*(fahr-32.0));}
double cels_2_fahr(double cels){return ((9.0/5.0)*cels +32.0);}```
2. Please edit your post to have [code][/code] tags around your code.
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
30
Rep Power
6
Originally Posted by takymounir
Code:
```#include<stdio.h>
#include<ctype.h>
void work(void);
double fah_2_cels(double fahr );
double cels_2_fahr(double  cels);
int main(void){
char more;
puts("\n enter the temperature followed by f for fahr and c for cels");
do{work();
puts("\n do you want to continue(Y/N)?");
scanf("%c" ,&more);
more =toupper(more);
}while(more!='N');
return 0;
}
void work(void)
{
double temp;
//char ch[6];
double num,fahr,cels;
printf("enter the temperature");
scanf("%lg %c",&num,&num);
if (num='70')
{temp=fahr_2_cels(num);
printf("temperature in cel is %g",temp);}
else
temp=cels_2_fahr(num);
printf("temperature in fahr is %g",temp);
}
double fah_2_cels(double fahr){return ((5.0/9.0)*(fahr-32.0));}
double cels_2_fahr(double cels){return ((9.0/5.0)*cels +32.0);}```
In function `work':
undefined reference to `fahr_2_cels'

Spelling mistake in calling function fahr_2_cels. It should be fah_2_cels.
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
13
Rep Power
0
So I'm going to just space your code so I looks okay. What exactly is the problem?
Code:
```#include<stdio.h>
#include<ctype.h>

void work(void);
double fah_2_cels(double fahr );
double cels_2_fahr(double cels);
int main(void)
{
char more;
puts("\n enter the temperature followed by f for fahr and c for cels");
do
{
work();
puts("\n do you want to continue(Y/N)?");
scanf("%c" ,&more);
more =toupper(more);
}
while(more!='N');
return 0;
}//closes int main not void main
void work(void)
{
double temp;
//char ch[6];
double num,fahr,cels;
printf("enter the temperature");
scanf("%lg %c",&num,&num);
if (num='70')
{
temp=fahr_2_cels(num);
printf("temperature in cel is %g",temp);
}
else
temp=cels_2_fahr(num);
printf("temperature in fahr is %g",temp);
}
double fah_2_cels(double fahr)
{
return ((5.0/9.0)*(fahr-32.0));
}
double cels_2_fahr(double cels)
{
return ((9.0/5.0)*cels +32.0);
}```
If you don't space out your code it makes the other coders angry. It's much harder to debug. And if you space it you may just see the issue, like the fact that you closed the else but never opened it and that you never closed the void main function. The appropriate code should be:

Code:
```#include<stdio.h>
#include<ctype.h>

void work(void);
double fah_2_cels(double fahr );
double cels_2_fahr(double cels);
int main(void)
{
char more;
puts("\n enter the temperature followed by f for fahr and c for cels");
do{
work();
puts("\n do you want to continue(Y/N)?");
scanf("%c" ,&more);
more =toupper(more);
}while(more!='N');
return 0;
}
void work(void)
{
double temp;
//char ch[6];
double num,fahr,cels;
printf("enter the temperature");
scanf("%lg %c",&num,&num);
if (num='70')
{
temp=fahr_2_cels(num);
printf("temperature in cel is %g",temp);
}
else
temp=cels_2_fahr(num);
printf("temperature in fahr is %g",temp);
}//closes void work()
double fah_2_cels(double fahr)
{
return ((5.0/9.0)*(fahr-32.0));
}
double cels_2_fahr(double cels)
{
return ((9.0/5.0)*cels +32.0); //If you find that the results are off look at these functions
}
} //Added this so void main closes.
/*extra idea, use more descriptive function names than work and two separate mains, because if you have main twice it screws with everything.*/```
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
5
Rep Power
0
hi thanks for help the code is still not working it has the problem with the loop can you please run it,and also it give me wrong value for fahrenh. and celisus can you please run it to see the problem
6. This part is wrong:
Code:
```  scanf("%lg %c",&num,&num);
if (num='70') {```
You've converted both a number and a character into the same memory location. &num,&num

What did you intend
if (num='70')
to mean?
The equal sign in num = '70' is an assignment.
'70' is (I believe) an illegal character notation although a non-strict compiler might accept it with a possibly useful interpretation as an integer constant.

(ASCII character 70 is 'F')

You might have meant

char unit;
...
scanf("%lg %c",&temp,&unit);

if ('F' == unit) /* == is a comparison operator */
7. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
5
Rep Power
0
Originally Posted by b49P23TIvg
This part is wrong:
Code:
```  scanf("%lg %c",&num,&num);
if (num='70') {```
You've converted both a number and a character into the same memory location. &num,&num

What did you intend
if (num='70')
to mean?
The equal sign in num = '70' is an assignment.
'70' is (I believe) an illegal character notation although a non-strict compiler might accept it with a possibly useful interpretation as an integer constant.

(ASCII character 70 is 'F')

You might have meant

char unit;
...
scanf("%lg %c",&temp,&unit);

if ('F' == unit) /* == is a comparison operator */
thanks for helping me
i did fix this issue but still not working wright i should enter for example 125 f and the code should figure that it is fehr. and convert it to cels. so can you please help me to do this
8. 'f' != 'F'

Perhaps you could change the case like you did with that all important question in your main function.

toupper
9. No Profile Picture
I haz teh codez!
Devshed Frequenter (2500 - 2999 posts)

Join Date
Dec 2003
Posts
2,574
Rep Power
2341
Code:
`double fah_2_cels(double fahr );`
Code:
`temp=fahr_2_cels(num);`
10. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2012
Posts
5
Rep Power
0
Originally Posted by b49P23TIvg
'f' != 'F'

Perhaps you could change the case like you did with that all important question in your main function.

toupper
Code:
```#include<stdio.h>
#include<ctype.h>
void work(void);
double fah_2_cels(double fahr );
double cels_2_fahr(double cels);
int main(void)
{
char more;
puts("\n This program for converting temperature from c to f and vice versa");
printf("\n--------------------------------------------------------------------\n");
do{
work();
puts("\n do you want to continue(Y/N)?");
scanf(" %c" ,&more);
}while(toupper(more)!='N');
return 0;
}
void work(void)
{
double temp,temp1,con;
char unit;

printf("enter the temperature value follwed by f for fehrenhuit and c for celisus\n ");
scanf("%lg",&temp1);
scanf("%c",&unit);

/* if(((temp1<=-459.67&&unit= 'f')||(temp1<=-459.67 &&unit='F')) || ((temp1<=-273.15 &&unit='c' )||(temp1<=-273.15 &&unit='C')))
{
con=-500;
printf("error -500 your temp is lower than absolute zero");
}*/
switch(toupper(unit))
{
case 'F':
temp=fah_2_cels(temp1);
printf("temperature in cel is %g C and in fehren is %g F" ,temp,temp1);
break;
case 'C':
temp=cels_2_fahr(temp1);
printf("temperature in fahr is %g F and in celisus is %g C",temp,temp1);
break;
default:
printf("You did enter wrong letter or you did not enter letter at all");
}
}//closes void work()
double fah_2_cels(double fahr)
{
return ((5.0/9.0)*(fahr-32.0));
}
double cels_2_fahr(double cels)
{
return ((9.0/5.0)*cels +32.0); //If you find that the results are off look at these functions
}```
THE PROGRAM IS WORKING BUT I NEED TO ADD CONDITION IF THE TEMP, IN FEHRENHUIT<=-459.67 OR TEMP. IN CELISUS <=-273.15 THE TWO FUNCTION IN THIS CASE WILL PRINTF("ERROR") SO HOW CAN I DO THIS
11. #### 3 untested workable ideas

puts(x<0?"negative":"non-negative");

char*classifications[] = {"negative","zero","positive};
/* ... */
puts(classifications[(0<=x)+(0<x)]);

#define SIGNUM(A) ((A) < 0 ? -1 : 0 < (A))
/* ... */
switch(SIGNUM(x)) {
-1: puts("negative");break;
0: puts("zero");break;
1: puts("positive");break;
default: fputs("\nDanger Will Robinson. SIGNUM failure!\n",stderr);exit(666);
}