Thread: help with WHILE Loop

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    2

    help with WHILE Loop

    Hi all,
    for some reason my WHILE loop is not working, I have tried a few different things (Inc a do-while) and it just won't do what I want!

    Thanks in advance for the help.

    Code:
    #include <stdio.h>
    
    int main( ) {
        char YN_Continue;
           float radius, circumference, area, pi=3.14159;
    
        YN_Continue = 'Y';
        while (YN_Continue == 'Y'){       
            printf(" Enter the radius of a circle: ");
            scanf("%f", &radius);
    
            circumference = 2 * pi * radius;
            area = pi * radius * radius;
    
            printf(" circumference of circle with radius: %.2f is: %.2f\n", radius, circumference);
            printf(" area of circle with radius: %.2f is: %.2f\n\n", radius, area);
    
            printf(" Would you like to enter another radius? (Type Y/N): \n\n");
            scanf("%c", &YN_Continue);
        }
        
        printf(" GoodBye!");
           return 0;
    }
    and the do-while also didn't work for me

    Code:
    #include <stdio.h>
    
    int main( ) {
    	char YN_Continue;
       	float radius, circumference, area, pi=3.14159;
    
    	do {   	
    		printf("Enter the radius of a circle: ");
    		scanf("%f", &radius);
    
    		circumference = 2 * pi * radius;
    		area = pi * radius * radius;
    
    		printf("circumference of circle with radius: %f is: %f\n", radius, circumference);
    		printf("area of circle with radius: %f is: %f\n", radius, area);
    
    		printf("Would you like to enter another radius? (Type Y/N): ");
    		scanf("%c", &YN_Continue);
    	} while (YN_Continue == 'Y');
    	
    	printf("GoodBye!");
       	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should explain how your programs aren't working.

    Fortunately, I think I know the problem you're having, and the reason for it (and solution) can be found here: FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    2
    Thanks! that did indeed explain it perfectly. greatly appreciate it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-05-2015, 03:29 PM
  2. Replies: 1
    Last Post: 03-28-2015, 08:59 PM
  3. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  4. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  5. Replies: 23
    Last Post: 04-05-2011, 03:40 PM

Tags for this Thread