简体   繁体   中英

How to loop a whole codeblock in C?

So basically after the calculation the program prompts the user if they want to quit the program and the user inputs a character ('y' or 'n') and if the user puts a number or letter that is not 'y' or 'n' then the program will keep prompting the user until they input one of the characters.

The issue I'm running into is that the program will keep looping and prompting the user even if 'y' or 'n' is inputted. When I try fflush(stdin) it still doesn't work

I want to know how to loop the statement again if the user does not input one of the options and when they do input it properly, how to get the code inside the "do while" loop to repeat. Preferably without having to copy and paste the whole bloc again.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>


int main()
{

    float x, t, term = 1 , sum = 1;
    int i;
    char d;

    printf("This program will compute the value of cos x, where x is user input\n\n");

    do {
        printf("Please input the value of x: ");
        while (scanf("%f", &x) != 1)
        {
            fflush(stdin);
            printf("Please input the value of x: ");
            scanf("%f", &x);
        }

        fflush(stdin);

        printf("\nPlease input the number of terms: ");
        while (scanf("%f", &t) != 1)
        {
            fflush(stdin);
            printf("\nPlease input the number of terms: ");
            scanf("%f", &t);
        }

        fflush(stdin);

        for (i=1; i<t+1; i++)
        {

            term =  -term *((x*x)/((2*i)*(2*i-1)));
            sum = sum+term;


        }

        printf("\nThe value of the series is %f", sum);
        printf("\n****************************************");
        printf("\nDo you wish to quit? (y/n): ");
        scanf("%c", &d);

        while (d != 'y' || d != 'n')
        {
            printf("\n****************************************");
            printf("\nDo you wish to quit? (y/n): ");
            scanf("%c", &d);
        }

    } while (d == 'n');

    if (d == 'y')
    {
        printf("terminating program");
        exit(0);
    }
    return (0);
}

scanf() will not throw away the newline character '\\n' in the input buffer unless your format string is set to discard it. In your code, after entering input for your floats and pressing Enter, the newline is still in the buffer. So for the code that prompts Y\\N, use this format string to ignore the newline

scanf(" %c",&d);

You can remove the fflush() calls if you do that. In your case, it looks like your loop conditionals are wrong though.

This line

while (d != 'y' || d != 'n')

is wrong.

Think of it like this:

The loop runs if d is NOT 'y' OR d is NOT 'n'

Now imagine you put in 'y'

d is 'y' . The loop runs if d is NOT 'y' OR d is NOT 'n' . Is d != 'y' ? No. Is d != 'n' ? Yes. Therefore the loop must run.

You need to use &&

while (d != 'y' && d != 'n')

Also, scanf doesn't throw away the newline so add a space for all your scanf s.

scanf("%c", &d); //change to scanf(" %c", &d);

Look in this part-

while (d != 'y' || d != 'n')
{
    printf("\n****************************************");
       printf("\nDo you wish to quit? (y/n): ");
            scanf("%c", &d);
}

  } while (d == 'n');

you are using while twice, i think you will wish to have a single while condition over here.. also if you are terminating while , then be sure there is a do involved.

Here is code which I think is right since you have many problems so i just have changed a lot :

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>


int main()
{

    float x, t, term = 1 , sum = 1;
    int i;
    char d;

    printf("This program will compute the value of cos x, where x is user input\n\n");

    do {
        printf("Please input the value of x: ");
        while (scanf("%f", &x) != 1)
        {
            fflush(stdin);
            printf("Please input the value of x: ");//delete the repeat scanf
        }

        fflush(stdin);

        printf("\nPlease input the number of terms: ");
        while (scanf("%f", &t) != 1)
        {
            fflush(stdin);
            printf("\nPlease input the number of terms: ");
        }

        fflush(stdin);
        sum=0;//no initalization 
        for (i=1; i<t+1; i++)
        {

            term =  -term *((x*x)/((2*i)*(2*i-1)));
            sum = sum+term;


        }

        printf("\nThe value of the series is %f", sum);
        printf("\n****************************************");
        printf("\nDo you wish to quit? (y/n): ");
        scanf("%c", &d);

        while ((d != 'y' )&& (d != 'n'))//this logical expression is the right way
        {

            scanf("%c", &d);
            fflush(stdin);
            printf("\n****************************************");//I change the pos of print to avoid double printing
            printf("\nDo you wish to quit? (y/n): ");
        }

    } while (d == 'n');

    if (d == 'y')
    {
        printf("terminating program");
        exit(0);
    }
    return (0);
}

ps:for your calculate part of cos I'm not sure about the correctness while runing:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM