code error

Discussion in 'Programmer's Corner' started by TAKYMOUNIR, Oct 31, 2012.

  1. TAKYMOUNIR

    Thread Starter Active Member

    Jun 23, 2008
    351
    1
    Code ( (Unknown Language)):
    1. #include<stdio.h>
    2. #include<ctype.h>
    3. void work(void);
    4. double fah_2_cels(double fahr );
    5. double cels_2_fahr(double cels);
    6. int main(void)
    7. {
    8. char more;
    9. puts("\n This program for converting temperature from c to f and vice versa");
    10. printf("\n--------------------------------------------------------------------\n");
    11. do{
    12. work();
    13. puts("\n do you want to continue(Y/N)?");
    14. scanf(" %c" ,&more);
    15. }while(toupper(more)!='N');
    16. return 0;
    17. }
    18. void work(void)
    19. {
    20. double temp,temp1,con;
    21. char unit;
    22. printf("enter the temperature value follwed by f for fehrenhuit and c for celisus\n ");
    23. scanf("%lg",&temp1);
    24. scanf("%c",&unit);
    25. /* if(((temp1<=-459.67&&unit= 'f')||(temp1<=-459.67 &&unit='F')) || ((temp1<=-273.15 &&unit='c' )||(temp1<=-273.15 &&unit='C')))
    26. {
    27. con=-500;
    28. printf("error -500 your temp is lower than absolute zero");
    29. }*/
    30. switch(toupper(unit))
    31. {
    32. case 'F':
    33. temp=fah_2_cels(temp1);
    34. printf("temperature in cel is %g C and in fehren is %g F" ,temp,temp1);
    35. break;
    36. case 'C':
    37. temp=cels_2_fahr(temp1);
    38. printf("temperature in fahr is %g F and in celisus is %g C",temp,temp1);
    39. break;
    40. default:
    41. printf("You did enter wrong letter or you did not enter letter at all");
    42. }
    43. }//closes void work()
    44. double fah_2_cels(double fahr)
    45. {
    46. return ((5.0/9.0)*(fahr-32.0));
    47. }
    48. double cels_2_fahr(double cels)
    49. {
    50. return ((9.0/5.0)*cels +32.0); //If you find that the results are off look at these functions
    51. }
    52.  
    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: Nov 1, 2012
  2. panic mode

    Senior Member

    Oct 10, 2011
    1,320
    304
    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)
     
    TAKYMOUNIR likes this.
  3. TAKYMOUNIR

    Thread Starter Active Member

    Jun 23, 2008
    351
    1
    num will catch the value and f or c
     
  4. TAKYMOUNIR

    Thread Starter Active Member

    Jun 23, 2008
    351
    1
    i declare it double to be able to deal with real number
    can you please run the code and see what should i do
     
  5. panic mode

    Senior Member

    Oct 10, 2011
    1,320
    304
    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: Oct 31, 2012
    TAKYMOUNIR likes this.
  6. TAKYMOUNIR

    Thread Starter Active Member

    Jun 23, 2008
    351
    1
    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
     
  7. panic mode

    Senior Member

    Oct 10, 2011
    1,320
    304
    what software you use and how are you testing? do you use breakpoints or step through code line by line and monitor variables?
     
    TAKYMOUNIR likes this.
  8. panic mode

    Senior Member

    Oct 10, 2011
    1,320
    304
    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.
     
    Last edited: Nov 1, 2012
    TAKYMOUNIR likes this.
  9. panic mode

    Senior Member

    Oct 10, 2011
    1,320
    304
    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.
     
    TAKYMOUNIR likes this.
  10. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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: Nov 1, 2012
    TAKYMOUNIR likes this.
  11. TAKYMOUNIR

    Thread Starter Active Member

    Jun 23, 2008
    351
    1
    i use code block gcc
     
  12. panic mode

    Senior Member

    Oct 10, 2011
    1,320
    304
    it has same debugging facilities. you should spend few minutes and learn about it. it will be worth it.
     
  13. WBahn

    Moderator

    Mar 31, 2012
    17,724
    4,788
    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.
     
  14. WBahn

    Moderator

    Mar 31, 2012
    17,724
    4,788
    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.
     
  15. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,867
    994
    +100M

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

    AAC Fanatic!

    Oct 29, 2009
    4,867
    994
    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.
     
Loading...