This is a discussion on New, please help, no compiler error but does not execute within the C Programming forums, part of the General Programming Boards category; Hi everyone I am having some trouble with a program meant to solve for x in ax^2+bx+c=0 given user-input values ...

Hi everyone

I am having some trouble with a program meant to solve for x in ax^2+bx+c=0 given user-input values for a, b, and c. This is homework and my professor requires separate functions for the input and operations in the main function and constants for any numbers in equations. The assignment also requires that we use if else statements to determine the value of x. The assignment sheet can be found here: Assignment G1 if you want any more clarification

I have written a code for it that I think should work and the compiler (DEV-C++) does not find any errors. However, when I try to execute the program it allows me to input the values but when I press Enter windows tells me the program has encountered an error and needs to close and asks if I want to send an error report. If any of you can help me I would greatly appreciate it!

Here is the code:

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void getData (double* ptrA, double* ptrB, double* ptrC);
double determine (double coefA, double coefB, double coefC);

int main (void)
{
double coefA;
double coefB;
double coefC;

getData(&coefA, &coefB, &coefC);
determine(coefA, coefB, coefC);
system("pause");
return 0;
}

void getData (double*ptrA, double*ptrB, double*ptrC)
{
double coefA;
double coefB;
double coefC;

printf("This function solves for x in the equation ax*x+bx+c=0. Please\n");
printf("enter values for coefficients a, b, and c: ");
scanf("%f %f %f", coefA, coefB, coefC);
*ptrA = coefA;
*ptrB = coefB;
*ptrC = coefC;
return;
}

double determine (double coefA, double coefB, double coefC)
{
double x;
double det;
double solA;
double solB;
const int number1 = 2;
const int number2 = 4;
double rootDet;

if ((coefA == 0) &&  (coefB == 0) && (coefC == 0))
printf("Any value of x is a solution.\n");
else if ((coefA == 0) && (coefB == 0) && (coefC != 0))
printf("No solution exists.\n");
else if ((coefA == 0) && (coefB != 0))
{
x = -coefC / coefB;
printf("The value of x is %f\n", x);
}
else if (coefA != 0)
{
det = (coefB * coefB) - (number2 * coefA * coefC);
if (det == 0)
{
x = -coefB / (number1 * coefA);
printf("The value of x is %f.\n", x);
}
else if (det > 0)
{
rootDet = sqrt(det);
solA = (-coefB + rootDet) / (number1 * coefA);
solB = (-coefB - rootDet) / (number1 * coefA);
printf("The solutions are %f and %f.\n", solA, solB);
}
else if (det < 0)
printf("The solutions have an imaginary component.\n");
return;
}}```

2. Your scanf on line 28 seems to be missing something. See this link for scanf.

Jim

3. Code:
`scanf("%f %f %f", coefA, coefB, coefC);`
scanf requires the address of the variable.

like this
Code:
`scanf("%f %f %f", &coefA, &coefB, &coefC);`
Note: I think %f is the wrong choice for double; I am not sure.

Tim S.

4. Wow, cant believe I missed that! Sorry everyone haha.

Now it executes, however no matter what values I input for a, b, and c, it prints "The value of x is -257._______). The decimals of x are slightly different depending on the coefficients. I have looked over the code a few times and I dont see why I am getting that answer. For some reason the if else statements are not working for 0 0 0 or 0 0 1, etc.

Sorry for the questions, I am completely new to programming and I am not very good with computers in the first place. I'm enjoying it, but get tripped up easily!

Thanks so much guys, I really appreciate the help.

Sam

5. Did you change the '%f' to '%lf'?

If so post the current code.

Jim

6. To follow up with Jim's comment, your compiler should throw a warning when using the wrong specifiers for scanf and printf. If you're not seeing these messages, turn up all the warnings on your compiler. It will help you catch many potential problems. If you are seeing these warnings, then pay attention to them! They're there for a reason. I got your code to work just fine by using %lf in scanf (didn't work without it). Also, you declare determin() to return a double, but you never return a double. Fix that by making it return void.

7. No warnings from the compiler whatsoever on the original code I posted. I will definitely crank up the warnings, I feel like a kid with bumper lanes in bowling haha.

changed that and it works perfectly. Thanks so much everyone! Can't believe they were both so simple

Not really an important question but why does %lf work and %f not? My textbook always shows %f in the example codes.

8. Basically because the number of bits required to hold a double is larger than the number of bits required to hold float.

Jim

9. oh ok that makes sense. Wish my professor would have said it so simply haha.

Thanks again everyone, I really appreciate it

Sam