Thread: Eliminate title

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    127

    Eliminate title

    How to eliminate the title in do-while loop for the first loop?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    // Function main begins program execution
    int main(void)
    {
    char date[11], name[50], *line, performance[10], rate[3], yesno;
    int a;
        
        // Title
        printf("Payslip system\n\n");
        // Prompt and read the date
        printf("Date: ");
        fgets(date, sizeof(date), stdin);
        fflush(stdin);
    
        /*Prompt user whether continue or stop creating Payslip */
        // Start do-while loop 
        do
        {
        // Title
        printf("Payslip system\n\n");
        
        // Prompt and read the user's name
        printf("Name: ");
        fgets(name, sizeof(name), stdin);
        
        // Remove newline
        line = strchr(name,'\n');
        *line = '\0';
    
        // Prompt and read the user's sales amount of the day
        printf("Sales Amount: ");
        scanf("%d", &a);
        
        if (a >= 2000)
        {
            strcpy (performance,"Excellent");
            strcpy (rate,"3%");
        }
        else if (a >= 1000)
        {
            strcpy (performance,"Good");
            strcpy (rate,"2%");
        }
        else
        {
            strcpy (performance,"Fair");
            strcpy (rate,"1%");
        }
    
        // Display payslip
        printf("%s\n", name);
        printf("%s\n", date);
        printf("%s\n", performance);
        printf("%s\n", rate);
        
        // Prompt and read the user whether continue or end
        printf("Continue (Y/N)? ");
        scanf(" %c",&yesno);
        fflush(stdin);
        printf("\n");
        } while (yesno == 'Y' || yesno == 'y');
        // End do-while loop
    
        // Pause the system and prompt the user to terminate the program
        system ("pause");
        }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Move or remove the line from the loop?

    And please stop using fflush(stdin), it's undefined behavior.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    What should we use ?

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    1)How to remove this?
    Eliminate title-3-jpg

    2)Teach me the other solution better than fflush(stdin). Thanks.

  5. #5

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    1)How to remove this?
    Use that fatty organ in your head and apply what was told to you in my original reply.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Quote Originally Posted by rags_to_riches View Post
    Use that fatty organ in your head and apply what was told to you in my original reply.
    If i remove the code in line 12, the title will not display from beginning.

    If i remove the code in line 23, the title will not display after the loop.

    So, I need a solution that can display the title from beginning but will not repeat again when goes to read user's name section. Furthermore, the title will display after the loop.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    One quick and dirty solution:

    Code:
      // put this at the top
      int firstLoop = 1;
    
      // put this in the "do-while" loop
      if (firstLoop)
          firstLoop = 0;
      else
          printf("heading\n");

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Thank you very much. Very useful solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Eliminate __builtin__* from preprocessed files
    By moxjet in forum Tech Board
    Replies: 0
    Last Post: 07-26-2010, 02:29 AM
  2. new to C...need to eliminate whitespace
    By cakestler in forum C Programming
    Replies: 7
    Last Post: 02-02-2009, 12:41 PM
  3. How to eliminate irrelevant conditionals?
    By cdave in forum C Programming
    Replies: 9
    Last Post: 12-10-2005, 04:39 PM
  4. How to eliminate extra whitespaces?
    By dat in forum C Programming
    Replies: 1
    Last Post: 10-24-2002, 06:42 PM