Need help with project

This is a discussion on Need help with project within the C Programming forums, part of the General Programming Boards category; Hi guys, im an amateur coder and ive got 4 C-based questions to do for a project and im using ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    8

    Need help with project

    Hi guys, im an amateur coder and ive got 4 C-based questions to do for a project and im using Cygwin bash shell with gVim easy editor.

    The first question is to write a program to calculate Co2 emission when the user enters a fuel/activity (from a list of 7), and then a quantity in the specific units. I've got a table of ratio for multiplying unit of fuel -> kg of Co2, for example petrol creates 2.3 kg CO2 per litre. I've done this fine and it all works.

    However, for the 2nd part, i have to use the same data to change the program so that the total CO2 emission so far is displayed at all times, using a switch setup. I'm not entirely sure how to do this, and my code so far is below:

    Code:
    #include <stdio.h>
    #include <math.h>
    int main(void)
    {
    double amt[7];
    int a,type;
    amt[0]=0.0,amt[1]=0.0,amt[2]=0.0,amt[3]=0.0,amt[4]=0.0,amt[5]=0.0,amt[6]=0.0;
    
    
    printf("==========\nCurrent Co2 Emission:\n");
    printf("Total Co2 Emission is: %f", amt[0]+amt[1]+amt[2]+amt[3]+amt[4]+amt[5]+amt[6]);
    printf("\nMenu:");
    printf("\n1.Petrol (litres)\n2.Oil (litres)\n3.Coal (kg)\n4.Wood (kg)\n5.Electricity (kWhr)\n6.Natural Gas (kWhr)\n7.Air Travel (miles)\n\n");
    
    switch (type) {
    case '1': printf("How much Petrol? (litres)");
                     scanf(" %d",&a);
                     amt[0] += a * 2.3;
    
    
    }
    getchar();
    return 0;
    }
    Basically this code asks the user what fuel to use, etc. However it doesnt work so far, it simply closes once I type in any number on the fuel type menu and press enter.
    I get 1 warning: 'type' might be used uninitialized in this function.

    I'm not sure where to go next. I need a 'quit' option on the menu, and i need the function to loop, so that the user selects a fuel, then an amount, and after this, it is added to the 'total emission' and the user can type more, until he presses quit.

    Any help would be appreciated
    Thanks
    Noori

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    8
    Here is the code from question 1 for an idea of what im doing:

    Code:
    #include <stdio.h>
    #include <math.h>
    int main(void)
    {
    double type,amt1,amt2,amt3,amt4,amt5,amt6,amt7;
    printf("List of Fuels/Activities:\n1.Petrol (litres)\n2.Oil (litres)\n3.Coal (kg)\n4.Wood (kg)\n5.Electricity (kWhr)\n6.Natural Gas (kWhr)\n7.Air Travel (miles)\n\n");
    printf("Please enter the number corresponding to the fuel/activity you wish to view:");
    scanf(" &#37;lf",&type);
    
    if (type==1) {
    printf("How many litres of petrol?: ");
    scanf(" %lf",&amt1);
    amt1 = amt1 * 2.3;
    printf ("This creates %lf kg of Carbon Dioxide", amt1);
    getchar();
    }
    
    if (type==2) {
    printf("How many litres of oil?: ");
    scanf(" %lf",&amt2);
    amt2 = amt2 * 2.7;
    printf ("This creates %lf kg of Carbon Dioxide", amt2);
    getchar();
    }
    
    if (type==3) {
    printf("How many kg of coal?: ");
    scanf(" %lf",&amt3);
    amt3 = amt3 * 2.4;
    printf ("This creates %lf kg of Carbon Dioxide", amt3);
    getchar();
    }
    
    if (type==4) {
    printf("How many kg of wood?: ");
    scanf(" %lf",&amt4);
    printf ("This creates 0 kg of Carbon Dioxide");
    getchar();
    }
    
    if (type==5) {
    printf("How many kWhr of Electricity??: ");
    scanf(" %lf",&amt5);
    amt5 = amt5 * 0.4;
    printf ("This creates %lf kg of Carbon Dioxide", amt5);
    getchar();
    }
    
    if (type==6) {
    printf("How many kWhr of natural gas?: ");
    scanf(" %lf",&amt6);
    amt6 = amt6 * 0.2;
    printf ("This creates %lf kg of Carbon Dioxide", amt6);
    getchar();
    }
    
    if (type==7) {
    printf("How many miles of air travel?: ");
    scanf(" %lf",&amt7);
    amt7 = amt7 * 0.3;
    printf ("This creates %lf kg of Carbon Dioxide", amt7);
    getchar();
    }
    
    printf("\n\n\tpress enter to exit ");
    getchar();
    return 0;
    
    }
    That code works OK, however in the next part i need the emission shown on the screen all the times, and a menu which allows the user to enter various fuels and then the emission calculated. I also need to use arrays.

    Cheers.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You'll need a loop for your menu function.

    Code:
    while(1)  {
       print your menu options all out here at the top - include a choice of 0 to exit.
    
       now add your switch statement to handle the user's input
       add case 0; break 
       so the user can quit the menu (and return to main(), to end the program)
    
    }
    What do you have in the way of functions to position the cursur on-screen? I'm thinking of Goto(x,y), (windows) or goto(x,y) (DOS), or maybe you have curses, or Ncurses?

    If you have nothing like that, then you may want to do it "old school". To do this, just print enough newlines '\n' to clear the screen, then print your data that needss to be on-screen, then print your menu, or whatever else the screen requires at that moment.

    I'd make printing the data to be constantly displayed as a function, so it can be called from any part of the switch statement, without repeating the same code, over and over.

    (which is a bit of a redundant description, but we'll just ignore that. )

    Adak

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    8
    I dont need the cursor on screen, i just need the list of 7 options, then a quit option, i.e.:

    1. Option 1
    2. Option 2
    3. Option 3
    etc
    etc
    etc
    etc
    8. Quit

    then the user taps in the number, presses enter, then gets asked how much of the fuel/activity, then it needs to add to the total CO2 emission, at the top, and the menu shows again, until the user selects quit.

    Im not sure where to put that while statement? Im a complete noob basically :P.

    Heres my code so far:

    Code:
    #include <stdio.h>
    #include <math.h>
    int main(void)
    {
    double amt[7];
    int a,b,c,d,e,f,g,type;
    amt[0]=0.0,amt[1]=0.0,amt[2]=0.0,amt[3]=0.0,amt[4]=0.0,amt[5]=0.0,amt[6]=0.0;
    
    
    printf("==========\nCurrent Co2 Emission:\n");
    printf("Total Co2 Emission is: %f", amt[0]+amt[1]+amt[2]+amt[3]+amt[4]+amt[5]+amt[6]);
    printf("\n\n======================\n\nPlease choose a fuel or activity from the menu below:");
    printf("\n1 - Petrol (litres)\n2 - Oil (litres)\n3 - Coal (kg)\n4 - Wood (kg)\n5 - Electricity (kWhr)\n6 - Natural Gas (kWhr)\n7 - Air Travel (miles)\n8 - Quit\n\n");
    
    switch (type) {
    case '1': printf("How much Petrol? (litres)");
                     scanf(" %d",&a);
                     amt[0] += a * 2.3;
    
    case '2': printf("How much Oil? (litres)");
                     scanf(" %d",&b);
                     amt[1] += b * 2.3;		
                    
    case '3': printf("How much Coal? (kg)");
                     scanf(" %d",&a);
                     amt[2] += c * 2.3;		 
    
    case '4': printf("How much Wood (kg)");
                     scanf(" %d",&a);
                     amt[3] += d * 2.3;
    
    case '5': printf("How much Electricity (kWhr)");
                     scanf(" %d",&a);
                     amt[4] += e * 2.3;
    
    
    case '6': printf("How much Natural Gas (kWhr)");
                     scanf(" %d",&a);
                     amt[5] += f * 2.3;
    		 
    case '7': printf("How much Air Travel (Miles)");
                     scanf(" %d",&a);
                     amt[6] += g * 2.3;
    
                     		 
    		 
    }
    getchar();
    return 0;
    }
    could you tell me where abouts the 'while' statement should be and what i should put in it?

    Cheers

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    8
    Ahh i kinda got what you mean now,

    Code:
    #include <stdio.h>
    #include <math.h>
    int main(void)
    {
    double amt[7];
    int a,b,c,d,e,f,g,type;
    amt[0]=0.0,amt[1]=0.0,amt[2]=0.0,amt[3]=0.0,amt[4]=0.0,amt[5]=0.0,amt[6]=0.0;
    
    while(1)  {
    printf("==========\nCurrent Co2 Emission:\n");
    printf("Total Co2 Emission is: %f", amt[0]+amt[1]+amt[2]+amt[3]+amt[4]+amt[5]+amt[6]);
    printf("\n\n======================\n\nPlease choose a fuel or activity from the menu below:");
    printf("\n1 - Petrol (litres)\n2 - Oil (litres)\n3 - Coal (kg)\n4 - Wood (kg)\n5 - Electricity (kWhr)\n6 - Natural Gas (kWhr)\n7 - Air Travel (miles)\n8 - Quit\n\n");
    
    
    switch (type) {
    case '1': printf("How much Petrol? (litres)");
                     scanf(" %d",&a);
                     amt[0] += a * 2.3;
    
    case '2': printf("How much Oil? (litres)");
                     scanf(" %d",&b);
                     amt[1] += b * 2.3;		
                    
    case '3': printf("How much Coal? (kg)");
                     scanf(" %d",&a);
                     amt[2] += c * 2.3;		 
    
    case '4': printf("How much Wood (kg)");
                     scanf(" %d",&a);
                     amt[3] += d * 2.3;
    
    case '5': printf("How much Electricity (kWhr)");
                     scanf(" %d",&a);
                     amt[4] += e * 2.3;
    
    
    case '6': printf("How much Natural Gas (kWhr)");
                     scanf(" %d",&a);
                     amt[5] += f * 2.3;
    		 
    case '7': printf("How much Air Travel (Miles)");
                     scanf(" %d",&a);
                     amt[6] += g * 2.3;
    		 
    case '8': break;
                     
    
    }
    
    
    		 
    		 
    }
    getchar();
    return 0;
    }
    Added that, no errors, but when run it just keeps looping the menu really fast, cant enter options or anything its just a blur of text, lol.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    First of all, you're not changing type anywhere before testing its value in the switch statement. Also, case statements fall through to the next case unless you break out of them.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,237
    Quote Originally Posted by Noori88 View Post
    Added that, no errors, but when run it just keeps looping the menu really fast, cant enter options or anything its just a blur of text, lol.
    You never read user input into the "type" variable. So yeah, it loops forever.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    8
    OK, i changed it a bit now:

    Code:
    #include <stdio.h>
    #include <math.h>
    int main(void)
    {
    double amt[7];
    int a,b,c,d,e,f,g,menu;
    amt[0]=0.0,amt[1]=0.0,amt[2]=0.0,amt[3]=0.0,amt[4]=0.0,amt[5]=0.0,amt[6]=0.0;
    
    while(1)  {
    printf("==========\nCurrent Co2 Emission:\n");
    printf("Total Co2 Emission is: %f", amt[0]+amt[1]+amt[2]+amt[3]+amt[4]+amt[5]+amt[6]);
    
    printf("\n\n=================\nPlease choose a fuel activity from below:\n\n1 - Petrol (litres)\n2 - Oil (litres)\n3 - Coal (kg)\n4 - Wood (kg)\n5 - Electricity (kWhr)\n6 - Natural Gas (kWhr)\n7 - Air Travel (miles)\n8 - Quit\n\n\t");
    scanf("%d",&menu); 
    
    
    switch (menu) {
    case '1': printf("How much Petrol? (litres)");
                     scanf(" %d",&a);
                     amt[0] += a * 2.3;
    		 break;
    
    case '2': printf("How much Oil? (litres)");
                     scanf(" %d",&b);
                     amt[1] += b * 2.3;
    		 break;
                    
    case '3': printf("How much Coal? (kg)");
                     scanf(" %d",&a);
                     amt[2] += c * 2.3;
    		 break;
    
    case '4': printf("How much Wood (kg)");
                     scanf(" %d",&a);
                     amt[3] += d * 2.3;
    		 break;
    
    case '5': printf("How much Electricity (kWhr)");
                     scanf(" %d",&a);
                     amt[4] += e * 2.3;
    		 break;
    
    
    case '6': printf("How much Natural Gas (kWhr)");
                     scanf(" %d",&a);
                     amt[5] += f * 2.3;
    		 break;
    		 
    case '7': printf("How much Air Travel (Miles)");
                     scanf(" %d",&a);
                     amt[6] += g * 2.3;
    		 break;
    		 
    case '8': break;
                     
    
    }
    if (menu != 1 || menu != 2 || menu != 3 || menu != 4 || menu != 5 || menu != 6 || menu != 7 || menu != 8);
    	printf("You did not enter a valid menu option");
    
    		 
    }
    getchar();
    return 0;
    }
    However it doesnt ask the user 'how much petrol' etc, i type a number and it displays the number and nothing else.

    I also get the warnings:

    Empty body in an if statement,
    c might be uninitialized,
    d """""""""""""""""""""
    e
    f
    g

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > Empty body in an if statement,
    That would be the ; at the end of the line.

    > c might be uninitialized,
    So initialise them then - is it that hard?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 08:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM

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