New, please help, no compiler error but does not execute

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 ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    8

    New, please help, no compiler error but does not execute

    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. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,699
    Your scanf on line 28 seems to be missing something. See this link for scanf.


    Jim

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,541
    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.
    Last edited by stahta01; 02-23-2011 at 11:09 AM. Reason: Grammer

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    8
    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. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,699
    Did you change the '%f' to '%lf'?

    If so post the current code.


    Jim

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,451
    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. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    8
    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.
    Last edited by svisger; 02-23-2011 at 11:34 AM.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,699
    Basically because the number of bits required to hold a double is larger than the number of bits required to hold float.

    Jim

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    8
    oh ok that makes sense. Wish my professor would have said it so simply haha.

    Thanks again everyone, I really appreciate it

    Sam

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 09-16-2006, 01:18 PM
  2. C Compiler and stuff
    By pal1ndr0me in forum C Programming
    Replies: 10
    Last Post: 07-21-2006, 11:07 AM
  3. I can't get this new compiler to work.
    By Loduwijk in forum C++ Programming
    Replies: 7
    Last Post: 03-29-2006, 05:42 AM
  4. how to call a compiler?
    By castlelight in forum C Programming
    Replies: 3
    Last Post: 11-22-2005, 10:28 AM
  5. Help With finding a compiler
    By macman in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 04-15-2005, 08:15 AM

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