Ok, first thing... more of an "administrative" detail. When posting source code, it makes it a whole lot easier for everyone to read if you surround the code with "code" tags. That is:
[ code ]
[ /code ]
But leave out the spaces inside the square brackets - otherwise LQ would have interpreted it as though I wanted to add code in my message
I'll post, in code tags, your program from the last message minus the line numbers (and using my indention preferences).
printf ("State your fuel use or way of travel\n");
printf (" 1. Petrol\n 2. Oil\n 3. Coal\n 4. Wood\n 5. Electricity\n 6. Natural gas\n 7. Air Travel ");
scanf ( "%g", &fuel );
printf (" How many units of fuel or distance travelled has been used?\n");
scanf ( "%g", &units );
printf("you are using $%f of kg CO2 by using Petrol\n", (float)fuel * 2.3);
printf("you are using $%f of kg CO2 by using Oil\n", (float)fuel * 2.7);
printf("you are using $%f of kg CO2 by using Coal\n", (float)fuel * 2.4);
printf("you are using $%f of kg CO2 by using Wood\n", (float)fuel * 0.0);
printf("you are using $%f of kg CO2 by using Electricity\n", (float)fuel * 0.4);
printf("you are using $%f of kg CO2 by using Natural Gas\n", (float)fuel * 0.2);
printf("you are using $%f of kg CO2 by using Air Travel\n", (float)fuel * 0.3);
I get the impression this is a homework/class assignment. So I'm not going to give you the answer, but point you in the right direction.
First, understand that your use of scanf() to read in a variable needs a conversion string that matches
the data type of the variable you're trying to store data in. Review you notes/man pages to determine whether %g is an appropriate conversion string for an int data type (man scanf
). The binary format of the data is different for some data types--which is why you need to make sure the conversion string and the data type match.
Second, it's generally good programming practice to never leave a logical "gap" in your code. That means to always
include "else" statements for each "if" and "default" blocks for every "switch". I have a feeling if you placed an error message like this:
printf( "Unexpected value for fuel! (%d)\n", fuel );
in your switch block, it likely would have shown you a symptom of the problem resulting from the scanf()-data type issue I mentioned earlier.