Thread: why doesn't this work?

  1. #1
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55

    why doesn't this work?

    this is my 1st attempt with pointers and I can't get this program to tell if there not real or to produce roots. Any help would be great... cuz I know its not 0.000 0.000 thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    void readabc(double *a, double *b, double *c, char *real);
    
    void quadroots(double *a,double *b,double *c,double *root1,double *root2, char *real);
    
    int main()
    {
        double a, b, c, root1, root2;
        char real;
        readabc( &a, &b, &c, &real);
        quadroots(&a, &b, &c, &root1, &root2, &real);
        real = b*b-4*a*c<0;
        real==1;
    
        root1=(-b+sqrt(b*b-4*a*c)/(2*a));
        root2=(-b-sqrt(b*b-4*a*c)/(2*a));
        return 0;
    }
    
    void readabc(double *a, double *b, double *c,char *real)
    {
        cout << "Enter a,b,c for a quad: " << endl;
        cin >> *a >> *b >> *c;
        return;
    }
    
    void quadroots(double *a,double *b,double *c,double *root1,double *root2, char *real)
    {
        if (!*real)
        {
            printf("roots are not real\n");
        }
        else
        {
            printf ("roots are %lf %lf\n", *root1, *root2);
        }//check for real roots and calculate root1 and root2
    }
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  2. #2
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    on line 15 you define 'char real;'

    line 35: 'if (!*real)'
    breakdown, real is a pointer to line 15.
    *real, returns the value pointed to by real.
    ! <value>

    if that value is 0, it will be true, else 1-255 will return false.

    You never set the value of real, only the pointer to it.

    Line 19 should produce a compiler warning,
    You should read it.

    Line 16 - 18 are not in the proper logic order for the math your trying to use.

    and you might have un-used includes
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Use std::cout for output instead of printf. There is no need for any type modifiers, just output it as you would any text.
    So
    printf ("roots are %lf %lf\n", *root1, *root2);
    should be
    std::cout << "roots are " << *root1 << *root2 << std::endl;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    ok been working on it and now I just can't get it to give the correct roots.... can anyone help me?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    void readabc(double *a, double *b, double *c, char *real);
    
    void quadroots(double a,double b,double c,double *root1,double *root2, char *real);
    
    int main()
    {
       
        double a, b, c, root1, root2;
        char real;
        readabc( &a, &b, &c, &real);
        quadroots(a, b, c, &root1, &root2, &real);
    
        return 0;
    }
    
    void readabc(double *a, double *b, double *c,char *real)
    {
        cout << "Enter a,b,c for a quad: " << endl;
    
        cin >> *a >> *b >> *c;
        return;
    }
    
    void quadroots(double a,double b,double c,double *root1,double *root2, char *real)
    {
    
        if (b*b-4*a*c < 0)
        {
            printf("roots are not real\n");
            return;
        }
    
        else
        {
            *root1=(-b+sqrt(b*b-4*a*c)/(2*a));
            *root2=(-b-sqrt(b*b-4*a*c)/(2*a));
            printf ("roots are %lf %lf\n", *root1, *root2);
        }//check for real roots and calculate root1 and root2
    return;
    }
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  5. #5
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    ok I got it to work better but it is acting funny with 0 0 1 when it should say "Sorry the roots are not real!!" anyone know why it does that and how I can fix that?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    void readabc(double *a, double *b, double *c, char *real);
    
    void quadroots(double a,double b,double c,double *root1,double *root2, char *real);
    
    int main()
    {
        
        double a, b, c, root1, root2;
        char real;
        readabc( &a, &b, &c, &real);
        quadroots(a, b, c, &root1, &root2, &real);
    
        return 0;
    }
    
    void readabc(double *a, double *b, double *c,char *real)
    {
        cout << "Enter a,b,c for your quadratic you need to calculate:" << endl;
    cout << "(note:you must type a enter b enter c enter) " << endl;
        cin >> *a >> *b >> *c;
        return;
    }
    
    void quadroots(double a,double b,double c,double *root1,double *root2, char *real)
    {
        double realnum = b*b-4*a*c;
        if (realnum < 0)
        {
            printf("Sorry the roots are not real!!\n");
            *real = 0;
            return;
        }
    
        else
        {
            *real= 1;
            *root1=(-b+sqrt(realnum))/(2*a);
            *root2=(-b-sqrt(realnum))/(2*a);
            printf ("roots are %lf %lf\n", *root1, *root2);
        }//check for real roots and calculate root1 and root2
        return;
    }
    ~Cess~
    AKA : total newbie
    ....and totally frustrated
    thanks for any help given.....
    I feel like I"m going to fail this class....blah!

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What result were you expecting to get when dividing by zero? You tend to need to avoid doing that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why have you not replaced the printfs?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-07-2010, 06:53 AM
  2. Anyone see why this doesn't work?
    By tzuch in forum C Programming
    Replies: 4
    Last Post: 03-27-2008, 08:40 AM
  3. Why Doesn't This Work??!!
    By adinclik in forum C Programming
    Replies: 4
    Last Post: 04-17-2006, 05:20 AM
  4. slash-b doesn't work when followed by slash-n
    By rllovera in forum C Programming
    Replies: 6
    Last Post: 03-11-2006, 12:28 PM
  5. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM