Cammaaron
August 3rd, 2010, 03:28 AM
When I try to run my program I will get a Segmentation fault error.
When I compile the code I get the warnings
math.c: In function ‘programone’:
math.c:63: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double’
math.c:65: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double’
math.c:67: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double’
Here is the source code, I got line 63, 65, and 67 marked with ">>> "
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int jsw_flush(void);
int programone(void);
int main()
{
char c;
for (;;)
{
printf("1: solve for right triangle \n");
printf("0: Exit program\n");
printf("\nSelect Your choice: ");
c = getchar();
jsw_flush();
printf("\n");
if(c=='0')
{
break;
}
switch (c)
{
case '1':
programone();
break;
case '0':
break;
default:
printf("wrong choice\n");
break;
}
}
return(0);
}
int jsw_flush (void)
{
int ch; /* getchar returns an int */
/* Read characters until there are none left */
do
ch = getchar();
while (ch != EOF && ch != '\n');
clearerr (stdin); /* Clear EOF state */
}
programone(void)
{
float x = 0.0;
float y = 0.0;
float z = 0.0;
float answer = 0.0;
char c;
for (;;)
{
printf("Please enter what numbers you know for the triangle\n");
printf("If all numbers are entered, it will test to see if this is a right triangle\n");
printf("\nWhat is the first side: ");
>>> scanf("%f",x);
printf("\nWhat is the second side: ");
>>> scanf("%f",y);
printf("\nWhat is the long side: ");
>>> scanf("%f",z);
printf("\n");
if (x = 0)
{
x = (sqrt(pow(z,2)-pow(y,2)));
printf ("The answer is : %f.\n", x);
}
else if (y = 0)
{
y = (sqrt(pow(z,2)-pow(x,2)));
printf ("The answer is : %f.\n", y);
}
else if (z=0)
{
z=(sqrt(pow(x,2)+pow(y,2)));
printf("The answer is : %f\n",z);
}
else if (x!=0&&y!=0&&z!=0)
{
answer = (sqrt(pow(x,2) + pow(y,2)));
if (answer=z)
{
printf ("This is a right triangle.\n");
}
else
{
printf ("This is not a right triangle.\n");
}
}
printf("Do you want to another one? (y or n)\n");
c = getchar();
jsw_flush();
if (c!='y'||c!='Y')
{
break;
}
}
}
When I compile the code I get the warnings
math.c: In function ‘programone’:
math.c:63: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double’
math.c:65: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double’
math.c:67: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double’
Here is the source code, I got line 63, 65, and 67 marked with ">>> "
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int jsw_flush(void);
int programone(void);
int main()
{
char c;
for (;;)
{
printf("1: solve for right triangle \n");
printf("0: Exit program\n");
printf("\nSelect Your choice: ");
c = getchar();
jsw_flush();
printf("\n");
if(c=='0')
{
break;
}
switch (c)
{
case '1':
programone();
break;
case '0':
break;
default:
printf("wrong choice\n");
break;
}
}
return(0);
}
int jsw_flush (void)
{
int ch; /* getchar returns an int */
/* Read characters until there are none left */
do
ch = getchar();
while (ch != EOF && ch != '\n');
clearerr (stdin); /* Clear EOF state */
}
programone(void)
{
float x = 0.0;
float y = 0.0;
float z = 0.0;
float answer = 0.0;
char c;
for (;;)
{
printf("Please enter what numbers you know for the triangle\n");
printf("If all numbers are entered, it will test to see if this is a right triangle\n");
printf("\nWhat is the first side: ");
>>> scanf("%f",x);
printf("\nWhat is the second side: ");
>>> scanf("%f",y);
printf("\nWhat is the long side: ");
>>> scanf("%f",z);
printf("\n");
if (x = 0)
{
x = (sqrt(pow(z,2)-pow(y,2)));
printf ("The answer is : %f.\n", x);
}
else if (y = 0)
{
y = (sqrt(pow(z,2)-pow(x,2)));
printf ("The answer is : %f.\n", y);
}
else if (z=0)
{
z=(sqrt(pow(x,2)+pow(y,2)));
printf("The answer is : %f\n",z);
}
else if (x!=0&&y!=0&&z!=0)
{
answer = (sqrt(pow(x,2) + pow(y,2)));
if (answer=z)
{
printf ("This is a right triangle.\n");
}
else
{
printf ("This is not a right triangle.\n");
}
}
printf("Do you want to another one? (y or n)\n");
c = getchar();
jsw_flush();
if (c!='y'||c!='Y')
{
break;
}
}
}