Home > other >  Show this printf just one time not again
Show this printf just one time not again

Time:03-10

I want to show this message one time : Which answer do you wanna see? 1-20 Then every time want to show this message : What you want to see next? using simple library and code like look or if,else. if anyone can, please help me.. i am just beginner.

#include <stdio.h>
int main() {
    while (1) {
        int n;
        printf ("\n\tWhich answer do you wanna see? 1-20 : ");
        scanf ("%d", &n);
        switch (n)  {
        case 1:
            printf ("\n\n\tQ1: Write a C Program to display your name 5 Times.\n\t---------------------------------------------------");
            printf ("\n\n\tAnswer to the question No: 1\n\t--------------------------");
            printf ("\n\tMd. Mehedi hasan (Siam)");
            printf ("\n\tMd. Mehedi hasan (Siam)");
            printf ("\n\tMd. Mehedi hasan (Siam)");
            printf ("\n\tMd. Mehedi hasan (Siam)");
            printf ("\n\tMd. Mehedi hasan (Siam)\n\n");
            break;
        case 2:
            printf ("\n\n\tQ2: Write a C Program to display your name 5 Times using do while loop.\n\t---------------------------------------------------");
            printf ("\n\n\tAnswer to the question No: 2\n\t--------------------------");

            int a;
            a = 1;
            do {
                printf ("\n\tMd. Mehedi hasan (Siam)");
                a  ;
            } while (a <= 5);
            break;
        case 3:
            printf ("\n\n\tQ3: Write a C Program to display your name 5 Times using do while loop and Display the Stop Value. \n\t---------------------------------------------------");
            printf ("\n\n\tAnswer to the question No: 3\n\t--------------------------");
            int b;
            b = 1;
            do {
                printf ("\n\tMd. Mehedi hasan (Siam)");
                b  ;
            } while (b <= 5);
            printf ("\n\n\tThe Loop Stopping Value Is = %d\n\n", b);
            break;
        case 0:
            exit (0);
            break;

        default:
            printf ("Invalid Choice");

        }
    }

    return 0;
}

CodePudding user response:

You need to use a variable to know whether the loop is in it's first iteration. Something like:

int isFirstLoop = 1;
while (1)
{
    if (isFirstLoop)
    {
       printf("\n\tWhich answer do you wanna see? 1-20 : ");
       isFirstLoop = 0;
    }
    else
    {
       printf("\n\tWhat do you want to see next? 1-20 : ");
    }
 // Rest of the code here....
}
  •  Tags:  
  • c
  • Related