Code to convert from pints

This is a discussion on Code to convert from pints within the C++ Programming forums, part of the General Programming Boards category; My assignment is to write a program that reads a list of dry measures in pints until end-of-file and then ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    13

    Code to convert from pints

    My assignment is to write a program that reads a list of dry measures in pints until end-of-file and then converts each measure into quarts and pecks.

    Here is my code so far:

    Code:
    #include <stdio.h>
    
    main()
    {
    
                    int pint, peck, quart;
    
    	printf(" Enter next amount:  ");
    	scanf( "%d", &pint );
    
    	while ( peck >= 0 ) {
    		peck = 0.0537104354 * pint;
    		quart = .5 * pint;
    		printf( "\n%d pints =\n", pint );
    		printf( "%d pecks\n", peck );
    		printf( "%d quarts\n", quart  );
    		printf( " Enter next amount: " );
    		scanf( "%d", &pint );
    	}
    
    	printf( "*** END OF PROGRAM ***\n" );
    
    }



    It is not converting at all. All it does is prompt the user to enter an amount, and after that it just says *** END OF PROGRAM ***. At one point it was converting, but the numbers weren't right. I changed some things and now it barely does anything. I don't know what's going on?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    side note: this is C code, so it should be in the C forum.

    - you should declare main as 'int main()', or perhaps 'int main(<parameters>)' (see the faq!)

    - note you read in something and store it in pint, but your while loop checks if peck >=0, i imagine you want peck to be pint. (FYI: do a printf on the values of pint, peck and quart, right after the 'int pint,...' declaration. you will see that the values printed wont be what you expect.

    - the int datatype stores integers--that is, numbers without a decimal. so, it will only keep the integer value of the product 0.05... * pint. also, 0.05... i imagine will converge/equal 0, not some fraction (because it will take it as an int). so 0*pint = 0, for all values of pint. the same may go for your other variables, check them out

    - you should probably store at least peck and quart as floats, and maybe pints if you allow fractions of pints to be entered. when you change this, make sure to change the scanf and/or printf functions so that they deal with floats not integers.
    Last edited by nadroj; 11-29-2007 at 10:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to convert decimal number to ASCII code?
    By oie in forum C Programming
    Replies: 11
    Last Post: 11-03-2006, 05:19 PM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  3. Replies: 1
    Last Post: 03-21-2006, 06:52 AM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 12:33 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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