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

• 01-07-2013
Moody Barrawi
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.
• 01-07-2013
grumpy
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.
• 01-07-2013
Moody Barrawi
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?
• 01-07-2013
grumpy
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.
• 01-07-2013
Moody Barrawi
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?
• 01-07-2013
c99tutorial
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)"