# Counter Errors for otherwise working program???

Printable View

• 03-22-2004
jereland
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; }```
• 03-22-2004
swoopy
> 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.
• 03-22-2004
Dave Evans
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
• 03-22-2004
jereland
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...
• 03-22-2004
major_blagger
Re: unfortunalty another problem
Quote:

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.
• 03-22-2004
Dave Evans
Re: unfortunalty another problem
Quote:

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
• 03-22-2004
jereland
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!
• 03-22-2004
Dave Evans
Onward and upward!

And never forget:

printf() is your friend.

Best regards,

Dave