Thread: Testing what can be done with a quadratic equation solver

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    5

    Testing what can be done with a quadratic equation solver

    I'm in college, and one of my previous assignments in my C class was to write a program that can solve a quadratic equation to give the x coordinates. I did all that, but I felt it could do with some refining and, more so, experimentation... I'm still in the middle of all the tweaks, so the code is kind of butchered, and I've commented out a lot of what I previously had in case I need it later on, but I've run into an issue that I just can't seem to figure out for some reason... I'll leave out all the code that actually deals with solving the equation, as what I'm having trouble with is just passing the values of the variables from one function to another.

    What I'm trying to do is make it so that whenever asked for the variables 'a', 'b', and 'c', if the user inputs a character rather than a digit, or a digit that's just insane to begin with, the program tells the user they input an unrecognized value, and to please retry. This is while looped so that any similar occurrences will lead to the same result, until a valid number is input(anything below like 2billion or not a character). Once the user inputs a valid response for the first variable, the program will then move on to the second, where everything, including the while loop, is identical, same goes for the third. From there, I'm aiming for the program to take the values that the user input and carry them to a fourth function where it will actually use the values to solve the equation, but I can't seem to figure out how to call user input variables from 3 (technically 6, since the functions that are looped will need to be called upon once a valid response is given) separate functions to be ran through the one that solves it. So far the furthest I've got is just an error saying "too few arguments for function to call" anytime any of the functions are told to run run_calc(), the function that solves the equation. Like I mentioned, I've been experimenting a good bit with it, so some of the code might be pointless, I'll be cleaning it all up after I get things figured out...
    The purposes of the different functions are as follows:

    main acts as the facilitator for looping the entire program
    input_a asks for the first variable of the equation
    input_b asks for the second
    input_c asks for the third
    run_calc runs the whole equation
    no_letters_a is for if the first variable isn't a valid response
    no_letters_b is for the second variable
    no_letters_c is for the third
    wrong_choice is if the user inputs an invalid response when asked if they want to repeat

    If anyone could help me with this, I'd really appreciate it... been working on it for the past couple days now, and have GOT to get started on my other classes' assignments... haha

    Code:
    #include <stdio.h>#include <math.h>
    
    
    
    //void single_quad();
    void wrong_choice();
    void no_letters_a();
    void no_letters_b();
    void no_letters_c();
    void input_a();
    void input_b();
    void input_c();
    void run_calc();
    
    int main(double a, double b, double c)
    {
        double a;
        double b;
        double c;
    
        char choice = ' ';
    
    
        do
        {
            input_a();
            getc(stdin);
    
            printf("\nIf you would like to input another equation, please hit 'y', otherwise hit 'n':\t");
            scanf_s("%c", &choice, 1);
    
            if (choice == '%c')
            {
                do
                {
                    wrong_choice();
                    getc(stdin);
                } while (choice != 'y', choice != 'n');//here goes the function for when a character other than 'y' or 'n' is input
            }
            printf("\n");
    
        }while (choice == 'y');
        
        return 0;
    }
    
    void input_a()
    {
        double a;
    
        printf("Please enter the variable for 'a':\t");
        scanf_s("%lf", &a);
        if (a == '%lf')
        {
            do
            {
                no_letters_b();
                getc(stdin);
            } while (a > 2000000000 || a < -2000000000);
        }
        input_b();    
        return a;
        //printf("%lf\n", a);
    }
    
    void input_b()
    {
        double b;
    
        printf("Please enter the variable for 'b':\t");
        scanf_s("%lf", &b);
        if (b == '%lf')
        {
            do
            {
                no_letters_b();
            }while (b > 2000000000 || b < -2000000000);
        }
        input_c();
        return b;
    }
    
    void input_c()
    {
        double c;
    
        
        printf("Please enter the variable for 'c':\t");
        scanf_s("%lf", &c);
        if (c == '%lf')
        {
            do
            {
                no_letters_c();
            }while (c > 2000000000 || c < -2000000000);
        }
        run_calc();// it's here I keep running into an issue, "too few arguments in function to call" is all I get
        return c;
    }
    
    void run_calc(double a, double b, double c)
    {
        double a_calc = a;
        double b_calc = b;
        double c_calc = c;
        
        printf("\na = %lf\nb = %lf\nc = %lf\n", &a_calc, &b_calc, &c_calc);
    
        double d = -a_calc;
        double e = -b_calc;
        double f = -c_calc;
    
    |
    |
    |
    |//all these lines are where the equation's code is, took up well over half the post with that alone so I cut it out
    |
    |
    }
    
    void wrong_choice()
    {
        char choice = ' ';
    
        printf("I'm sorry, that selection is not recognized, please input 'y' for yes or 'n' for no.");
        scanf_s("%c", &choice, 1);
        printf("\n");
    }
    
    void no_letters_a()
    {
        double a;
    
        getc(stdin);
        printf("\nI'm sorry, you have input an unrecognized value, please input a numeric digit:\t");
        scanf_s("%lf", &a);
    
        if (a < 2000000000 && a > -2000000000)
        {
            input_b();
        }
        else
        {
            no_letters_a();
        }
    
    }
    
    void no_letters_b()
    {
        double b;
    
        printf("\nI'm sorry, you have input an unrecognized value, please input a numeric digit:\t");
        scanf_s("%lf", &b);
        if (b < 2000000000 && b > -2000000000)
        {
            input_c();
        }
        else
        {
            no_letters_b();
        }
    }
    
    void no_letters_c()
    {
        double a;
        double b;
        double c;
    
        printf("\nI'm sorry, you have input an unrecognized value, please input a numeric digit:\t");
        scanf_s("%lf", &c);
        if (c < 2000000000 && c > -2000000000)
        {
            run_calc();
        }
        else
        {
            no_letters_c();
        } }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,684
    Code:
    } while (choice != 'y', choice != 'n');
    Likely you want

    Code:
    } while (choice != 'y' && choice != 'n');
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    5
    I hadn't noticed that bit since it was working fine without any adjustment, but it probably should be haha

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    677
    @Deus_Venatus

    There are many problems with this code, It obviously cannot compile, or if it did, it did so with warnings turned off!

    Code:
    int main(double a, double b, double c)
    
    //main is either defined as:
    
    int main(void) // No command line arguments
    int main(int argc, char *argv[])  // With Command line args
    There is a third but just a variation on the second example

    Code:
    if (choice == '%c')
    '%c' is actually "%c", a string, not a char, and is NOT what you wanted to use anyway!

    There are far more problems that I have time to point out right now.

    Turn on, and turn up your warning levels to the highest level, and examine ALL the warnings, and errors presented!!!

  5. #5
    Registered User
    Join Date
    Oct 2019
    Posts
    5
    That bit with the main is one of the parts I was experimenting on, trying to figure out pointers but know it's the wrong method, I was at that point when I just decided to go for help on here and forgot to clear out the parentheses, as for the '%c' instead of "%c", it's how my coding instructor told us to type it, since we defined char as being > ( char choice = ' '; ) I'm guessing the space is supposed to be a place holder, but hadn't thought to ask further, it could be his mistake, or just a typo, not sure but I'll ask

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quadratic equation
    By Tokalosh in forum C Programming
    Replies: 7
    Last Post: 04-13-2017, 01:13 AM
  2. Quadratic Equation
    By joboo in forum C Programming
    Replies: 5
    Last Post: 10-18-2013, 03:28 AM
  3. Extreme beginner quadratic roots solver
    By browser in forum C Programming
    Replies: 3
    Last Post: 11-08-2009, 11:33 AM
  4. quadratic equation
    By nynicue in forum C Programming
    Replies: 5
    Last Post: 12-26-2008, 05:57 PM
  5. Quadratic Equation
    By Lucid15 in forum C Programming
    Replies: 20
    Last Post: 09-16-2008, 05:59 PM

Tags for this Thread