Problem with sentinel values

This is a discussion on Problem with sentinel values within the C Programming forums, part of the General Programming Boards category; I am working independently through Deitel's How to Program C and I am having some difficulty with sentinel values. The ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    1

    Problem with sentinel values

    I am working independently through Deitel's How to Program C and I am having some difficulty with sentinel values. The problem is that the sentinel value doesn't stop the program, but instead uses the sentinel value( in this case -1) in the calculation. This leads to averages that are negative or far from the correct value. Please help. Here is what I have so far, if it helps.

    also - I am using visual c++ 2005 express addition, and i am constantly having to insert the first piece of code (#define _CRT_SECURE_NO_DEPRECATE)
    because visual c++ wants me to use scanf_s instead of scanf. Is this normal?

    Code:
    #define _CRT_SECURE_NO_DEPRECATE
    #include <stdio.h>
    
    int main( void )
    {
    	int miles, gallons, totalmiles, totalgallons;
    	float mpg, totalmpg;
    
    	totalmiles = 0, totalgallons = 0;
    
    	printf( "Enter the gallons used (-1 to end): " );
    	scanf( "%d", &gallons );
    
    	totalgallons = totalgallons + gallons;
    
    	printf( "Enter the miles driven: " );
    	scanf( "%d", &miles );
    
    	totalmiles = totalmiles + miles;
    
    	mpg = ( float )miles / gallons;
    	
    	printf( "The miles/gallon for this tank was %.6f\n", mpg );
    
    	while ( gallons != -1 ) {
    		printf( "Enter the gallons used (-1 to end): " );
    		scanf( "%d", &gallons );
    		
    		totalgallons = totalgallons + gallons;
    		
    		printf( "Enter the miles driven: " );
    		scanf( "%d", &miles );
    
    		totalmiles = totalmiles + miles;
    
    		mpg = miles / gallons;
    		printf( "The miles / gallon for this tank was %.6f\n", mpg );
    	}
    		
    	if ( totalmiles != 0 ) {
    		totalmpg = ( float )totalmiles / totalgallons;
    
    		printf( "The overall average miles/gallon was %.6f\n", totalmpg );
    	}
    	else {
    		printf( "No gallons were entered\n" );
    	}
    
    	return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, of course it uses the -1 in the calculation (and it asks for a number of miles too, right?)

    You need to add a check to see if the gallons is -1 and only do the rest of the loop if it is. A while-loop doesn't test the value at every instance the value is changed, only when it reaches the top of the loop.

    --
    Mats

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    also - I am using visual c++ 2005 express addition, and i am constantly having to insert the first piece of code (#define _CRT_SECURE_NO_DEPRECATE)
    because visual c++ wants me to use scanf_s instead of scanf. Is this normal?
    I have heard that MSVC++ 2005 express complains whenever you use scanf() or sprintf() or other standard C functions. So yes, it's normal. I didn't know that you could disable it with a #define, though.

    Code:
    	while ( gallons != -1 ) {
    		printf( "Enter the gallons used (-1 to end): " );
    		scanf( "&#37;d", &gallons );
    		
    		totalgallons = totalgallons + gallons;
    		
    		/* ... */
    	}
    You read in gallons, then go on to process it, assuming it is a valid number; then, after all of the calculations have been performs, and only then, is the expression in the while evaluated and found to be false.

    You could fix this with a break statement:
    Code:
    if(gallons == -1) break;
    Or you could wrap if(gallons != -1) around the following code.

    [edit] A bit late, but oh well. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Code:
    	printf( "Enter the gallons used (-1 to end): " );
    	scanf( "%d", &gallons );
    
    	totalgallons = totalgallons + gallons;
    
    	printf( "Enter the miles driven: " );
    	scanf( "%d", &miles );
    
    	totalmiles = totalmiles + miles;
    
    	mpg = ( float )miles / gallons;
    	
    	printf( "The miles/gallon for this tank was %.6f\n", mpg );
    It looks like you've duplicated this code twice, once outside the loop and once inside. If you get rid of the instance outside the loop, you could then add a check for
    Code:
    if (gallons < 0) {
        printf ("exiting\n"); 
        break; 
    }
    just after scanf. You should avoid code duplication wherever possible!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Problem Passing Values between functions
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2007, 01:21 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Replies: 1
    Last Post: 02-03-2005, 02:33 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM

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