Thread: How to optimise this c program?

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    51

    Unhappy How to optimise this c program?

    Code:
    #include <stdio.h>
    void main(void)
    {
    
    /* ================================== Local Definitions ================================== */
    	/*Declare a variable for Sales Agent's code number*/
    	int num;
    	/*Declare variables for sales amount for week 1,2,3,4 , total sales
    	and the commission earned*/
    	double week1, week2, week3, week4, sum, earn;
    
    start:
    /* Statements */
    	printf("Please key in Sales Agent's code number>");/* Output statement*/
    	scanf("%d", &num);/* Input statement*/
    	fflush(stdin);
    	{
    	  if(num>=1000&&num<=9999)/* Define maximum 4 digits for Sales Agent's code number */
    		goto next;
         
    	  else {
            printf("Invalid code number! Please key in 4 digits code number.\n");
    	  goto start;
    	  }
    	}
    next:
    	printf("\nPlease key in sales amount for Week 1 (RM)> ");
    	/* Output statement*/
    	scanf("%lf", &week1);
    	/* Input statement*/	
    
    	fflush(stdin); /* flush the "enter" or other unrelated characters in the memory */
    
    	printf("\nPlease key in sales amount for Week 2 (RM)> ");
    	/* Output statement2*/
    	scanf("%lf", &week2);
    	/* Input statement*/
    
    	fflush(stdin);
    	
    	printf("\nPlease key in sales amount for Week 3 (RM)> ");/* Output statement*/
    	scanf("%lf", &week3);	/* Input statement*/
    
    	fflush(stdin);
    
    	printf("\nPlease key in sales amount for Week 4 (RM)> ");	/* Output statement*/
    	scanf("%lf", &week4);	/* Input statement*/
    
    	sum= week1 + week2 + week3 + week4;/* Formula for total sales */
    
    	printf("\nTotal sales in a month: RM %.2lf", sum);/* Output statement*/
    
    	earn= sum *.2; /* Formula for commission earned */
    
    	printf("\n\nCommission earned: RM %.2lf\n\n", earn);/* Output statement*/
    
    	printf("Press Any Key to continue");  
        getch();  /* Waiting for any key press to exit the program */
    }

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    *sniff sniff* smells like homework.

    Start by getting rid of the gotos. They aren't needed - find something else to replace them. Then get rid of the variables you don't need. Like this (it's ridiculous):

    Code:
    earn= sum *.2; /* Formula for commission earned */
    
    	printf("\n\nCommission earned: RM %.2lf\n\n", earn);/* Output statement*/
    try this instead:
    Code:
    printf("\n\nCommission earned: RM %.2lf\n\n", (sum*.2));/* Output statement*/
    After that, show us what you've done/tried...
    Away.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    void main(void)
    *BZZZZT* WRONG!!!
    You should always use int main( void ).

    Code:
    fflush(stdin);
    Nope. Check the FAQ.

    Code:
    goto next;
    Eww. Don't use gotos.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    51
    :XSquared

    1)After using int main( void ) and removed all the fflush(stdin) in my program ... it introduce new problems... when the user input "."(a dot or other alphabets) for variable num causes the program to loop at :start

    2) How to use other than gotos (using too much batch file =D)?

    3) Is that other way than using the code below?
    Code:
    getch();  /* Waiting for any key press to exit the program */
    4)Any code that let the program clear the screen without include new library file, show me both ways(w/ w/o new library file)?

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    51
    Code:
    #include <stdio.h>
    int main(void)
    {
    
    /* ================================== Local Definitions ================================== */
        /*Declare a variable for Sales Agent's code number*/
        int num, x;
        /*Declare variables for sales amount for week 1,2,3,4 , total sales
        and the commission earned*/
        double sum, sales;
    
    	/* Statements */
    	printf("Please key in Sales Agent's code number>");/* Output statement*/
        scanf("%d", &num);/* Input statement*/
    
    	while( !(num >= 1000 && num <= 9999) )/*if the condition is not true then print the error message*/
    	{
    		printf("\aInvalid code number! Please key in 4 digits code number.\n");
    		printf("Please key in Sales Agent's code number>");
    		scanf("%d", &num);
    	}
    
    	for (x=0; x<4; x++){ /*Generate 4 weeks sales input*/
    		printf("\nPlease key in sales amount for Week %d (RM)> ", x+1);
    		scanf("%lf", &sales);
    		sum += sales;}
    	
        printf("\nTotal sales in a month: RM %.2lf", sum);/* Output statement*/
    
        printf("\n\nCommission earned: RM %.2lf\n\n", sum*.2);/* Output statement*/
    
        printf("Press Any Key to continue"); 
        getch();  /* Waiting for Any Key press to exit the program */
    }
    Here my lastest modification.
    Yet the output seems to have little problem....
    I get a huge negative number for Total sales in a month and Comission earned

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    51
    thanks guys!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM