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:
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.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; }
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



LinkBack URL
About LinkBacks




