Thread: I cant figure out why my simple program is not working!

  1. #16
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    Quote Originally Posted by Tomwa View Post
    Your primary issues were:
    1) Failed to initialize your variables, when you create a variable memory is allocated for that variable by the operating system, there is however no guarantee that the value in that memory is 0 (Thus why you have to initialize your variables to zero) this ensures you don't get crazy numbers from your arithmetic.

    2) Indentation, remember that you indent when you enter a new code block and when you have to much to fit on one line. You also dropped a space between return and 0.

    3) Arithmetic, you had some botched arithmetic, you didn't need to use the modulus operator at all (thats the % sign).

    4) Botched printf (I use linux man to lookup most function definitions:
    printf(3): formatted output conversion - Linux man page

    5) Unnecessary scanf, after entering all of your monetary values you accidentally read new values in to sum and r.

    Hopefully I've been concise in my explanations, if you need any further clarification I'll be glad to assist.

    Best of luck!

    Here's the fixed code:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        /* Remember to initialize variables to zero
         * as you cannot depend on having zero-ed memory */
        int p = 0,n = 0,d = 0,q = 0;
        double sum = 0;
        /* You didn't need the additional variables,
         * everything you wanted to do can be done with the variables 
         * you already made */
    
        printf("enter number of pennies:");
        scanf("%d",&p);
        
        printf("enter number of nickels:");
        scanf("%d",&n);
        /* Multiply number of nickels by 5,
         * Note: n*=5 is the same thing as n = n * 5 */
        n *= 5;
    
        printf("enter number of dimes:");
        scanf("%d",&d);
        /* Multiply number of dimes by 10,
         * Note: d*=10 is the same thing as d = d * 5 */
        d *= 10;
    
        printf("enter number of quarters:");
        scanf("%d",&q);
        // Do the same thing for quarters
        q *= 25;
    
        /* Because money has decimal points we need more precision
         * This means we need to cast our integers to doubles (or floats)
         * to ensure we don't lose the decimal points 
         * (An integer divided by an integer is an integer) */                                       
        sum=(double)(n+d+q+p)/100;
    
        // Print the value of sum;
        // Note: .2f is a format specifiers it tells printf you print
        // 2 decimal places.
        printf("you have %.2f dollars",sum);
    
        return 0;
    }
    Thanks a lot for very useful info I am very novice on programming and I thought "float" represent only decimal values between 0 and 1. And thats why I needed to calculate cents and dollars separately and get them together at the end. I created kind of complex formula to make the "sum" an integer. I know I should have read more before programming this.

  2. #17
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Lesshardtofind View Post
    And I guess that is why Grumpy and Laser seem to lean towards initializing or declaring based upon its nearest usage point because it encourages understanding of the need for the variable.
    In my case, definitely.

    There is only one sure-fire way to eliminate bugs from a program. That involves hard work. To give a precise description of requirements (and the problem to be addressed or the task that the software is to achieve). To understand what each code construct (whether it is a declaration/definition, an expression, or even a preprocessor directive) you use contributes to meeting those requirements. To define test cases that will fail unless the software meets the requirements, or - if the test cases are passed - can be used to provide a body of defendable evidence about how well the requirements are met.

    Instead, what we have is a lot of rules of thumb, often based on a false premise that a few small simple tricks can eliminate bugs from a program. The thing is, if you dig into the places where many of those rules originate (journal articles, teacher guides, coding standards) their primary goal is to be easy to use, teach (it is easier to get a novice to add a "= 0" than it is to get them to think about how a variable contributes to the code working as required) and (in a lot of cases) be automatically enforced (eg by some software tool, such as a compiler, that enforces coding standards). The secondary goal (at best) of these rules is that they can, in some hopefully realistic scenarios, be demonstrated to reduce incidence of bugs in software.

    In practice, some authors do express a wish that, by encouraging programmers to follow their chosen rules of thumb or coding guidelines, that they will encourage programmers to think more carefully about their code, and thereby reduce bug counts. In most cases, those authors wish in vain - most programmers learn to follow the "rules of thumb" without understanding, as a habit rather than something they think about, and therefore eventually run into the inconvenient pitfalls.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #18
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Code:
    #include <stdio.h>
    
    int main()
    {
        float p,n,dm,q;
        float sum_p,sum_n,sum_dm,sum_q;
        float total_sum;
        float dollars;
    
        printf("\nEnter the number of pennies:\n");
        scanf("%f",&p);
        sum_p=p*1.00;
        
        printf("\nEnter the number of nickels:\n");
        scanf("%f",&n);
        sum_n=n*5.00;
        
        printf("\nEnter the number of dimes:\n");
        scanf("%f",&dm);
        sum_dm=dm*10.00;
    
        printf("\nEnter the number of quarters:\n");
        scanf("%f",&q);
        sum_q=q*25.00;
    
        total_sum=(sum_p)+(sum_n)+(sum_dm)+(sum_q);
        dollars=total_sum/100.00;
    
        printf("\nYou have $%.2f dollars\n",dollars);
    
    return(0);
    }

  4. #19
    Registered User
    Join Date
    Oct 2012
    Posts
    22
    Quote Originally Posted by Ozlem Kayra View Post
    Thanks a lot for very useful info I am very novice on programming and I thought "float" represent only decimal values between 0 and 1. And thats why I needed to calculate cents and dollars separately and get them together at the end. I created kind of complex formula to make the "sum" an integer. I know I should have read more before programming this.
    The best way to learn is through practice, I'm glad you learned from this as that's without a doubt the best thing you could've gotten from posting on here. There are plenty of great tutorials on the site if you'd like to read more about programming in C.

  5. #20
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    Quote Originally Posted by LTA85 View Post
    Code:
    #include <stdio.h>
    
    int main()
    {
        float p,n,dm,q;
        float sum_p,sum_n,sum_dm,sum_q;
        float total_sum;
        float dollars;
    
        printf("\nEnter the number of pennies:\n");
        scanf("%f",&p);
        sum_p=p*1.00;
        
        printf("\nEnter the number of nickels:\n");
        scanf("%f",&n);
        sum_n=n*5.00;
        
        printf("\nEnter the number of dimes:\n");
        scanf("%f",&dm);
        sum_dm=dm*10.00;
    
        printf("\nEnter the number of quarters:\n");
        scanf("%f",&q);
        sum_q=q*25.00;
    
        total_sum=(sum_p)+(sum_n)+(sum_dm)+(sum_q);
        dollars=total_sum/100.00;
    
        printf("\nYou have $%.2f dollars\n",dollars);
    
    return(0);
    }
    Thank you very much!

  6. #21
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Thank you very much!
    Yes, you got a spoon-fed answer from some overly enthusiastic newbie poster.

    The real question is, have you learnt anything from this exercise (except perhaps that there's a sucker to answer your question somewhere).

    Will you feel as confident with next weeks assignment (which will be harder)?
    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. #22
    Registered User
    Join Date
    Jan 2013
    Posts
    10
    Quote Originally Posted by Salem View Post
    > Thank you very much!
    Yes, you got a spoon-fed answer from some overly enthusiastic newbie poster.

    The real question is, have you learnt anything from this exercise (except perhaps that there's a sucker to answer your question somewhere).

    Will you feel as confident with next weeks assignment (which will be harder)?
    I don't know what did u mean exactly, but I learned a lot from these answers. Tomwa and LTA85 gave me good idea how to do program something similar to what I did. I know there is still a lot to learn, but I am not in a hurry.
    Last edited by Ozlem Kayra; 01-07-2013 at 04:48 AM.

  8. #23
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by laserlight View Post
    If there is no correct initial value at that point, then there is no correct initialisation.

    Consider this code:
    Code:
    int i = 0;
    scanf("%d", &i);
    printf("%d\n", i);
    When run with certain user input, the program prints "2". Was the initialisation of i with 0 a bug or was it intentional? If it was intentional, why was i not used before the scanf?
    In this example, the initialization to 0 is arbitrary but useful: if scanf() fails to find a valid integer, then the output is defined to be 0. Without an initilization, the output is undefined in the case that scanf fails to find an integer.

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by c99tutorial
    In this example, the initialization to 0 is arbitrary but useful: if scanf() fails to find a valid integer, then the output is defined to be 0. Without an initilization, the output is undefined in the case that scanf fails to find an integer.
    Nonetheless, it is still arbitrary, and it is probably a mistake to use i without further assignment if scanf fails. The correct approach to handling such an input error is not to check if i still has a value of 0 after the scanf call since indeed 0 is a valid input; rather, it would be to check the return value of scanf.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    Quote Originally Posted by Salem View Post
    Yes, you got a spoon-fed answer from some overly enthusiastic newbie poster.
    i was going to make the comment after reading page 1, "since when do we DO the coding for someone?"

  11. #26
    Registered User
    Join Date
    Oct 2012
    Posts
    22
    Quote Originally Posted by Salem View Post
    > Thank you very much!
    Yes, you got a spoon-fed answer from some overly enthusiastic newbie poster.

    The real question is, have you learnt anything from this exercise (except perhaps that there's a sucker to answer your question somewhere).

    Will you feel as confident with next weeks assignment (which will be harder)?
    Quote Originally Posted by Crossfire View Post
    i was going to make the comment after reading page 1, "since when do we DO the coding for someone?"
    I love how hostile people are on here, you act as though the OP said "Write me a program that gets the value of money from the number of coins" and I just did it, in reality a user posted his NEARLY complete code with errors (caused by his misunderstanding of floats) and asked how to fix a problem, I posted repaired code (Using comments to explain WHY things were changed) and posted a detailed post explaining what was broken (Initialization aside), I especially love how Salem isn't even addressing the fact that the OP blatantly stated how it cleared up the concept of floats for him.

    But hey go ahead and be hostile.
    Last edited by Tomwa; 01-08-2013 at 05:49 AM.

  12. #27
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Tomwa View Post
    I love how hostile people are on here, you act as though the OP said "Write me a program that gets the value of money from the number of coins" and I just did it, in reality a user posted his NEARLY complete code with errors (caused by his misunderstanding of floats) and asked how to fix a problem, I posted repaired code (Using comments to explain WHY things were changed) and posted a detailed post explaining what was broken (Initialization aside), I especially love how Salem isn't even addressing the fact that the OP blatantly stated how it cleared up the concept of floats for him.
    I'm not Salem but my reading of this thread is that Salem answered to the post of Ozlem Kayra in which he answered to the post of LTA85 who just dumped a complete solution without any further comments.

    Thus I don't think Salem had you in mind when he talks about an "overly enthusiastic newbie poster"

    Bye, Andreas
    Last edited by AndiPersti; 01-09-2013 at 04:36 AM.

  13. #28
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I liked your post, Tomwa - LTA's also. You gave plenty of info, and LTA's code was so clear that there was no need to explain anything further.

    The forum does have a problem, that if the OP posts no code, we quickly become a "please give me the codez" gathering spot, for lazy students.

    Clearly, that was NOT the case here. The OP's code was "close", imo.

    Well done, both of you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why is this simple c program not working ?
    By TexasKid in forum C Programming
    Replies: 5
    Last Post: 04-23-2012, 06:27 AM
  2. Simple program not working, don't know why
    By Bakster in forum C Programming
    Replies: 11
    Last Post: 01-29-2009, 01:56 PM
  3. Simple program not working
    By oobootsy1 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2005, 02:20 AM
  4. can't figure out simple program
    By blight2c in forum C++ Programming
    Replies: 4
    Last Post: 03-16-2002, 05:17 PM
  5. simple program not working
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-04-2002, 11:36 PM

Tags for this Thread