Thread: What is wrong with my Code, can you all Check?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    37

    What is wrong with my Code, can you all Check?

    Whenever i try to run it, there's 3 erros but i don't know where, This is my first program and I'm new at this.


    Write a C program that
    Reads the coefficients (double a, b, c) of a quadratic equation a*x2..+..b*x..+..c..=..0
    Computes the roots of that equation (if exist), and
    Prints out an appropriate msg with roots (if any).
    ..
    For example,
    when a =0, we have a linear equation and we have only one root x=-c/b.
    Compute d =....4*a*c - b2.
    For d<0, we have two real roots,
    For d=0, we have one root,
    For d>0, we have no root


    Code:
    #include<stdio.h>
    #include<math.h>
    
    int main(void)
    {
        
        
    /*Declare variables*/
    double a, b, c, d;
    
    /*Request Information from user*/
    printf("Enter Value for a:");
    scanf("%lf", &a);
    
    printf("Enter Value for b:");
    scanf("%lf", &b);
    
    printf("Enter Value for c:");
    scanf("%lf", &c);
    
    /*Perform Calculations*/ 
    printf("Equation: %fx^2 + %fx + c = 0\n", a, b, c);
    
    if (a == 0)
    {
    printf("we have one root\n");
    }
    else
    {
    d = b * b - 4 * a * c;
    
    if (d < 0)
    {
    printf("we have two real roots\n");
    }
    else if (d == 0)
    {
    printf("we have one root\n");
    }
    else if (d > 0)
    {
    printf("we have no root\n");
    
    /*Exit Program*/  
    return 0;
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What errors?

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    See also here, which is where this copypasta came from.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    }

    These are the problems you will continue to have if you prefer not to INDENT YOUR CODE IN BLOCKS.

    Also, one of your printf's has too many arguments.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    have you tried a good indentation...???
    your brackets arenot balanced you know...
    take a look at the last elseif, at least you need 2 closing brackets there....

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    37
    Quote Originally Posted by MK27 View Post
    }

    These are the problems you will continue to have if you prefer not to INDENT YOUR CODE IN BLOCKS.

    Also, one of your printf's has too many arguments.

    I correct it the Indenting problem and now it runs.
    When i run it and put the values for a, b, c and click enter so it can give me an answer, it closes, and doesn't give an answers, why.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Cyberman86 View Post
    I correct it the Indenting problem and now it runs.
    When i run it and put the values for a, b, c and click enter so it can give me an answer, it closes, and doesn't give an answers, why.
    Perhaps this is a segmentation fault, in which case you will need a debugger. However, it "worked" for me.

    If you didn't write this code yourself and you are starting from scratch, it will be difficult for you to debug. It is also extremely unfinished, if you were (for example) expecting it to perform any calculations at all, so probably not worth bothering with if this is the case. You will be better off starting from scratch and writing something yourself.

    To reiterate: This is a bad piece of code. If you wrote it, that's okay, don't be embarrassed or insulted! You might get it to work eventually. But if you didn't write it, throw it away now
    Last edited by MK27; 02-12-2009 at 05:00 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by Cyberman86 View Post
    I correct it the Indenting problem and now it runs.
    When i run it and put the values for a, b, c and click enter so it can give me an answer, it closes, and doesn't give an answers, why.
    maybe you could try to use

    system( "pause" );

    or

    getch(); if you have conio.h

    but if it's still not working maybe it is a segmentation fault.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with this code?
    By Finchie_88 in forum Networking/Device Communication
    Replies: 10
    Last Post: 05-27-2005, 09:46 AM
  2. What's wrong with my Win32 Wrapper code?
    By miica in forum Windows Programming
    Replies: 9
    Last Post: 02-22-2005, 08:55 PM
  3. What's wrong with this code?
    By Tride in forum C Programming
    Replies: 5
    Last Post: 05-26-2003, 12:40 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. what's wrong with this qsort code?
    By Sargnagel in forum C Programming
    Replies: 2
    Last Post: 01-12-2003, 06:58 AM