consumption

This is a discussion on consumption within the C Programming forums, part of the General Programming Boards category; i wrote this program, but somthenig is missing when i input -1 to finish. instead to have straight this :the ...

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

    consumption

    i wrote this program, but somthenig is missing when i input -1 to finish. instead to have straight this :the overall average miles/gallons:.... my average consumption result, the program output this: Enter the miles driven and it follows:the overall average miles/gallons:

    itwil be kind, i had two days stugling.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      int miles;
      int gallons;
      int counter;
      int total;
        
      float average;
      float consumption;
      
      counter = 0;
      total =  0;
        
      printf ("Enter the gallons used, -1 to end :");
      scanf("%d", &gallons);
      printf("Enter the miles driven :");
      scanf("%d", &miles);
          
      while (gallons != -1){
            consumption = (float) miles / gallons;
      printf("The miles / gallons for this thank was %.2f \n", consumption);  
       
            total += consumption;
            counter += 1;
            
      printf ("\nEnter the gallons used, -1 to end :");
      scanf("%d", &gallons);          
       
      printf("Enter the miles driven :");
      scanf("%d", &miles);  
            }        
            
      if (counter != 0){
                  average = (float) total / counter;
      printf ("\nthe overall average miles/gallons %.2f \n", average);
            }
            
      else{
      printf ("\nNo  No No  ");      
      }
      
      system("PAUSE");	
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    please indent your code better next time. Here's the new indented code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	int miles;
    	int gallons;
    	int counter;
    	int total;
    
    	float average;
    	float consumption;
    
    	counter = 0;
    	total =  0;
    
    	printf ("Enter the gallons used, -1 to end :");
    	scanf("&#37;d", &gallons);
    	printf("Enter the miles driven :");
    	scanf("%d", &miles);
    
    	while (gallons != -1){
    		consumption = (float) miles / gallons;
    		printf("The miles / gallons for this thank was %.2f \n", consumption);
    
    		total += consumption;
    		counter += 1;
    
    		printf ("\nEnter the gallons used, -1 to end :");
    		scanf("%d", &gallons);
    
    		printf("Enter the miles driven :");
    		scanf("%d", &miles);
    	}
    
    	if (counter != 0){
    		average = (float) total / counter;
    		printf ("\nthe overall average miles/gallons %.2f \n", average);
    	}
    
    	else{
    		printf ("\nNo  No No  ");
    	}
    
    	system("PAUSE");
    	return 0;
    }

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Compare the differences from the changes I have made.

    1) You were taking in a -1 for gallons, but then acting upon it as if it were a valid value in the loop. Having a WHILE condition at the top of the loop was not correct.

    2) Now, the questions are only asked in one place, instead of two places.

    3) The overall calculation is now correct. You can't save the "consumption" (which was the average miles per gallon) and just add to it time and time again.

    Todd

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	int miles, total_miles = 0 ;
    	int gallons, total_gallons = 0 ;
    	int counter = 0 ;
    	
    	float average;
    	float consumption;
              
    	while (1) {
    
    		printf ("Enter the gallons used, -1 to end :");
    		scanf("&#37;d", &gallons);
    		if (gallons <= 0) break ; 
    		
    		total_gallons += gallons ; 
    		
    		printf("Enter the miles driven :");
    		scanf("%d", &miles);
    	
    		if (miles <=0) break ; 
    	
    		total_miles += miles ; 
    	
    		consumption = (float) miles / gallons;
    		printf("The miles / gallons for this thank was %.2f \n", consumption);  
    		counter += 1;
    	}        
            
    	if (counter != 0){
    		average = (float) total_miles / total_gallons ;
    		printf ("\nthe overall average miles/gallons %.2f \n", average);
    	}
              
    	system("PAUSE");	
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    you'll need to put a break statement in the loop after -1 has been input.

    EDIT: sorry, Todd beat me.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2
    Quote Originally Posted by Abda92 View Post
    you'll need to put a break statement in the loop after -1 has been input.

    EDIT: sorry, Todd beat me.
    thanks very much, it did really get the result i have been waiting. the thing, is i don't want to use the worb "break" in the program, because i can't show it to the teacher, we stil didn t get to this level of programming., I need it as it is but with adjustment.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Then just put...
    Code:
    while (miles <= 0)
    ...instead.
    Be sure to initialize miles to 0.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    system("PAUSE");
    Dont use system use getchar, protobility issues

    ssharish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calculate memory consumption and cpu usage
    By GermanDev in forum C++ Programming
    Replies: 18
    Last Post: 12-14-2008, 04:32 PM
  2. Should it be legalized?
    By abachler in forum A Brief History of Cprogramming.com
    Replies: 168
    Last Post: 06-08-2008, 08:56 AM
  3. programming laptop
    By tuurb046 in forum Tech Board
    Replies: 26
    Last Post: 01-02-2008, 01:41 AM
  4. Computer power consumption
    By Micko in forum Tech Board
    Replies: 2
    Last Post: 11-07-2006, 11:10 PM
  5. How to measure memory consumption?
    By nysubmarine in forum C++ Programming
    Replies: 25
    Last Post: 06-27-2006, 03:40 AM

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