Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-14-2013, 09:15 AM   PM User | #1
jerdev
New Coder

 
Join Date: Nov 2012
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
jerdev is an unknown quantity at this point
Program I wrote in C. Any improvements I can make?

I want to learn how to write code in a way so that if I need to add/edit things in(to) it, I can do it without changing much, if any, code that was already written.
examples could be like, making it so that if there is only 1 coin [be it either a quarter/dime/nickel/penny] it would say "1 penny" not "1 pennies". etc etc

So basically, I want a seamless style of coding.

this is a program i wrote for a pset while learning on cs50.tv
Code:
#include <stdio.h>
#include <cs50.h>

// get value of money to get change for
// convert money into cents
// while (change is more than a quarter)
//     subtract a quarter
//     increment number of coins used by 1
// while (change is more than a dime)
//     subtract a dime
//     increment number of coins used by 1
// ...
// while (change is 0)
//     output number of coins used
// finish program

int main(void)
{
    int q = 0;
    int d = 0;
    int n = 0;
    int p = 0;
    printf("This program will calculate the least amount of coins needed to give you your change.\nChange due in dollars (exclude the dollar sign): ");
    float f = GetFloat();
    float a = f * 100;
    printf("$%.2f = %.0f˘\n", f, a);
    while (a >= 25)
    {
        a = a - 25;
        q = q + 1;
    }
    while (a >= 10)
    {
        a = a - 10;
        d = d + 1;
    }
    while (a >= 5)
    {
        a = a - 5;
        n = n + 1;
    }
    while (a >= 1)
    {
        a = a - 1;
        p = p + 1;
    }
    printf("You need %d quarters.\nYou need %d dimes.\nYou need %d nickels.\nYou need %d pennies.\n%d coins total.\n", q, d, n, p, q + d + n + p);
}

Last edited by jerdev; 01-14-2013 at 09:19 AM..
jerdev is offline   Reply With Quote
Old 01-14-2013, 11:10 PM   PM User | #2
negative zero
New to the CF scene

 
Join Date: Jan 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
negative zero is an unknown quantity at this point
I can tell just by reading that my implementation won't compile this. My implementation has no <cs50.h>. In addition, I've never seen a built-in GetFloat().

I have reason to believe that cs50.h is a project file. If this is the case, it doesn't belong between <these brackets> but instead between "these quotation marks".

I have reason to believe that GetInt() is declared and defined in cs50.h. Code shouldn't be defined in .h (header) files, but in .c files. Furthermore, "GetFloat()" seems a horrible Java-esque practise. The reason it's horrible in C is that C has no exceptions. By masking the return value of scanf, you are making error handling extremely difficult. I suggest studying this scanf manual carefully before answering the following questions:
1. How would you describe the value that scanf returns on match failure?
2. How would you describe the value that scanf returns when stdin is EOF-marked, or some other error occurs?
3. What would be the only successful return value of a scanf call that is expected to write to 4 items?

Once you've studied that manual carefully, you shouldn't need to mask it behind GetFloat.
negative zero is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:51 AM.


Advertisement
Log in to turn off these ads.