Thread: Help with this program

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    Help with this program

    Hello, I have a program I am trying to write. In my book it asks for me to make a program that takes pennies, dimes, nickles, half dollars, and quaters and make a total out of them. I have attempted to make the program. If anyone can point me in the right direction on fixing this, it would be great.

    I have 20 quaters, 12 Half dollars, 32 dimes, 45 Nickels, 27 pennies. I

    Heres my code:

    Code:
    #include <stdio.h>
    int main()
    {
      float quat;    /* declare quat as a double variable */
      float halfdol; /* declare halfdol as a double variable */
      float dime;    /* declare dime as a double variable */ 
      float nick;    /* declare nick as a double variable */
      float pen;     /* declare pen as a double variable */
      
      quat = 20 * .25f;
      halfdol = 12 * .50f;
      dime = 32 * .10f;
      nick = 45 * .05f;
      pen = 27 * .01f;
      total = quat + halfdol + dime + nick + pen;
      printf("The total of the change is %f");
        
        return 0;
    }
    Thanks, I think I am totally off on this. I am not looking for someone to do this for me. I am just looking for help. Thanks.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    printf("The total of the change is %f");
    printf() expects a variable to be passed for each %* in the format string; in this case, for a %f, it's expecting a float or a double variable.
    Code:
    float num = 3.14;
    printf("PI = %f\n", num);
    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.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    30
    your are not printing your formula in total:

    Code:
    printf("The total of the change is %f");

    just do this:

    Code:
    printf("The total of the change is %f", total);

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    Code:
    total = quat + halfdol + dime + nick + pen;
    where exactly is 'total' coming from? you haven't declared it!

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And your comments are inaccurate:
    Code:
    float quat;    /* declare quat as a double variable */
    A double variable is different from a float variable. doubles have more digits (or equal digits, but if one of the mentioned data types has more digits, it will be the double).
    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.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    Hello, OK I have gotten some help so far. I fixed her up to this:

    Code:
    #include <stdio.h>
    int main()
    {
      float quat = .25;    
      float halfdol = .50; 
      float dime = .10;     
      float nick = .05;    
      float pen = .01;
      float total;
    
        quat * 20, halfdol * 12, dime * 32, nick * 45, pen * 27;
    
        total = quat + halfdol + dime + nick + pen;
    
      printf("The dollar amount is %f\n")
    
        return 0;
    }
    I get an error at the return 0; line
    I get this error when i try to compile:

    "fig263B.c", line 17.5: 1506-277 (S) Syntax error: possible missing ';' or ','?

    it looks right to me... I just want to take the money, multiply the given amount of change by the value of each of the change and get a total. im confused beyond this... Please help!

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It's because you're missing the semi-colon at the end of the printf() call right above it.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    ok, I fixed the error, but when I run the program, it just gives me 0.0000000 as an answer. Thanks for the help.

    Somewheres I must have some thing put wrong somewhere. it wont do the math, so somthing isent working.
    Last edited by kdubbz; 09-19-2006 at 02:49 PM.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    printf("The dollar amount is %f\n")
    You have to tell printf() what to substitute for the %f. Look at dwk's and neo_phyte's posts.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by kdubbz
    Hello, OK I have gotten some help so far. I fixed her up to this:

    Code:
    #include <stdio.h>
    int main()
    {
      float quat = .25;    
      float halfdol = .50; 
      float dime = .10;     
      float nick = .05;    
      float pen = .01;
      float total;
    
        quat * 20, halfdol * 12, dime * 32, nick * 45, pen * 27;
    
        total = quat + halfdol + dime + nick + pen;
    
      printf("The dollar amount is %f\n")
    
        return 0;
    }
    I get an error at the return 0; line
    I get this error when i try to compile:

    "fig263B.c", line 17.5: 1506-277 (S) Syntax error: possible missing ';' or ','?

    it looks right to me... I just want to take the money, multiply the given amount of change by the value of each of the change and get a total. im confused beyond this... Please help!
    Ok, you've fixed the printf statement so it includes a semi-colon at the end. Now are you printing total, in that line of code?

    And if you're printing total (pennies), what about then changing that total pennies to customary terms for money?

    We don't say "I have 552 pennies", we say "I have 5 dollars and 52 cents". Something to work on there. For the sake of clarity, it might be nice to include an integer variable "dollars", if your assignment allows it.

    Adak

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > printf("The dollar amount is %f\n")
    Pass a parameter, say
    printf("The dollar amount is %f\n", total);
    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.

  12. #12
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    See post 2 and 3 in this thread. If you had read them you wouldn't be having that issue.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    4
    Ok, I fixed it up and its all done. Hey im getting good at this, this is my 2nd one done so far. hers the final code:

    Code:
    #include <stdio.h>
    int main()
    {
      float quat;   
      float halfdol; 
      float dime;     
      float nick;    
      float pen;
      float total;
    
      quat = .25;
      halfdol = .5;
      dime = .10;
      nick = .05;
      pen = .01;
    
       total =  quat * 20 +  halfdol * 12 +  dime * 32 +  nick * 45 +  pen * 27;
    
        printf("The dollar amount is %f\n",total);
    
        return 0;
    }
    Which gives you after compiling: The dollar amount is 16.719999


    Success. Thanks guys.

  14. #14
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Perhaps I dweal in an alternate universe. . . or am way behind the times. . . but would someone explain what the following line of code does?
    Code:
    quat * 20, halfdol * 12, dime * 32, nick * 45, pen * 27;
    I think the answer to my question is NOTHING!!!

    EDIT: Ah, kdubbz took that out just before I posted. . .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM