Problem with While loop

This is a discussion on Problem with While loop within the C Programming forums, part of the General Programming Boards category; This is, what seemed like, a pretty simple homework assignment. I've tested it using all the menu options including an ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    4

    Problem with While loop

    This is, what seemed like, a pretty simple homework assignment. I've tested it using all the menu options including an invalid response, and everything works perfectly, but it seems to exit the loop even if I say Y/y at the end one run-through.

    Any help would be appreciated.

    Code:
    #include <stdio.h>
    
    int get_problem();
    void get2_pt(double *x_onep, double *y_onep, double *x_twop, double *y_twop);
    void get_pt_slope(double *x_onep, double *y_onep, double *slopep);
    void slope_intcpt_from2_pt(double x_one, double y_one, double x_two, double y_two, double *slopep, double *y_intp);
    double intcpt_from_pt_slope(double x_one, double y_one, double slope);
    void display2_pt(double x_one, double y_one, double x_two, double y_two);
    void display_pt_slope(double x_one, double y_one, double slope);
    void display_slope_intcpt(double slope, double y_int);
    
    int
    	main(void)
    {
    	double x_one,
    		x_two,
    		y_one,
    		y_two,
    		slope,
    		y_int;
    	int problem;
    	char cont;
    
    	cont = 'Y';
    	while (cont == 'Y' || cont == 'y') {
    		problem = get_problem();
    		if (problem == 1) {
    			get2_pt(&x_one, &y_one, &x_two, &y_two);
    			slope_intcpt_from2_pt(x_one, y_one, x_two, y_two, &slope, &y_int);
    			display2_pt(x_one, y_one, x_two, y_two);
    			display_slope_intcpt(slope, y_int);
    			printf("\n\nDo another conversion? (Y/N): ");
    			scanf("%c", &cont);
    		}
    		else if (problem == 2) {
    			get_pt_slope(&x_one, &y_one, &slope);
    			y_int = intcpt_from_pt_slope(x_one, y_one, slope);
    			display_pt_slope(x_one, y_one, slope);
    			display_slope_intcpt(slope, y_int);
    			printf("\n\nDo another conversion? (Y/N): ");
    			scanf("%c", &cont);
    		}
    		else if (problem == 3) {
    			printf("Exiting Program");
    			cont = 'N';
    		}
    		else {
    			printf("Invalid response.  Please try again.\n\n");
    			cont = 'Y';
    		}
    	}
    
    	return(0);
    }
    
    int
    	get_problem()
    {
    	int problem;
    
    	printf("Select the form that you would like to convert to slope intercept form:");
    	printf("\n1) Two-point form (you know two points on the line)");
    	printf("\n2) Point-slope form (you know the line's slope and one point)");
    	printf("\n3) Exit");
    	printf("\n\n>> ");
    	scanf("%d", &problem);
    	return(problem);
    }
    
    void
    	get2_pt(double *x_onep, double *y_onep, double *x_twop, double *y_twop)
    {
    	double one,
    		two;
    	printf("\nEnter the coordinates of the first point separated by one space: ");
    	scanf("%lf %lf", &one, &two);
    	*x_onep = one;
    	*y_onep = two;
    	printf("Enter the coordinates of the second point: ");
    	scanf("%lf %lf", &one, &two);
    	*x_twop = one;
    	*y_twop = two;
    }
    
    void
    	get_pt_slope(double *x_onep, double *y_onep, double *slopep)
    {
    	double x,
    		y,
    		slope;
    	printf("\nEnter the coordinates of the known point separated by one space: ");
    	scanf("%lf %lf", &x, &y);
    	printf("Enter the slope of the line: ");
    	scanf("%lf", &slope);
    	*x_onep = x;
    	*y_onep = y;
    	*slopep = slope;
    }
    
    void
    	slope_intcpt_from2_pt(double x_one, double y_one, double x_two, double y_two, double *slopep, double *y_intp)
    {
    	*slopep = ((y_two - y_one) / (x_two - x_one));
    	*y_intp = (y_one - (*slopep * x_one));
    }
    
    double
    	intcpt_from_pt_slope(double x_one, double y_one, double slope)
    {
    	double y_int;
    
    	y_int = y_one - (slope * x_one);
    	return(y_int);
    }
    
    void
    	display2_pt(double x_one, double y_one, double x_two, double y_two)
    {
    	printf("\nTwo-point form");
    	printf("\n   (%.2f - %.2f)", y_two, y_one);
    	printf("\nm = -----------");
    	printf("\n   (%.2f - %.2f)", x_two, x_one);
    }
    
    void
    	display_pt_slope(double x_one, double y_one, double slope)
    {
    	printf("\nPoint-slope form");
    	printf("\n  y - %.2f = %.2f(x - %.2f)", y_one, slope, x_one);
    }
    
    void
    	display_slope_intcpt(double slope, double y_int)
    {
    	printf("\n\nSlope-intercept form");
    	printf("\n  y = %.2fx - %.2f", slope, y_int);
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,707
    Try replacing this

    Code:
    scanf("%c", &cont);
    with this

    Code:
    scanf(" %c", &cont);
    I know it seems stupid, but it does make a difference. White space isn't ignored by %c, unlike the rest of the options so it's very possible cont equals something other than the 'y'/'Y' you think it is by mistake.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4
    That fixed it, thanks!

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,707
    You're welcome

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by whiteflags View Post
    White space isn't ignored by %c, unlike the rest of the options so it's very possible cont equals something other than the 'y'/'Y' you think it is by mistake.
    None of the options ignore whitespace. The f in the scanf and printf functions mean "formatted". It reads (or writes) exactly what you tell it to expect.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,707
    None of the options ignore whitespace.
    I'll correct myself in this manner.

    Input white-space characters (as specified by isspace()) shall be skipped, unless the conversion specification includes a [, c, C, or n conversion specifier.
    fscanf

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while loop problem
    By matt345 in forum C Programming
    Replies: 8
    Last Post: 01-12-2011, 11:49 AM
  2. Problem with Do while loop
    By prasdhy2008 in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 06:02 PM
  3. Problem with a loop
    By wiggsfly in forum C Programming
    Replies: 5
    Last Post: 11-30-2008, 11:45 AM
  4. I have problem with for(loop)
    By LINUX in forum C++ Programming
    Replies: 5
    Last Post: 03-24-2007, 03:19 PM
  5. Loop Problem..can you help please?
    By Debbie in forum C Programming
    Replies: 2
    Last Post: 12-06-2001, 08:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21