Program crashing? 
Author Message
 Program crashing?

I've just made a simple program that should convert some number to Celsius,
and during that calculation is when my program receives a general protection
fault. What is wrong. Thank you?

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <math.h>
/*****************   Functions  ************************************/
char menu(void);
void celsius_func();
void answer(double *total);
/********************************************************************/
main()
{
  char choice;

  do
  {
  choice = menu();

  switch (choice)
  {
  case ('C'):{celsius_func();
    break;}
  case ('E'):{printf("End Program");
    break;}
  default:{printf("Enter C");}
  }
  }
  while (choice!='E');

return(0);

Quote:
}

/***************************************************************************
*/
char menu(void)
{
 char choice;

 printf("\n\n");
 printf("Conversion Calculation Program\n\n");
 printf("Enter C for Celsius to Fahrenheit\n");
 printf("Enter E to end the program");
 printf("Enter your selection: ");

 choice=getch();
 choice=toupper(choice);

 return(choice);

Quote:
}

/***************************************************************************
/
void celsius_func()
{
 double formula=0;
 double celsius=0;

 printf("\n\nEnter Celsius Temp: ");
 scanf("%lf", celsius);
 formula = celsius * 1.8 + 32;
 answer(&formula);

 return;

Quote:
}

/***************************************************************************
/
void answer(double *formula)
{
 printf("\n%10.2lf", *formula);
 printf("\n\nanswer function");

return(0);

Quote:
}



Tue, 13 Mar 2001 03:00:00 GMT  
 Program crashing?
To scanf function, the parameter is of type pointer. So please change it to
   scanf( "%lf", &celsius );

-- Baliga

Quote:

> I've just made a simple program that should convert some number to Celsius,
> and during that calculation is when my program receives a general protection
> fault. What is wrong. Thank you?

> #include <stdio.h>
> #include <stdlib.h>
> #include <conio.h>
> #include <ctype.h>
> #include <math.h>
> /*****************   Functions  ************************************/
> char menu(void);
> void celsius_func();
> void answer(double *total);
> /********************************************************************/
> main()
> {
>   char choice;

>   do
>   {
>   choice = menu();

>   switch (choice)
>   {
>   case ('C'):{celsius_func();
>     break;}
>   case ('E'):{printf("End Program");
>     break;}
>   default:{printf("Enter C");}
>   }
>   }
>   while (choice!='E');

> return(0);
> }
> /***************************************************************************
> */
> char menu(void)
> {
>  char choice;

>  printf("\n\n");
>  printf("Conversion Calculation Program\n\n");
>  printf("Enter C for Celsius to Fahrenheit\n");
>  printf("Enter E to end the program");
>  printf("Enter your selection: ");

>  choice=getch();
>  choice=toupper(choice);

>  return(choice);
> }
> /***************************************************************************
> /
> void celsius_func()
> {
>  double formula=0;
>  double celsius=0;

>  printf("\n\nEnter Celsius Temp: ");
>  scanf("%lf", celsius);
>  formula = celsius * 1.8 + 32;
>  answer(&formula);

>  return;
> }
> /***************************************************************************
> /
> void answer(double *formula)
> {
>  printf("\n%10.2lf", *formula);
>  printf("\n\nanswer function");

> return(0);
> }



Tue, 13 Mar 2001 03:00:00 GMT  
 Program crashing?

Quote:

> To scanf function, the parameter is of type pointer. So please change it to
>    scanf( "%lf", &celsius );

......snip.......

Quote:
> >  printf("\n\nEnter Celsius Temp: ");
> >  scanf("%lf", celsius);

.......snip.......

Quote:
> >  printf("\n%10.2lf", *formula);

*formula is type double.With printf(), the correct conversion specification for
type double is %f.
printf("\n10.2f,*formula);
--
Al Bowers
Tampa, FL

http:www.gate.net/~abowers/index.html


Wed, 14 Mar 2001 03:00:00 GMT  
 Program crashing?


Quote:
>I've just made a simple program that should convert some number to Celsius,
>and during that calculation is when my program receives a general protection
>fault. What is wrong. Thank you?

>#include <stdio.h>
>#include <stdlib.h>
>#include <conio.h>

<conio.h> is a non-standard header.

Quote:
>#include <ctype.h>
>#include <math.h>
>/*****************   Functions  ************************************/
>char menu(void);
>void celsius_func();

Try to use prototypes consistently

 void celsius_func(void);

Quote:
>void answer(double *total);
>/********************************************************************/
>main()

This is corrently valid in C although not a particularly good style. It
will almost certainly be invalid when the next revision of the C standard
comes into fore in a year or two. Make that

 int main(void)

Quote:
>{
>  char choice;

>  do
>  {
>  choice = menu();

>  switch (choice)
>  {
>  case ('C'):{celsius_func();
>    break;}

You don't need the ()'s or the {}'s here. It is unusual to put them in.

Quote:
>  case ('E'):{printf("End Program");
>    break;}
>  default:{printf("Enter C");}
>  }
>  }

Give some thought to your indentation style. Having adjacent }'s at the same
level suggests that there is room for improvement.

Quote:
>  while (choice!='E');

>return(0);

You don't nees the ()'s here, you can write simply

 return 0;

Quote:
>}
>/***************************************************************************
>*/
>char menu(void)
>{
> char choice;

> printf("\n\n");
> printf("Conversion Calculation Program\n\n");
> printf("Enter C for Celsius to Fahrenheit\n");
> printf("Enter E to end the program");
> printf("Enter your selection: ");

> choice=getch();

getch() is not a standard C library function. It is probably best to avoid
platform-specific fucntions for a simple program like this.

Quote:
> choice=toupper(choice);

> return(choice);

Again, you can write simply

  return choice;

Quote:
>}
>/***************************************************************************
>/

You don't appear to have terminated the comment here.

Quote:
>void celsius_func()

Again, not wrong but consider using

 void celsius_func(void)

Quote:
>{
> double formula=0;
> double celsius=0;

> printf("\n\nEnter Celsius Temp: ");
> scanf("%lf", celsius);

All of scanf's arguments must be pointers. Make that.

  scanf("%lf", &celsius);

It is also a good idea to check the return value of scanf to see if it
was able to perform a successful conversion.

Quote:
> formula = celsius * 1.8 + 32;
> answer(&formula);

> return;
>}
>/***************************************************************************
>/

You don't appear to have terminated the comment here.

Quote:
>void answer(double *formula)

There's no need to pass a pointer here, you could pass a double directly.

Quote:
>{
> printf("\n%10.2lf", *formula);

Unlike scanf(), the printf conversion specifier for double is %f, not %lf.

Quote:
> printf("\n\nanswer function");

>return(0);
>}

--
-----------------------------------------


-----------------------------------------


Wed, 14 Mar 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Program crashes(Help)

2. Program Crashing

3. why does this little program crash ?

4. program crash

5. Program crash

6. Program crashes with fscanf and string input

7. help, simple program crashes...

8. Program Crashed!

9. Help, program crashes

10. Program crash

11. program crashes before executing first line!

12. Example program crashing

 

 
Powered by phpBB® Forum Software