Thread: need a little help to make this code works!

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    45

    need a little help to make this code works!

    hi ..

    i'm having a trouble trying to make this code works !
    actually the question says:
    you have to ask the user for three values : positive double value for deposit, positive double value for interest rate , positive int for the number of years. and the program should compute the deposit in the savings account including interest after specified time of period with interest assessed ( every month) and added back to the deposit.so i have to compute the final deposit value ( which i declared as y )

    so here is what i came up with :
    Code:
    #include<stdio.h>
    
    
    
    
    int main(void)
    {
        double d ; // deposit
        double i ; //interest rate
        int x;     // years
        double y=0.00;     //final deposit value computed by the program
        
        
        
        printf("Enter the deposit in positive double value please:\n");
        scanf("%f" , &d);
        
        printf("Enter the interest rate in positive double value please:\n");
        scanf("%f" , &i);
        
        printf("Enter the time period in years:\n");
        scanf("%d" , &x);
        
        
        
        y= d*(1+(i*x));
        printf("After  %d  of years , the deposit in the savings account is now %1.2f \n" , x , y);
    
    
        
        return 0;
        
    }
    i tried ( 1200.00 as deposit , 0.08 as interest rate , 2 years) the output still 00.0 .. and i'm confused do i need to calculate the formula for 12 months ? please help me .. i appreciate it .. thanks !!

  2. #2
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    You should turn on warnings for your compiler and it would have told you.

    The correct format string for double is "%lf".
    "A Professor of Computer Science gave a paper on how he uses Linux to teach his undergraduates about operating systems. Someone in the audience asked why use Linux rather than Plan 9?' and the professor answered:Plan 9 looks like it was written by experts; Linux looks like something my students could aspire to write'."

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    45
    Quote Originally Posted by Jimmy View Post
    You should turn on warnings for your compiler and it would have told you.

    The correct format string for double is "%lf".
    thank you for your correction , but after making it %lf i still get 0.00000 as an answer !

  4. #4
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by Hana Nasser View Post
    thank you for your correction , but after making it %lf i still get 0.00000 as an answer !
    Post your code as it is now.

    What does your compiler say, now that you have turned on warnings, and are you sure you fixed both lines?
    "A Professor of Computer Science gave a paper on how he uses Linux to teach his undergraduates about operating systems. Someone in the audience asked why use Linux rather than Plan 9?' and the professor answered:Plan 9 looks like it was written by experts; Linux looks like something my students could aspire to write'."

  5. #5
    Registered User
    Join Date
    Feb 2015
    Posts
    45
    Quote Originally Posted by Jimmy View Post
    Post your code as it is now.

    What does your compiler say, now that you have turned on warnings, and are you sure you fixed both lines?
    yeah i fixed them
    but i'm afraid i have no idea how to turn on warnings..
    the program successfully compiled , the only problem is in the result ( which is y) probably sth wrong in y formula ?

  6. #6
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by Hana Nasser View Post
    yeah i fixed them
    but i'm afraid i have no idea how to turn on warnings..
    the program successfully compiled , the only problem is in the result ( which is y) probably sth wrong in y formula ?
    As I said, post your code as it is now.

    Also, nobody can help you turn on warnings for your compiler unless you say what compiler you are using. We are not mind readers...

    I cut and pasted your program and did the fix I told you and it works for me.
    "A Professor of Computer Science gave a paper on how he uses Linux to teach his undergraduates about operating systems. Someone in the audience asked why use Linux rather than Plan 9?' and the professor answered:Plan 9 looks like it was written by experts; Linux looks like something my students could aspire to write'."

  7. #7
    Registered User
    Join Date
    Feb 2015
    Posts
    45
    Quote Originally Posted by Jimmy View Post
    As I said, post your code as it is now.

    Also, nobody can help you turn on warnings for your compiler unless you say what compiler you are using. We are not mind readers...

    I cut and pasted your program and did the fix I told you and it works for me.
    silly me..i forgot to tell you i'm using (JGrasp)
    this is after i fixed the printf statement

    Code:
    #include<stdio.h>
    
    
    
    int main(void)
    {
        double d ; // deposit
        double i ; //interest rate
        int x;     // years
        double y=0.00;     //final deposit value computed by the program
        
        
        
        printf("Enter the deposit in positive double value please:\n");
        scanf("%lf" , &d);
        
        printf("Enter the interest rate in positive double value please:\n");
        scanf("%lf" , &i);
        
        printf("Enter the time period in years:\n");
        scanf("%d" , &x);
        
    
    
        
        y= d*(1+(i*x));
        printf("After  %d of years , the deposit in the savings account is now %1f \n" , x , y);
    
    
        
        return 0;
        
    }

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    It works almost OK for me.

    Edit: I am using a C Compiler instead of using something else.
    Do you know what a compiler is?

    Code:
    Enter the deposit in positive double value please:
    1200.00
    Enter the interest rate in positive double value please:
    0.08
    Enter the time period in years:
    2
    After  2 of years , the deposit in the savings account is now 1392.000000
    Tim S.
    Last edited by stahta01; 02-15-2015 at 12:14 PM.
    "...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

  9. #9
    Registered User
    Join Date
    Feb 2015
    Posts
    45
    Quote Originally Posted by stahta01 View Post
    It works almost OK for me.

    Edit: I am using a C Compiler instead of using something else.
    Do you know what a compiler is?

    Code:
    Enter the deposit in positive double value please:
    1200.00
    Enter the interest rate in positive double value please:
    0.08
    Enter the time period in years:
    2
    After  2 of years , the deposit in the savings account is now 1392.000000

    Tim S.
    thank you i appreciate ur help , at least it works but not for me.....what do u mean "C compiler" ?! i know that i can compile the code in any c compilers ex: grasp , netbeans , dev c/c++ is this what you mean ?

  10. #10
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    I think your scanf calls don't read the newline from stdin.
    Write a space in front of the format in the formatstring on line 15, 18 and 21.
    This trick catch all whitespaces in front of the digits.
    Other have classes, we are class

  11. #11
    Registered User
    Join Date
    Feb 2015
    Posts
    45
    Quote Originally Posted by WoodSTokk View Post
    I think your scanf calls don't read the newline from stdin.
    Write a space in front of the format in the formatstring on line 15, 18 and 21.
    This trick catch all whitespaces in front of the digits.
    YaaaaaaaaY it finally works !
    can't believe it was all about the spaces
    thank you so much i'll keep that in mind !
    have a good day

    hanaa

  12. #12
    Registered User
    Join Date
    Feb 2015
    Posts
    45
    Quote Originally Posted by stahta01 View Post
    It works almost OK for me.

    Edit: I am using a C Compiler instead of using something else.
    Do you know what a compiler is?

    Code:
    Enter the deposit in positive double value please:
    1200.00
    Enter the interest rate in positive double value please:
    0.08
    Enter the time period in years:
    2
    After  2 of years , the deposit in the savings account is now 1392.000000
    Tim S.
    it works now ,, thank you for you help
    have a good day

    hanaa

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Hana Nasser View Post
    thank you i appreciate ur help , at least it works but not for me.....what do u mean "C compiler" ?! i know that i can compile the code in any c compilers ex: grasp , netbeans , dev c/c++ is this what you mean ?
    Those are IDEs; you really need to learn what a Compiler is; then, learn the name of your compiler.

    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. why this code Works.
    By jafar in forum C Programming
    Replies: 4
    Last Post: 05-01-2014, 03:53 PM
  2. How do I make a constructor make code fail?
    By MutantJohn in forum C++ Programming
    Replies: 29
    Last Post: 08-31-2013, 06:59 PM
  3. Replies: 4
    Last Post: 10-11-2011, 01:57 PM
  4. How come my code works? Seriously. :)
    By assiduus in forum C Programming
    Replies: 15
    Last Post: 02-10-2011, 12:54 PM
  5. How Do u See if a code works
    By Nos in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2005, 01:34 PM