Counter Errors for otherwise working program???

This is a discussion on Counter Errors for otherwise working program??? within the C Programming forums, part of the General Programming Boards category; Problem: The counter for acceptable doesn't work upon entering values that should increase acceptable by 1. Instead it adds to ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    13

    Counter Errors for otherwise working program???

    Problem: The counter for acceptable doesn't work upon entering values that should increase acceptable by 1. Instead it adds to unacceptable, it may have something to do with the {} braces however im not sure. Any Ideas? this is the only current problem, thanks pplz !



    Code:
    #include <stdio.h>
    
    
    int main(){
    
    	int subtotal_c = 0, subtotal_h = 0, subtotal_n = 0, subtotal_nm = 0;
    	int gas, distance, gas_emmissions, years;
    	int acceptable_carbon = 0, unacceptable_carbon = 0;
    	int acceptable_hydro = 0, unacceptable_hydro = 0;
    	int acceptable_nitro = 0, unacceptable_nitro = 0;
    	int acceptable_nonmeth = 0, unacceptable_nonmeth = 0;
    	double carbon_a = 2.13;
    	double carbon_b = 2.63;
    	double hydro_a = 0.19;
    	double hydro_b = 0.24;
    	double nitro_a = 0.25;
    	double nitro_b = 0.31;
    	double nonmeth_a = 0.16;
    	double nonmeth_b = 0.19;
    
    
    
    
    
    		printf( "**************** Welcome To The Emmission Calculation Program ****************\n\n" );
    
    		printf( "(1)	Carbon monoxide\n");
    		printf( "(2)	Hydrocarbons\n");
    		printf( "(3)	Nitrogen oxides\n");
    	    printf( "(4)	Non methane hydrocarbons\n");
    
    
    		
    		printf( "\nPlease enter the number corresponding to the gas you require (0 to exit): ");
    	    scanf( "%d",&gas);
    
    		
    		while (gas != 0){
    
    			if ( gas >= 5 ){
    
    			printf("Error please try again\n");
    			break;}
    	
    			if ( gas < 0 ){
    			printf("Error please try again\n");
    			break;}
    
    		printf( "\nPlease enter the distance from the source: ");
    		scanf( "%d",&distance);
    
    	      printf( "\nPlease enter number of years: " );
    		scanf( "%d", &years);
    
    		printf( "\nPlease enter the level of gas emmissions: ");
    		scanf( "%f",&gas_emmissions);
    
    
    		if (gas == 1){
    			if (( distance <= 80000 ) || ( years < 5 ))
    				if (gas_emmissions < carbon_a)
    					acceptable_carbon++;
    				else
    					unacceptable_carbon++;
    
    			if (( distance > 80000 ) || ( years > 5 ))
    				if (gas_emmissions < carbon_b)
    					acceptable_carbon++;
    				else
    					unacceptable_carbon++;
    
    			}
    
    		if (gas == 2){
    			if (( distance <= 80000 ) || ( years < 5 ))
    				if (gas_emmissions < hydro_a)
    					acceptable_hydro++;
    				else
    					unacceptable_hydro++;
    
    			if (( distance > 80000 ) || ( years > 5 ))
    				if (gas_emmissions < hydro_b)
    					acceptable_hydro++;
    				else
    					unacceptable_hydro++;
    			}
    
    		if (gas == 3){
    			if (( distance <= 80000 ) || ( years < 5 ))
    				if (gas_emmissions < nitro_a)
    					acceptable_nitro++;
    				else
    					unacceptable_nitro++;
    
    
    			if (( distance > 80000 ) || ( years > 5 ))
    				if (gas_emmissions < nitro_b)
    					acceptable_nitro++;
    				else
    					unacceptable_nitro++;
    			}
    
    		if (gas == 4){
    			if (( distance <= 80000 ) || ( years < 5 ))
    				if (gas_emmissions < nonmeth_a)
    					acceptable_nonmeth++;
    				else
    					unacceptable_nonmeth++;
    
    
    			if (( distance > 80000 ) || ( years > 5 ))
    				if (gas_emmissions < nonmeth_b)
    					acceptable_nonmeth++;
    				else
    					unacceptable_nonmeth++;
    			}
    
    		subtotal_c = acceptable_carbon + unacceptable_carbon;
    		subtotal_h = acceptable_hydro + unacceptable_hydro;
    		subtotal_n = acceptable_nitro + unacceptable_nitro;
    		subtotal_nm = acceptable_nonmeth + unacceptable_nonmeth;
    
    		printf( "\n***********************   These are the results:   ***********************\n\n");
    		printf( "\t\t\t\tAcceptable   Unacceptable   Sub Totals");
    		printf( "\nCarbon monoxide\t\t\t\t%d\t%d\t\t%d\n",acceptable_carbon, unacceptable_carbon, subtotal_c);
    		printf( "Hydrocarbons\t\t\t\t%d\t%d\t\t%d\n",acceptable_hydro, unacceptable_hydro, subtotal_h);
    		printf( "Nitrogen oxides\t\t\t\t%d\t%d\t\t%d\n",acceptable_nitro, unacceptable_nitro, subtotal_n);
    		printf( "Non-methane hydrocarbons\t\t%d\t%d\t\t%d\n\n",acceptable_nonmeth, unacceptable_nonmeth, subtotal_nm);
    
    		printf( "\nPlease enter the number corresponding to the gas you require (0 to exit): ");
    	    scanf( "%d",&gas);
    	}
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > int gas, distance, gas_emmissions, years;
    Make gas_emmissions a double:
    double gas_emmissions;

    > scanf( "%f",&gas_emmissions);
    And change this to:
    scanf( "%lf",&gas_emmissions);

    Or you could make gas_emmissions a float and leave the scanf() unchanged.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Here's a suggestion: Whenever your calculations don't give the results you expect, put in a few printf() statements to make sure they are working on the data you expected:

    So, after all data has been entered by the user, put the following
    Code:
        printf("You entered: distance      = %d\n", distance);
        printf("             years         = %d\n", years);
        printf("             gas_emissions = %f\n", gas_emmissions);
    That is, a print statement for each data item, with the same format specifiers that scanf() had for each.

    Swoopy did the debug for you this time, but maybe next time you can try it. (What if Swoopy is not around?)

    printf() is your friend.

    Dave

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    13

    Question unfortunalty another problem

    Thanks for your help swoppy.
    (i know what u r saying dave, but that is not my biggest concern atm, i can address that after i get the program to work properly)
    although another problem has now occured/noticed. The counter will not to add unacceptable, adds only to acceptable every time, no matter what the condition.
    Will chaning all the doubles to floats make a difference...

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    72

    Re: unfortunalty another problem

    Originally posted by jereland
    Thanks for your help swoppy.
    (i know what u r saying dave, but that is not my biggest concern atm, i can address that after i get the program to work properly)
    Follow Dave's suggestion. It will help your program to work properly.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    536

    Re: unfortunalty another problem

    Originally posted by jereland
    Thanks for your help swoppy.
    (i know what u r saying dave, but that is not my biggest concern atm, i can address that after i get the program to work properly)
    although another problem has now occured/noticed. The counter will not to add unacceptable, adds only to acceptable every time, no matter what the condition.
    Will chaning all the doubles to floats make a difference...
    I claim that "making it work" might actually be quicker if you put in a few printf() statements near your inputs, calculations, and outputs. (Quicker than posting an appeal for help and waiting for a helpful response.)

    If you think changing doubles to floats will make a difference, try it (but I'm not sure why would think this might solve your problem).

    Asking for help understanding your problem is one thing; getting someone else to debug your homework is another.

    Dave

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    13

    thanks

    thanks for your reply dave.. spent some time on it and have successfully got the expected results. just got so frustrated before..
    cheers guys U rock!

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Onward and upward!

    And never forget:

    printf() is your friend.

    Best regards,

    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program giving errors
    By andy bee in forum C Programming
    Replies: 5
    Last Post: 08-11-2010, 10:38 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Unknown Errors in simple program
    By neandrake in forum C++ Programming
    Replies: 16
    Last Post: 04-06-2004, 02:57 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. Program logic not working..
    By ronkane in forum C++ Programming
    Replies: 2
    Last Post: 01-22-2002, 07:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21