Find the answer to your Linux question:
Results 1 to 2 of 2
Hi all, The following program successfully applies the do while loop when the user enters 'Y' or 'y' when run in Windows after being compiled with Dev C++. That is ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Posts
    11

    Do While Loop Failing when compiled w/ GCC but not on Windows Dev-C++


    Hi all,

    The following program successfully applies the do while loop when the user enters 'Y' or 'y' when run in Windows after being compiled with Dev C++. That is what my Intro to Algorithms class uses. But when I take the same code and compile it on linux with gcc, it doesn't respond properly to y or Y, and ends the program. Here's the code:
    Code:
    #include  <stdio.h> // reqd for printf(), scanf() and fprintf()
    #include <string.h>
    #define addr(var) &var
    #define REPORTHEADINGS "     Name                   Pay Rate     Hours     Gross     Tax     Net\n"
    #define FORMAT         "     %-23s%8.2f%10.2f%10.2f%8.2f%10.2f\n"
    #define TAXRATE 0.15
    
    	
      int main(void)
        {
          char lastName[15+1];
          char firstName[13+1];
          char fullName[15+13+2+1];
          float payrate,hours,gross;
          float tax;
          float netpay;
          char answer;
    	
          FILE * reportFile;
           
          reportFile = fopen("/home/erin/Documents/IntroAlgorithms/file.txt","wt");               
          fprintf(reportFile,REPORTHEADINGS);
           printf(REPORTHEADINGS); 
    	    
          do {
    		printf("Enter employee's last name: ");  
    		scanf("%s",lastName);
    		printf("Enter employee's first name: "); 
    		scanf("%s",firstName);
    		printf("Enter hourly pay rate: ");
    		scanf("%f",addr(payrate));
    		printf("Enter hours worked this pay period: ");
    		scanf("%f",addr(hours));
    		
    		if (hours <= 40)
    	           gross = hours * payrate;
    	        else
                       gross = 40 * payrate + (hours-40)*1.5*payrate;   
                       tax = gross * TAXRATE;
                       netpay = gross-tax;    
              
    		strcpy(fullName,lastName);
    		strcat(fullName,", ");
    		strcat(fullName,firstName);
    		printf(FORMAT,fullName,payrate,hours,gross,tax,netpay);
    	      
    		fprintf(reportFile,FORMAT,fullName,payrate,hours,gross,tax,netpay);
    		fflush(stdin); // clear keyboard buffer before answer input
    		printf("Another employee(Y/N) ");
    		scanf("%c",addr(answer));
    		} 
    	while (answer == 'Y' || answer == 'y');
    		
    	    
          fclose(reportFile);
          fflush(stdin);
          getchar();
          return 0;
        }
    Any advice would be awesome. Thanks!

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    636
    I see two things that are not "standard C" code.
    • The "t" in the mode within the fopen call
    • fflush flushes output buffers and not input buffers.

    According to the C Standard:

    int fflush(FILE* stream);

    Flushes stream stream and returns zero on success or EOF on error.

    Effect undefined for input stream. fflush(NULL) flushes all output streams.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •