Thread: Y/N while loop

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    13

    Lightbulb Y/N while loop

    New to C programming, can't get a repeat loop to work (GFX is a graphics library, currently it does not loop, it only prints the question)
    Code:
    char ans, Y, y, N, n
    printf("\nWould you like to go again(Y/N)?: \n");
                scanf("%c,", &ans);
                getchar();
                GFX_UpdateDisplay();
                    while (ans == 'y' || ans == 'Y')
                    {
                     int user_input;
                    printf("Please enter coordinate x: ");
                    scanf("%d,", &user_input);
                    getchar();
                    int user_input_2;
                    printf("Please enter coordinate y: ");
                    scanf("%d,", &user_input_2);
                    getchar();
                    stickman(user_input,user_input_2);
                    GFX_UpdateDisplay();
                    float user_input_3;
                    printf("Please specify velocity of projectile: ");
                    scanf("%f,",&user_input_3);
                    getchar();
                    float user_input_4;
                    printf("Please enter the angle: ");
                    scanf("%f,",&user_input_4);
                    getchar();
                    projectile(user_input,user_input_2,user_input_3,user_input_4);                GFX_UpdateDisplay();
                    printf("\nWould you like to go again(Y/N)?: \n");
                    scanf("%c",&ans);
                    getchar();
                    GFX_UpdateDisplay();
                    }

  2. #2
    Hadi
    Join Date
    Jan 2015
    Posts
    14
    just do this:

    Code:
    char ans, Y, y, N, n
    
    do
                    {
    printf("\nWould you like to go again(Y/N)?: \n");
                scanf("%c,", &ans);
                getchar();
                GFX_UpdateDisplay();
                     int user_input;
                    printf("Please enter coordinate x: ");
                    scanf("%d,", &user_input);
                    getchar();
                    int user_input_2;
                    printf("Please enter coordinate y: ");
                    scanf("%d,", &user_input_2);
                    getchar();
                    stickman(user_input,user_input_2);
                    GFX_UpdateDisplay();
                    float user_input_3;
                    printf("Please specify velocity of projectile: ");
                    scanf("%f,",&user_input_3);
                    getchar();
                    float user_input_4;
                    printf("Please enter the angle: ");
                    scanf("%f,",&user_input_4);
                    getchar();
                    projectile(user_input,user_input_2,user_input_3,user_input_4);                GFX_UpdateDisplay();
                    printf("\nWould you like to go again(Y/N)?: \n");
                    scanf("%c",&ans);
                    getchar();
                    GFX_UpdateDisplay();
                    } while (ans == 'y' || ans == 'Y')
    [/QUOTE]

  3. #3
    Registered User
    Join Date
    Apr 2017
    Posts
    13
    thanks
    Do while loops not work like how I was using them in C?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    The main problem would seem to be your use of %c in scanf.

    Almost all other formats skip leading whitespace (spaces, tabs, newlines) before conversion.

    %c takes the next available character, which more often than not is the newline left behind by the previous conversion.

    If you want to get the next non-whitespace character, then do
    scanf(" %c",&ans); // note: leading space in format.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-22-2016, 09:08 AM
  2. While loop in insertNode function is an infinite loop
    By blongnec in forum C Programming
    Replies: 8
    Last Post: 03-19-2016, 09:57 PM
  3. Replies: 1
    Last Post: 03-28-2015, 08:59 PM
  4. 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
  5. Replies: 23
    Last Post: 04-05-2011, 03:40 PM

Tags for this Thread