Thread: Trouble with program

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    4

    Trouble with program

    Hey all, I am having this problem in my intro to programming class. This program will execute correctly but the only answer I get is 0. Can someone help me?

    Instructions: Write a program that will convert a dollar amount and return the number of cents in the amount. For example, if the user enters 5.33 then your program should return "533 cents". Your program should use a function (besides the main) called compute_cents that will take the dollar amount entered by the user and return the number of cents in the amount.

    code:

    Code:
    #include <stdio.h>
    int compute_cents(int cents);
    int main(void)
    {
         double dollar_amount, cents;
         printf("Enter dollar amount");
         scanf_s("%lf", &dollar_amount);
         cents = 100 * dollar_amount;
         printf("Cents: %d", cents);
         return 0;
    }
    
    
    int compute_cents(int cents);

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    In your program as is, you don't need the compute_cents prototypes since you haven't written the function and aren't trying to use it. Note however that you've declared cents in main as a double but are printing it with "%d" which is used for ints. Changing the "%d" to "%f.0" should work.

    But the instructions say to use a function.

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    4
    Quote Originally Posted by algorism View Post
    In your program as is, you don't need the compute_cents prototypes since you haven't written the function and aren't trying to use it. Note however that you've declared cents in main as a double but are printing it with "%d" which is used for ints. Changing the "%d" to "%f.0" should work.

    But the instructions say to use a function.
    That worked! Thank you very much. The only issue is that if I remove the compute_cents prototype it works perfectly. I know that my instructor had a reason for us to use that header so I think that I am doing something wrong.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You're supposed to write a function to go along with that function prototype.
    In that case, it will return an int and you should print it with "%d".
    Actually, the function prototype looks wrong since it's supposed to take in the dollar_amount and return the cents.
    Presumably it should be:
    Code:
    int compute_cents(double dollar_amount);
    Then the function would look something like this:
    Code:
    int compute_cents(double dollar_amount) {
        int cents;
    
        // compute the cents (as an int) here
    
        return cents;
    }
    And your main would be something like:
    Code:
    int main(void)
    {
        double dollar_amount;
        int cents;
    
        printf("Enter dollar amount");
        scanf_s("%lf", &dollar_amount);
    
        cents = compute_cents(dollar_amount);
    
        printf("Cents: %d\n", cents);
    
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Feb 2017
    Posts
    4
    Thanks! Unfortunately on the "return cents;" line, it is saying that cents in uninitialized.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Quote Originally Posted by Kosios View Post
    Thanks! Unfortunately on the "return cents;" line, it is saying that cents in uninitialized.
    Of course it does.
    You have to read the comment and put some code in there to actually do what the comment says.

    int cents;
    // compute the cents (as an int) here
    cents = 42;
    return cents;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Kosios View Post
    Thanks! Unfortunately on the "return cents;" line, it is saying that cents in uninitialized.
    Well, duh! It's not the whole program. You need to first of all THINK (jeez!), and then fill in the part where I put a comment. I've given you too much already.

  8. #8
    Registered User
    Join Date
    Feb 2017
    Posts
    4
    Oh tha'ts what that was... i'm new at this lol. Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble with this program! Help please!
    By Sheeez in forum C Programming
    Replies: 2
    Last Post: 11-10-2013, 03:14 PM
  2. My first program trouble
    By pal1ndr0me in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2006, 12:09 AM
  3. trouble with my first c program
    By mblazar in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 02:06 AM
  4. Having trouble with program
    By Bonedaddy in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2003, 06:14 PM
  5. big trouble in little program
    By jonesy in forum C Programming
    Replies: 1
    Last Post: 10-04-2001, 02:04 PM

Tags for this Thread