Thread: Adding a yes or no (Y/N) function

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    20

    Exclamation Adding a yes or no (Y/N) function

    Okai, so this is the program I have reached, it works perfectly. But there is one thing I need. I need to add a Y/N function. What I mean is I want for there to be a question to ask the user at the end of the program, 'Do you want to make an other calculation and for it to go from the beginning if I write Y and exit if I write N.
    #include <stdio.h>

    Code:
    int get_problem(void);
    double get_pt_slope(double *m, double *x1, double *y1);
    double get2_pt(double *x1,double *y1,double *x2,double *y2);
    double slope_intcpt_from2_pt(double x1,double y1,double x2,double y2,double *m, double *intercept);
    double intcpt_from_pt_slope(double m,double x1, double y1,double *intercept);
    double display2_pt(double x1,double y1,double x2,double y2);
    double display_pt_slope(double m,double x1,double y1);
    double display_slope_intcpt(double m, double intercept);
    
    
    
    
    int main()
    {
    int x;
    x=get_problem();
    
    
    
    
    if (x==1)
    {
        double x1,y1,x2,y2,intercept,m;
        get2_pt(&x1,&y1,&x2,&y2);
        slope_intcpt_from2_pt(x1,y1,x2,y2,&m,&intercept);
        display2_pt(x1,y1,x2,y2);
        display_slope_intcpt(m,intercept);
    }
    if (x==2)
    {
        double m,x1,y1,intercept;
        get_pt_slope(&m,&x1,&y1);
        intcpt_from_pt_slope(m,x1,y1,&intercept);
        display_pt_slope(m,x1,y1);
        display_slope_intcpt(m,intercept);
    }
    
    
    
    
    
    
    
    
    
    
    return (0);
    
    
    }
    
    
    int get_problem(void)
    {
        int x;
        printf("Select the form that you would like to convert to slope-intercept form: \n 1) Two-point form (you know two points on the line) \n 2) Point =slope form (you know the line's slope and one point) \n  =>");
        scanf("%d",&x);
        return(x);
    }
    double get_pt_slope(double *m, double *x1, double *y1)
    {
    
    
        printf("Enter the slope =>");
        scanf("%lf",&*m);
        printf("Enter the x-y coordinates of the point seperated by a space =>");
        scanf("%lf%lf",&*x1,&*y1);
    }
    double get2_pt(double *x1,double *y1,double *x2,double *y2)
    {
        printf("Enter the x-y coordinates of the first point seperated by a space =>");
        scanf("%lf%lf",&*x1,&*y1);
        printf("Enter the x-y coordinates of the second point seperated by a space =>");
        scanf("%lf%lf",&*x2,&*y2);
    }
    double slope_intcpt_from2_pt(double x1,double y1,double x2,double y2,double *m, double *intercept)
    {    
        *m=(y2-y1)/(x2-x1);        
        *intercept=((*m*(-x1))+y1);        
    }
    double intcpt_from_pt_slope(double m, double x1, double y1, double *intercept)
    {
        *intercept=(m*(-x1))+y1;        
    }
    
    
    double display2_pt(double x1,double y1,double x2,double y2)
    {
        printf("Two Point Form \n     ((%.2f-%.2f) \n m=-----------------\n    (%2f-%2f))\n",y2,y1,x2,x1);
    }
    double display_pt_slope(double m,double x1,double y1)
    {
            printf("Point-Slope Form \n  y-%.2f=%.2f(x-%.2f)\n",y1,m,x1);
    }
    double display_slope_intcpt(double m, double intercept)
    {
        printf("Slope-Intercept Form \n y=%.2fx+%.2f ",m,intercept);
    }


    And basically we are using the C-free compiler, and this is extreme basics of the c programming. All we have took is shown to you here, with the addition of for loops and while loops.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    None of that code is relevant to a "Y/N" function (except, presumably, you want to use that "Y/N" function somewhere in all that).

    Have you tried writing a function that behaves like your "Y/N" function might? (for example, writing a prompt, and then returning an indication of whether Y, N, or something else was entered by the user)

    Try doing that first. One you have done that, see if you can work out where to call it, and how to make your code loop.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    20
    Oooh, so i make a function that behaves like a loop that only goes out if N was put, and go around in the loop if I press Y. And I should make all of the MAIN program inside this function is what you are saying?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    No. I'm saying don't worry about the loop just yet.

    I'm saying writing a function that prompts the user, receives user input, and then returns an indication of what was input.

    Once you have a function like that, you can work where to add a loop construct. One part of the loop will be a call to said function. Another part of the loop will be code you want to do over and over again.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    20
    So does the function go like char x; printf "do you want to do an other conversion"; then scanf "%c",'x' then if x==N | x==n then exit the program and if x==Y | x==y then repeat the program?

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    I think the suggestion is to start simple. For example, you could start with the "Hello world" program and add a yes/no function to that.

    1. Print "Hello World"
    2. Ask the user "Do you want to see it again? (y/n)"
    3. Read the answer
    4. If the answer is "y", repeat step 1.

    That is the procedure outline. You will want to use either a "for" or a "while" or a "do...while" construction to implement the loop from 4. to 1.

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    20
    I know how to do that but my program is in a big scale. So I kind of get confused

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    That's the reason for starting small. Do the loop with a small program, and when it works... do the same on the large program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 05-11-2011, 09:41 AM
  2. Adding to a vector from a function
    By 7smurfs in forum C++ Programming
    Replies: 3
    Last Post: 09-09-2005, 09:16 PM
  3. adding a function to a program
    By trippedwire in forum C++ Programming
    Replies: 9
    Last Post: 09-30-2004, 12:35 PM
  4. Adding a timer to a recursive function
    By kippwinger in forum C++ Programming
    Replies: 7
    Last Post: 04-22-2004, 05:32 PM
  5. Adding double numbers in a function
    By ybala in forum C Programming
    Replies: 1
    Last Post: 01-29-2003, 09:36 AM