Thread: getting incorrect output

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    18

    Post getting incorrect output

    hi i'm doing an exercise from C Primer Plus. the problem is this:
    In the U.S. system of volume measurements, a pint is 2 cups, a cup is 8 ounces, an
    ounce is 2 tablespoons, and a tablespoon is 3 teaspoons. Write a program that requests a
    volume in cups and that displays the equivalent volumes in pints, ounces, tablespoons,
    and teaspoons. Why does a floating-point type make more sense for this application than
    an integer type?
    this is the code i wrote:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        float pint, cup, ounce, tablespoon, teaspoon;
        pint = 2 * cup;
        cup = 8 * ounce;
        ounce = 2 * tablespoon;
        tablespoon = 3 * teaspoon;
        printf("How many cups? ");
        scanf("%f", &cup);
        printf("pints: %f -- ounces: %f -- tablespoon: %f -- teaspoon %f \n", pint, ounce, tablespoon, teaspoon );
        return 0;
    }
    and this is the output:
    How many cups? 25
    pints: 0.000000 -- ounces: 0.000000 -- tablespoon: 0.000000 -- teaspoon 0.000000

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Ask yourself, what does cup equal when you are calling this line

    pint = 2* cup;

    You need to initialize your variables before trying to do calculations on them, else they will have random values associated with them.
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    18
    i fixed this with this code:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        float pint, cup, ounce, tablespoon, teaspoon;
        printf("How many cups? ");
        scanf("%f", &cup);
        pint = cup / 2;
        ounce = cup / 8;
        tablespoon = ounce / 2;
        teaspoon = tablespoon / 2;
        printf("pints: %f -- ounces: %f -- tablespoon: %f -- teaspoon %f \n", pint, ounce, tablespoon, teaspoon );
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2016
    Posts
    7
    Quote Originally Posted by Mohsen Abasi View Post
    i fixed this with this code:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        float pint, cup, ounce, tablespoon, teaspoon;
        printf("How many cups? ");
        scanf("%f", &cup);
        pint = cup / 2;
        ounce = cup / 8;
        tablespoon = ounce / 2;
        teaspoon = tablespoon / 2;
        printf("pints: %f -- ounces: %f -- tablespoon: %f -- teaspoon %f \n", pint, ounce, tablespoon, teaspoon );
        return 0;
    }
    When I try to run this program in Visual Basic 2013 it only asks for the first line flashes then leaves the screen how can i get it to pause to show it all printed? I have tried the ("PAUSE") but that doesn't work.

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    look into adding two getchar(); statements at the bottom of your program.

    The first getchar() will be to take in the newline character that will be left in the buffer after you enter in your scanf call

    second getchar() will be to keep the screen open until someone hit's a key.
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by camel-man View Post
    look into adding two getchar(); statements at the bottom of your program.

    The first getchar() will be to take in the newline character that will be left in the buffer after you enter in your scanf call

    second getchar() will be to keep the screen open until someone hit's a key.
    You need to do the above suggestion before the return statement.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. incorrect output
    By Alint808 in forum C Programming
    Replies: 8
    Last Post: 07-17-2011, 06:21 PM
  2. RPN incorrect output
    By csharp100 in forum C Programming
    Replies: 5
    Last Post: 10-14-2010, 12:36 PM
  3. incorrect output
    By linuxman in forum C Programming
    Replies: 4
    Last Post: 01-03-2004, 08:01 AM
  4. Incorrect Output
    By Nutshell in forum C Programming
    Replies: 2
    Last Post: 01-07-2002, 09:11 AM
  5. incorrect output
    By runtojesus in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2001, 04:18 PM

Tags for this Thread