Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
October 31st, 2012, 12:26 AM
 takymounir
Registered User

Join Date: Oct 2012
Posts: 5
Time spent in forums: 1 h 44 m 29 sec
Reputation 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
October 31st, 2012, 12:50 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,264
Time spent in forums: 2 Months 4 Weeks 1 Day 17 h 18 m 9 sec
Reputation Power: 1827
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#3
October 31st, 2012, 01:08 AM
 shilpac
Contributing User

Join Date: Oct 2012
Posts: 30
Time spent in forums: 11 h 3 m 34 sec
Reputation Power: 2
Quote:
 Originally Posted by takymounir Code: ```#include #include 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
October 31st, 2012, 01:14 AM
 hovestar
Registered User

Join Date: Oct 2012
Posts: 13
Time spent in forums: 3 h 44 m 21 sec
Reputation 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
October 31st, 2012, 08:05 AM
 takymounir
Registered User

Join Date: Oct 2012
Posts: 5
Time spent in forums: 1 h 44 m 29 sec
Reputation 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
October 31st, 2012, 11:24 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,213
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 4 m 30 sec
Reputation Power: 455
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?
'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 */
__________________
[code]Code tags[/code] are essential for python code!

#7
October 31st, 2012, 12:52 PM
 takymounir
Registered User

Join Date: Oct 2012
Posts: 5
Time spent in forums: 1 h 44 m 29 sec
Reputation Power: 0
Quote:
 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
October 31st, 2012, 01:15 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,213
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 4 m 30 sec
Reputation Power: 455
'f' != 'F'

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

toupper

#9
October 31st, 2012, 01:28 PM
 ptr2void
I haz teh codez!

Join Date: Dec 2003
Posts: 2,531
Time spent in forums: 1 Month 2 Weeks 3 Days 18 h 55 m 21 sec
Reputation Power: 2337
Code:
`double fah_2_cels(double fahr );`

Code:
`temp=fahr_2_cels(num);`
__________________
I ♥ ManiacDan & requinix

This is a sig, and not necessarily a comment on the OP:
Please don't be a help vampire!

#10
November 1st, 2012, 12:16 AM
 takymounir
Registered User

Join Date: Oct 2012
Posts: 5
Time spent in forums: 1 h 44 m 29 sec
Reputation Power: 0
Quote:
 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
November 1st, 2012, 07:53 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,213
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 4 m 30 sec
Reputation Power: 455
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);
}

 Viewing: Dev Shed Forums > Programming Languages > C Programming > C Code Problem