Thread: function program help

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3

    function program help

    I had to do this program which calculates wind chill factor for my homework.
    2 Problems:
    -the answer is supposed to have 4 numbers after the decimal but it is just coming out as an integer with no decimal points even though I used setprecision(4)?
    -the answer i am getting is not what the prof's answer was on our handout...
    W= 33 – ((10√v – v + 10.5)(33-t))/23.1
    is the equation, where w is the windchill factor, v is wind speed and t is temperature...did I write it correctly in the code?
    Thanks a bunch.

    Code:
    #include <iostream>
    using std::cout; 
    using std::endl; 
    using std::cin; 
    #include <cmath>
    #include <iomanip>
    using std::fixed; 
    using std::setprecision; 
    int windchill(double, double); 
    int main()
    { 
    double wind, temp; 
    cout<<"Please enter the wind speed: "; 
    cin>>wind; 
    cout<<"Please enter the temperature: "; 
    cin>>temp;
    while (temp>10) 
    { 
    cout<<"The temperature should be less than 10.  Try again: "; 
    cin>>temp; 
    } 
    cout<<"The wind chill index is "<<setprecision(4)<<fixed<<windchill(wind,temp)<<endl;
     return 0; 
    } 
    int windchill(double v, double t) 
    { 
    int w;
    w=33-(((10*sqrt(v)-v+10.5)*(33-t))/23.1)<<endl; 
    return w; 
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Make your function return double rather than int perhaps?
    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. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    Hmm when I do that 2 error messages come up..

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    when you changed the return type did you also change it in the function prototype? also change w to double, or better yet, get rid of it and just: return (33-(((10*sqrt(v)-v+10.5)*(33-t))/23.1))

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    excellent -- it works now!! thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM