Friday, May 21, 2010

Problem with my C program.. please help me.. thanks?

the question says:


Develop a program that will input the miles driven and gallons used for each tankful. The program should calculate and display the miles per gallon obtained for each tankful. After processing all input information, the program should calculate and print the combined miles per gallon obtained for all tankfuls.








I really don't know what else to do all i have is:


#include %26lt;stdio.h%26gt;





int main(void)


{


int miles;


int gallons;


float mpg;





printf("enter miles traveled\n");


scanf("%d", %26amp;miles);


printf("enter Number of gallons used\n ");


scanf("%d", %26amp;gallons);


mpg = (float)(miles/gallons);


printf("total miles per gallons: %0.2f\n", mpg);





system("pause");


return 0;


}


how can i make several tankfulls add up and how can i make the person imput more that one tankfulls


em... why does the program output in decimal? help me please i'm a beginner and i'm practically lost





thanks,, thanks,, thanks,

Problem with my C program.. please help me.. thanks?
Try this for an input loop:





int main(void)


{


int miles = 0;


int gallons = 0;


float mpg;


int totalgallons = 0;


int totalmiles = 0;





while(miles %26gt; 0) {


printf("enter miles traveled [enter 0 to exit]\n");


totalmiles += scanf("%d", %26amp;miles);


if(miles %26gt; 0) {


printf("enter Number of gallons used\n ");


totalgallons += scanf("%d", %26amp;gallons);


}


}


mpg = (float)(totalmiles / totalgallons);


printf("total miles per gallons: %0.2f\n", mpg);





system("pause");


return 0;


}


No comments:

Post a Comment