guidence to a program C

This is a discussion on guidence to a program C within the C Programming forums, part of the General Programming Boards category; I need guidence to solve a problem i'm having with a program (Exercise), When I enter -1 why does the ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    17

    guidence to a program C

    I need guidence to solve a problem i'm having with a program (Exercise), When I enter -1 why does the "Enter miles used" show up. It should just print " The overall average miles / gallon was" and if no data is entered print "No Data entered"

    PLEASE I dont want the answer just direction so I can learn for meself.

    Code:
    #include <stdio.h>
    
        /* fuction main begins program execution*/
    int main(void)
    {
    	/* intialization phase */
       int counter = 0;      /* number of tanful entered/ intialize loop counter */
       int miles = 0;          /* miles value  */
       float total = 0;        /*  sum of tankful/ intialize total */
    	
       float gallons;          /* gallons value */
       float tankful;          /* number with decimal point for average */
       float average;   	
    
    	/* process phase */
    	/* get gallons used from user  */
    	   printf("Enter gallons used, -1 to Exit: ");    /* prompt for input */
               scanf("%f", &gallons);		    		 /* read gallons from user */
    	   printf(" Enter miles used: ");		       /* prompt for input   */	
    	   scanf("%d", &miles);					 /* read miles from user*/
    	   tankful = miles / gallons;				 /* calculate average */	
    	   printf("The miles / gallons for this tank was %f", tankful);
      
          /* loop while sentinel value not yet read from user */
    	   while(gallons != -1) {    
    		 total = total + tankful;	     		   /* add average to total   */
    		 counter = counter + 1;			        /* increment counter       */
    
        /* get next gallonful from user  */	
    	   printf("\nEnter gallons used, -1 to Exit: ");   /* prompt for input */
    	   scanf("%f", &gallons);		    		    /* read gallons from user */
    	   printf(" Enter miles used: ");		          /* prompt for input */
               scanf("%d", &miles);					    /* read miles from user  */
    	   tankful = miles / gallons;
    	   printf("The mile / gallons for this tank was %f", tankful);
      } /* end while */   
    
        /* termination phase  */
        /* if user entered at least one tankful*/
    	 if(counter != 0) {
    
    	/* calculate average of all the tankfuls entered*/
    	    average = (float) total / counter;        /* avoid truncation*/
    
    	/* display average with six digits of precision*/
    	   printf("The overall average miles / gallon was %f\n", average);
      } /* end if                                            */
    	 else { /* if no tankfuls were entered, output message*/
    	   printf("\nNo data entered\n");
      } /* end else */
    
    	return (0);		                       /* indicate program ended ok */
    }   /* end fuction main  */
    Thank for your time

    Nurofen

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    because the program goes to the next line of code.

    You should start the while statement just after the first scanf.

    that should work

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    17
    When I do that the output is:

    "Enter gallons used, -1 to Exit: " 12.8
    and the it repeats to
    "Enter gallons used, -1 to Exit: "

    it should be
    "Enter gallons used, -1 to Exit: " 12.8
    " Enter miles used: " 287
    "The miles / gallons for this tank was " 22.421875

    "Enter gallons used, -1 to Exit: "

    Thanks for your time

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    17
    sorry people but I've tried but with no joy.

    Can someone show me what i'm doing wrong please.

    Thanking you in advance

    Nurofen

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to put everything that you want to repeat in the while loop -- so the while loop should start after reading the gallons used and go through to the next reading in of the gallons used.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    17
    Code:
    printf("Enter gallons used, -1 to Exit: "); 
               scanf("%f", &gallons);	
    
    while(gallons != -1) {    
    		 total = total + tankful;	     		  
    		 counter = counter + 1;			       
    
       
    	   printf("\nEnter gallons used, -1 to Exit: ");   
    	   scanf("%f", &gallons);		    		    
    	   printf(" Enter miles used: ");		         
               scanf("%d", &miles);					   
    	   tankful = miles / gallons;
    	   printf("The mile / gallons for this tank was %f", tankful);
      }
    when i do this the output is:
    "Enter gallons used, -1 to Exit: " 12.8
    and the it repeats to
    "Enter gallons used, -1 to Exit: "

    it should be
    "Enter gallons used, -1 to Exit: " 12.8
    " Enter miles used: " 287
    "The miles / gallons for this tank was " 22.421875

    "Enter gallons used, -1 to Exit: "

    Nurofen

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You don't start the while loop with the prompt for information, you just did the prompt for information. You need to end the while loop with the prompt for information (so that the new data is ready to check when you get back to the top). So move the printf and scanf from the top of the loop to the bottom.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    gallons != -1
    not very good idea for float values
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, -1 can be represented exactly in a float; so I guess it's up to the user, when it says "-1 to exit" to not type -1.153.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    17
    yes very true.
    Last edited by nurofen; 04-13-2008 at 01:35 PM.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    17

    Question

    I still can't get it to work, what i'm I doing wrong?

  12. #12
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    493
    try using '\n' in all ur printf stts.
    EDIT:Also the o/p u have mentioned is what is to be expected as you are asking for
    gallons twice(once before the while and once within it).
    Last edited by stevesmithx; 04-14-2008 at 03:39 PM.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    17
    I tried \n in printf stts no joy.

    I see I have changed the part (thank you )

    when i enter -1 why does it as for miles used?

    i would like it to go
    Code:
    printf("\nThe overall average miles / gallon was &#37;f\n", average);
    Last edited by nurofen; 04-14-2008 at 03:47 PM.

  14. #14
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    493
    try changing your code to:
    Code:
    printf("Enter gallons used, -1 to Exit: "); 
               scanf("%f", &gallons);	
    
    while(gallons != -1) {    
    		 total = total + tankful;	     		  
    		 counter = counter + 1;			       
         
    	   printf(" Enter miles used: ");		         
               scanf("%d", &miles);					   
    	   tankful = miles / gallons;
    	   printf("The mile / gallons for this tank was %f", tankful);
               printf("\nEnter gallons used, -1 to Exit: ");   
    	   scanf("%f", &gallons);		    		    
      }
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  15. #15
    Registered User
    Join Date
    Mar 2008
    Posts
    17

    Thumbs up

    Thank you so much it works...


    Can you please explain why it works that why if you wouldn't mind and not all the othe ways


    Thank you again

    stevesmithx

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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