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);
}
Re: General protection exception??
Quote:
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.