function returns weird value

This is a discussion on function returns weird value within the C Programming forums, part of the General Programming Boards category; I'm having trouble getting a function to work right. it's been bugging me badly.. I have a crystalfontz 633 lcd ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    function returns weird value

    I'm having trouble getting a function to work right. it's been bugging me badly.. I have a crystalfontz 633 lcd display connected to the serial port with a dallas one wire temperature sensor connected to the display. it sends a temperature report back to the computer once every second. the ConvertTempReport function is used to decode the sensor's output and return it as degrees fahrenheit. if I use printf inside the function, it works perfectly. but I want it to return the temperature as the functions output. when I do that, it gives me weird numbers. I can't figure out why it's doing that or what I'm doing wrong. someone please help. I'm running centos 5 server.

    Code:
    while(1){
      usleep(100000);  // 1/10 second
      if(check_for_packet()){
      //Terminate the incoming data so C handles it well in case it is a string.
      incoming_response.data[incoming_response.data_length]=0;
    
      double tempf;
      tempf=ConvertTempReport(incoming_response.data);
      printf("%f\n",tempf);
    
      }
    }
    
    
    double ConvertTempReport(ubyte data[4]){
      double temp;
      temp=((data[2] << 8) | data[1])/16.0;
      if(temp!=0){temp=(1.8 * temp)+32.0;}
      return temp;
    }


    this is the output:

    14336.000000
    12320.000000
    10272.000000
    8224.000000
    6176.000000
    4128.000000
    19809.000000
    17761.000000
    32097.000000
    30049.000000
    28001.000000
    25953.000000

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Did you prototype the function? If you didn't, the compiler would assume that the function returned an int, and as you're actually returning a double, that could mess things up.

    What's a prototype? Just try putting this line before both code snippets you posted, globally, perhaps just after your #includes . . . .
    Code:
    double ConvertTempReport(ubyte data[4]);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Quote Originally Posted by dwks View Post
    Did you prototype the function? If you didn't, the compiler would assume that the function returned an int, and as you're actually returning a double, that could mess things up.

    What's a prototype? Just try putting this line before both code snippets you posted, globally, perhaps just after your #includes . . . .
    Code:
    double ConvertTempReport(ubyte data[4]);

    sweet, thank you. I knew it was most likely something simple, I just needed an outside point of view. thanks :-)

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    OK, listen to warnings. This will definitely warn.
    To be able to call a function without a prototype is just pure idiocy IMO. It should not be allowed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Quote Originally Posted by Elysia View Post
    OK, listen to warnings. This will definitely warn.
    To be able to call a function without a prototype is just pure idiocy IMO. It should not be allowed.

    it wasn't giving me any warnings. I thought of adding a prototype but didn't think it would cause the problem. now I have a better understanding of it all. I'm learning as fast as I can.

    now, I'm trying to add a log file to my program. this is giving me some trouble now too. again, probably something simple. it creates the new file, but it's always empty.

    Code:
    FILE *f1=NULL;
    
    int main(int argc, char* argv[]){
    
    f1=fopen("temp.log","w");
    if(f1==NULL){printf("Error opening temp.log for writing");}
    
    fputs("test",f1);
    
    fclose(f1);
    }

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Quote Originally Posted by lorenzo View Post
    it wasn't giving me any warnings. I thought of adding a prototype but didn't think it would cause the problem. now I have a better understanding of it all. I'm learning as fast as I can.
    Then enable warnings. With Dev-C++, choose Options->Compiler Options, and add "-W -Wall" into the extra compiler options textbox. (Without the quotes.) Or something like that. If you use another IDE, just say so.

    now, I'm trying to add a log file to my program. this is giving me some trouble now too. again, probably something simple. it creates the new file, but it's always empty.
    Code:
    FILE *f1=NULL;
    
    int main(int argc, char* argv[]){
    
    f1=fopen("temp.log","w");
    if(f1==NULL){printf("Error opening temp.log for writing");}
    
    fputs("test",f1);
    
    fclose(f1);
    }
    Hmm, that's strange. It looks okay. Perhaps you should try printing a newline after "test", as "test\n", or calling fflush(f1) just before closing it. Neither should be required, but I can't see much else wrong with it. Assuming you #include <stdio.h>.

    Another thing: you should exit or something upon encountering an error reading the file. Or else you'll try to call fputs("test", NULL), which will probably crash.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Another thing is that you could benefit from is indentation help.
    It's a very nice thing and helps find bugs, too!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework help
    By mkdl750 in forum C Programming
    Replies: 45
    Last Post: 07-17-2008, 10:44 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21