Thread: C fuel program

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    2

    C fuel program

    Right I wrote this code and i need to modify it now to account for other problems so.. the code is as follows.
    Code:
          #include <stdio.h>
          #include <ctype.h>
          
      
          int main(void)
      
          {
    
          int fuel;
      
          int c;
          
          
          printf ("Petrol 2.3kg CO2 / unit\nOil 2.3kg CO2 / unit\nCoal 2.3kg CO2 / unit\nWood 2.3kg CO2 / unit\n");
          printf ("Electricity 2.3kg CO2 / unit \nNatural gas 2.3kg CO2 / unit \nAir travel 2.3kg CO2 / unit \n\n\n");
          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 ");
                
      
      
          do
      
          {
      
          scanf ( "%d", &fuel);
      
          getchar();
      
    
          switch( fuel)
      
          {
      
          case 1:
      
          
          printf("you are using %f of kg CO2 by using Petrol\n", (float)fuel * 2.3);
          
          break;
      
          case 2:
      
          printf("you are using %f of kg CO2 by using Oil\n", (float)fuel * 2.7);
          
          break;
      
          case 3:
    
          printf("you are using %f of kg CO2 by using Coal\n", (float)fuel * 2.4);
        
          break;
      
          case 4:
      
          printf("you are using %f of kg CO2 by using Wood\n", (float)fuel * 0.0);
          
          break;
      
          case 5:
      
          printf("you are using %f of kg CO2 by using Electricity\n", (float)fuel * 0.4);
          
          break;
      
          case 6:
      
          printf("you are using %f of kg CO2 by using Natural Gas\n", (float)fuel * 0.2);
                
          break;
      
          case 7:
      
          printf("you are using %f of kg CO2 by using Air Travel\n", (float)fuel * 0.3);
    
          break;
          }
    
          printf("Would you like to do it again?(Y/N): ");
    
      
          c = toupper(getchar());
      
          if(c == 'Y')
    
          {
    
          printf("How much fuel is being used?: ");
    
          }
      
      }
    
          while( c != 'N');
    
          
    
          getchar();
    
          return 0;
    
    }
    With this i need to make the code read and summarise a file containing this table;
    Gas 10000
    Electricity 3000
    Flight 8000
    etc etc..
    The program has to also analyse a file containing any number of lines and not in any order. The first column should be a single word describing the fuel/activity. i have to have the following declarations in force: double amount;
    char fuel[20],description [40]
    one line of the code can be read using
    fscanf(fptr," %19s %lf %39[^\n]",fuel,&amount,description);
    I have to give the total CO2 emision in kg together with the equivalent number of CO2 ”rations” (where a CO2 ration is 2500 kg)
    I need to provide a breakdown of the emissions, listing the
    emission corresponding to each line in the file
    and the total for each kind of emission (e.g.
    Car, Flight etc etc..) I should contain and use a function declared as:
    double co2(char[] fuel, double amount)
    My program will need to make use ofthe strncmp function.


    I know this may seem like im asking for work to be done but i only need steps in right direction as ive already written code and not asking all to be done by you just pointers and explaining how i can do this.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    First, a warning - whatever block of code you're writing, make sure it works before going on to some other bit of code. Nothing will slap you upside the face like a frozen trout faster, than having a lot of code you've spent hours working on, and *now* after all that work, nothing will compile or run right.

    For the string functions, just be sure to include <string.h>, right under <stdio.h>. There are zillions of posts right on the forum showing how to use FILE pointers - use one to study up on that.

    Take it step by step - make sure you're reading all the right data, before you start writing up your code to compute something from that data. If you used a bunch of #defines PetrolCO2 0.3, type lines of code, your code would be much clearer. (Putting them just in the CO2 function would help as well.

    Personally, I wouldn't trust fscanf() very far at all, but if it works in this instance, OK. Make no assumptions about it's "robust" character though, because it doesn't have any.

    If you can indent 2 to 5 spaces where indentation is called for in your code, and delete the double spacing throughout, it will assist you and all others trying to review it.

    If you need to use a function named CO2(), shouldn't it be in there? I don't understand what your question is about coding up that function. Did you ask a question about that?

    Barry, the more detailed your questions, the more we can help with answers. Here, the code doesn't have even a full outline of the assignment, and I don't know why not. You need a CO2 function to do the CO2 calculations in; you need a FILE pointer to read data from a file, but there's no code for either. You have work to do. The examples to use both these things, are all over this forum. Study a few, and get that added. Let us know if you have some specific problem making that happen.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    2
    Thanks im just working on what you've told me to do and ill come back with any problems i encounter. Your help is much apprieciated.

  4. #4
    Registered User
    Join Date
    May 2007
    Location
    Australia, Melbourne.
    Posts
    5
    BarryKamp,

    There are few things i need to point out to you,
    1. You are doing fine as far as solution is concerned.
    2. You need to learn to write code, with the intent of readibility.
    3. Each function you use from the std c lib's, read what it does and what you want to do.
    4. Start with plan NOT with a code. A plan of attack is important.

    Hope it should give you the right direction.
    good luck.

  5. #5
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    Just some tips on formatting your code:

    After a statement don't put a blank line.

    e.g

    Code:
    if (i > 2)
    
    {
            do_this();
    }

    put it like this:

    Code:
    if (i > 2)
    {
           do_this();
    }
    
    or
    
    if (i > 2) {
           do_this();
    }

    and a switch should be set up similar to:

    Code:
    switch(opt)
    {
                case 'y':
                         yes = 1;
                         break;
     
                case 'e':
                         delete = 1;
                         break;
    
                default:
                         do_something();
    }
    
    or
    
    switch(opt) {
                case 'y':
                         yes = 1;
                         break;
     
                case 'e':
                        delete = 1;
                        break;
    
                default:
                        do_something();
    }
    makes code easier to read
    other than that, good job!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM