General protection exception??

This is a discussion on General protection exception?? within the C Programming forums, part of the General Programming Boards category; I keep gettin a General protection exception and I don't know why. I had this program working before I add ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    9

    General protection exception??

    I keep gettin a General protection exception and I don't know why. I had this program working before I add <math.h> but know it just bums out on me.


    #include<stdio.h>
    #include<math.h>
    float cal_winchil (float v,float t);
    void print_chill (float a);
    void main (void)
    {
    float b,c,d,e;

    printf("Would you like to find the wind chill:Press any number(0 to quit)");
    scanf("%f",&b);
    while(b!=0)
    {
    printf("Please enter the wind velocity:\n");
    scanf("%f",&e);
    printf("Please enter a Temperature:\n");
    scanf("%f",&c);

    if(c<=10)
    {
    d=cal_winchil(c,e);
    print_chill(d);
    }

    else
    {
    printf("Wind chill is not valid past +10");
    }
    printf("\n\n\nEnter another number (0 to quit):\n");
    scanf("%f",&b);
    }
    }
    float cal_winchil (float v,float t)
    {
    float w;
    w=33-(10*v-sqrt(v)+10.5)*(33-t)/23.1;
    return(w);
    }

    void print_chill (float a)
    {
    printf("The wind chill is:%3.2f",a);
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    checking it out....will reply...

    NOPE pasted and compiled ok with my compiler

    test data any number 8
    velocity 10 temp 4
    answer - 15.29

    number 3
    v 25 t 3
    answer +19.57

    guess something must be wrong with your method of calculation
    might have something to do with order of precedence
    Last edited by bigtamscot; 10-01-2001 at 04:20 PM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    your code worked fine with my compiler too, what was the input , that cause the crash ?

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Worked for me too....except one little piece

    void main(void)

    before Salem looks at this:

    use int main(void).

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    I think that as well as probable problems with precedence, you get the warning when trying to input a negative value with the use of function sqrt() as declared in math.h.....An error occurs if value of number is negative
    e.g. temp of -1....

    post your equation for windchill factor, and maybe we could sort precedence out....
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    412

    Re: General protection exception??

    Originally posted by jonesy
    I keep gettin a General protection exception and I don't know why. I had this program working before I add <math.h> but know it just bums out on me.


    #include<stdio.h>
    #include<math.h>
    float cal_winchil (float v,float t);
    void print_chill (float a);
    void main (void)
    {
    float b,c,d,e;

    printf("Would you like to find the wind chill:Press any number(0 to quit)");
    scanf("%f",&b);
    while(b!=0)
    {
    printf("Please enter the wind velocity:\n");
    scanf("%f",&e);
    printf("Please enter a Temperature:\n");
    scanf("%f",&c);

    if(c<=10)
    {
    d=cal_winchil(c,e);
    print_chill(d);
    }

    else
    {
    printf("Wind chill is not valid past +10");
    }
    printf("\n\n\nEnter another number (0 to quit):\n");
    scanf("%f",&b);
    }
    }
    float cal_winchil (float v,float t)
    {
    float w;
    w=33-(10*v-sqrt(v)+10.5)*(33-t)/23.1;
    return(w);
    }

    void print_chill (float a)
    {
    printf("The wind chill is:%3.2f",a);
    }

    I can see one Biiiig problem, and again, as I mentioned in another post, you don't name your variables well, so you don't see the problem immediately.

    You call the cal_winchil() function with the arguments c,e. Here, c = temp, e = velocity. But your calculate wants the values in the OPPOSITE order, i.e. velocity first, then temp. So you could call it with cal_windchill(e,c);

    Please, for your own sanity, and the sanity of all who read your code, name your variables DESCRIPTIVELY! "c", "e", etc mean NOTHING when I read your code. Name them "velocity", "wchill", etc. and it's MUCH easier to read. It probably wouldn't have taken 9 days for someone to tell you your variables were out of order had you simply named them better in the first place.

    I knew a CS prof once who told his class that if your code needs comments, you wrote it wrong. Your variables and functions should be clearly named, so that when you read each line, you understand instantly what it does.

    Your code, BTW, crashes when the user inputs a negative temperature -- because you mix up temperature and velocity when you pass them to your calculation function, your calculation thinks you have a negative VELOCITY, and takes the square root of a negative number. And crashes.
    Last edited by The V.; 10-10-2001 at 01:05 AM.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    9

    I got

    I am running Borland Turbo C++ 4.5
    on a Windows ME machine
    and I get all kinds of funny errors

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A general protection exception failure
    By louis_mine in forum C Programming
    Replies: 7
    Last Post: 11-25-2004, 02:22 AM
  2. Please assistance, general protection mistake
    By louis_mine in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2004, 11:45 PM
  3. General Protection Exception
    By s9uare in forum C++ Programming
    Replies: 1
    Last Post: 11-02-2002, 10:46 AM
  4. General Protection?
    By drdroid in forum C++ Programming
    Replies: 1
    Last Post: 08-17-2002, 11:09 AM
  5. general protection exception
    By chiw in forum C Programming
    Replies: 1
    Last Post: 08-14-2002, 10:54 AM

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