code error

Thread Starter

TAKYMOUNIR

Joined Jun 23, 2008
352
Rich (BB 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
}
this program has 2 function one for change from fehrenheit to celisus and the other from celisus to fehr.
the user will enter temperature like 125f and the program read it and figure that it is fehrenheit and convert it to celisus
i write the code and it work but i need to ad condition if temp in feh. <=-459.67 or the cel<=-273.15 both function will return error so how can i do this
 
Last edited:

panic mode

Joined Oct 10, 2011
2,761
why are you declaring everything as double?
"double num,fahr,cels;"

is the 'num' supposed to be variable to catch numeric input... or unit... or both?

what is this:
"scanf("%lg %c",&num,&num);"
why are you loading two different things into one variable?

also what happens if someone types 125f one time but 123F the other time?
(in case you didn't notice first one was lowercase and later one upper case f)
 

Thread Starter

TAKYMOUNIR

Joined Jun 23, 2008
352
why are you declaring everything as double?
"double num,fahr,cels;"

is the 'num' supposed to be variable to catch numeric input... Or unit... Or both?

What is this:
"scanf("%lg %c",&num,&num);"
why are you loading two different things into one variable?

Also what happens if someone types 125f one time but 123f the other time?
(in case you didn't notice first one was lowercase and later one upper case f)
num will catch the value and f or c
 

Thread Starter

TAKYMOUNIR

Joined Jun 23, 2008
352
why are you declaring everything as double?
"double num,fahr,cels;"

is the 'num' supposed to be variable to catch numeric input... Or unit... Or both?

What is this:
"scanf("%lg %c",&num,&num);"
why are you loading two different things into one variable?

Also what happens if someone types 125f one time but 123f the other time?
(in case you didn't notice first one was lowercase and later one upper case f)
i declare it double to be able to deal with real number
can you please run the code and see what should i do
 

panic mode

Joined Oct 10, 2011
2,761
i don't need to run it, i can see why it does not work. besides i would not want to deprive you of a chance to do some debugging, that is how you learn.

that was the reason for my previous post. did you read it?

"is the 'num' supposed to be variable to catch numeric input... Or unit... Or both?"

"scanf("%lg %c",&num,&num);"
"why are you loading two different things into one variable?"

"what happens if someone types 125f one time but 123F the other time?"

i could go on but that should get you started.
 
Last edited:

Thread Starter

TAKYMOUNIR

Joined Jun 23, 2008
352
i don't need to run it, i can see why it does not work. besides i would not want to deprive you of a chance to do some debugging, that is how you learn.

that was the reason for my previous post. did you read it?

"is the 'num' supposed to be variable to catch numeric input... Or unit... Or both?"

"scanf("%lg %c",&num,&num);"
"why are you loading two different things into one variable?"

"what happens if someone types 125f one time but 123F the other time?"

i could go on but that should get you started.
thanks
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
 

panic mode

Joined Oct 10, 2011
2,761
ok, i just run it and it converted for example 100C into 212 F. note that the input has to be entered without space since program expects it like that. if you enter value with space such as "100 c" then the unit variable in your program will get value 32 and program will fail.

32 is decimal value for ASCII space character, so first character after numeric value '100' was read in and that was the space, not the unit 'c'.

next i tried input -40f and again using breakpoint and stepping through code, i could see variables taking values -40 and f (see both values highlighted in blue at the bottom of the third image). again result is correct - at least for those two tests. you need to be specific if there is anything else you need to check.
 

Attachments

Last edited:

panic mode

Joined Oct 10, 2011
2,761
i am using Netbeans with cygwin c/c++ compiler and debuger. to run program in debug mode, obviously debugger need to be installed, and you should start program using Ctrl+F5 (or use icon directly under "Windows" menu). if you choose to run program with F6 or the large green arrow that is left from debug icon, program will just run and all break points etc will be ignored. to step through code you can use F8 to step over or F7 to step into. tabs at the bottom allow switching between output and variables windows. happy debugging.
 

t06afre

Joined May 11, 2009
5,934
TAKYMOUNIR it looks to me like you have some very strong objections. About using the debugger. You prefer to let the forum do the debugging for you. The debugger is your best friend. But you seems to allergic against any use of the debugger. How come?
 
Last edited:

WBahn

Joined Mar 31, 2012
30,077
An alternative to using a debugger is to instrument your code. That can be as simple as putting print statements at various points in your code to see what the values of variables are at those points. Sometimes this is easier and quicker than interacting with a debugger, but definitely learn at least the basics of using the debugging capabilities of your compiler.
 

WBahn

Joined Mar 31, 2012
30,077
When you post code and then ask people to comment on it, please DO NOT then go back and edit that code to reflect the discussion! The code in its original form IS what is being discussed! This is like posting the question, "What do dinosaurs eat?" and then, after three pages of discussion about dinosaurs, going back and changing the word dinosaurs in the original question to puppies. It makes the whole thread confusing as hell.
 

spinnaker

Joined Oct 29, 2009
7,830
TAKYMOUNIR it looks to me like you have some very strong objections. About using the debugger. You prefer to let the forum do the debugging for you. The debugger is your best friend. But you seems to allergic against any use of the debugger. How come?
+100M

i declare it double to be able to deal with real number
can you please run the code and see what should i do
Oh that is funny. TAKYMOUNIR that is your job not ours. We are very happy to help but we are not going to do your job for you.
 

spinnaker

Joined Oct 29, 2009
7,830
When you post code and then ask people to comment on it, please DO NOT then go back and edit that code to reflect the discussion! The code in its original form IS what is being discussed! This is like posting the question, "What do dinosaurs eat?" and then, after three pages of discussion about dinosaurs, going back and changing the word dinosaurs in the original question to puppies. It makes the whole thread confusing as hell.
Or at least say you updated the code. Not sure if you use SatckOverflow but that forum is horrible from a usage standpoint. Shame because a lot of really talented people hang out there. Anyway, you can't add corrections to your code! Your responses are simple text. So no real way to make corrections to your code other than editing your OP. :( But no excuse here. Unless of course you make an error then an hour later you fix it hoping no one has seen it.
 
Top